Module: Chingu::Traits::BoundingCircle

Defined in:
lib/chingu/traits/bounding_circle.rb

Overview

Providing a bounding circle in the form of 2 attributes, self.radius and self.diameter It creates these 2 attributes from reading image.height, image.width, factor_x and factor_y

…this usually only makes sense with rotation_center = :center

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#collidableObject

Returns the value of attribute collidable



31
32
33
# File 'lib/chingu/traits/bounding_circle.rb', line 31

def collidable
  @collidable
end

Instance Method Details

#cache_bounding_circleObject



60
61
62
# File 'lib/chingu/traits/bounding_circle.rb', line 60

def cache_bounding_circle
  @cached_radius = self.radius
end

#circle_bottomObject



72
# File 'lib/chingu/traits/bounding_circle.rb', line 72

def circle_bottom;  self.y + self.radius; end

#circle_leftObject

def update_trait

cache_bounding_circle  if trait_options[:bounding_circle][:cache] && !@cached_radius
super

end



69
# File 'lib/chingu/traits/bounding_circle.rb', line 69

def circle_left;    self.x - self.radius; end

#circle_rightObject



70
# File 'lib/chingu/traits/bounding_circle.rb', line 70

def circle_right;   self.x + self.radius; end

#circle_topObject



71
# File 'lib/chingu/traits/bounding_circle.rb', line 71

def circle_top;     self.y - self.radius; end

#collision_at?(x, y) ⇒ Boolean



45
46
47
# File 'lib/chingu/traits/bounding_circle.rb', line 45

def collision_at?(x, y)
  Gosu.distance(self.x, self.y, x, y) < radius
end

#diameterObject



56
57
58
# File 'lib/chingu/traits/bounding_circle.rb', line 56

def diameter
  radius * 2
end

#draw_debugObject

Visualises the bounding circle as a red circle.



82
83
84
# File 'lib/chingu/traits/bounding_circle.rb', line 82

def draw_debug
  $window.draw_circle(self.x, self.y, self.radius, Chingu::DEBUG_COLOR)
end

#draw_traitObject



74
75
76
77
# File 'lib/chingu/traits/bounding_circle.rb', line 74

def draw_trait
  draw_debug  if trait_options[:bounding_circle][:debug]
  super
end

#radiusObject



49
50
51
52
53
54
# File 'lib/chingu/traits/bounding_circle.rb', line 49

def radius
  return @cached_radius if @cached_radius        
  radius = (self.width + self.height) / 4
  radius = radius * trait_options[:bounding_circle][:scale] if  trait_options[:bounding_circle][:scale]
  return radius
end

#setup_trait(options) ⇒ Object



39
40
41
42
43
# File 'lib/chingu/traits/bounding_circle.rb', line 39

def setup_trait(options)
  @cached_radius = nil
  @collidable = true
  super
end