Module: Chingu::Traits::BoundingBox
- Defined in:
- lib/chingu/traits/bounding_box.rb
Overview
Providing a bounding_box-method which generates a AABB on the fly from: x, y, factor_x, factor_y and rotation_center
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- CENTER_TO_FACTOR =
{ 0 => -1, 0.5 => 0, 1 => 1 }
Instance Attribute Summary collapse
-
#collidable ⇒ Object
Returns the value of attribute collidable.
Instance Method Summary collapse
-
#bounding_box ⇒ Object
(also: #bb)
Returns an instance of class Rect.
- #cache_bounding_box ⇒ Object
- #collision_at?(x, y) ⇒ Boolean
-
#draw_debug ⇒ Object
Visualises the bounding box as a red rectangle.
-
#draw_trait ⇒ Object
def update_trait cache_bounding_box if trait_options[:cache] && !@cached_bounding_box super end.
- #setup_trait(options) ⇒ Object
Instance Attribute Details
#collidable ⇒ Object
Returns the value of attribute collidable.
30 31 32 |
# File 'lib/chingu/traits/bounding_box.rb', line 30 def collidable @collidable end |
Instance Method Details
#bounding_box ⇒ Object Also known as: bb
Returns an instance of class Rect
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/chingu/traits/bounding_box.rb', line 51 def bounding_box if @cached_bounding_box @cached_bounding_box.x = self.x + @_x_diff @cached_bounding_box.y = self.y + @_y_diff return @cached_bounding_box end width, height = self.size if [:bounding_box][:scale] width = width * [:bounding_box][:scale] height = height * [:bounding_box][:scale] end x = self.x - width * self.center_x y = self.y - height * self.center_y x += width * CENTER_TO_FACTOR[self.center_x] if self.factor_x < 0 y += height * CENTER_TO_FACTOR[self.center_y] if self.factor_y < 0 return Rect.new(x, y, width, height) end |
#cache_bounding_box ⇒ Object
75 76 77 78 79 80 |
# File 'lib/chingu/traits/bounding_box.rb', line 75 def cache_bounding_box @cached_bounding_box = nil @cached_bounding_box = self.bounding_box @_x_diff = @cached_bounding_box.x - self.x @_y_diff = @cached_bounding_box.y - self.y end |
#collision_at?(x, y) ⇒ Boolean
44 45 46 |
# File 'lib/chingu/traits/bounding_box.rb', line 44 def collision_at?(x, y) bounding_box.collide_point?(x,y) end |
#draw_debug ⇒ Object
Visualises the bounding box as a red rectangle.
95 96 97 |
# File 'lib/chingu/traits/bounding_box.rb', line 95 def draw_debug $window.draw_rect(self.bounding_box, Chingu::DEBUG_COLOR, Chingu::DEBUG_ZORDER) end |
#draw_trait ⇒ Object
def update_trait
cache_bounding_box if [:bounding_box][:cache] && !@cached_bounding_box
super
end
87 88 89 90 |
# File 'lib/chingu/traits/bounding_box.rb', line 87 def draw_trait draw_debug if [:bounding_box][:debug] super end |
#setup_trait(options) ⇒ Object
38 39 40 41 42 |
# File 'lib/chingu/traits/bounding_box.rb', line 38 def setup_trait() @cached_bounding_box = nil @collidable = true super end |