Class: RGame::Engine::CollisionBox
- Inherits:
-
Object
- Object
- RGame::Engine::CollisionBox
- Defined in:
- lib/rgame/engine/collision_box.rb
Overview
A character's collision rectangle, expressed as an offset + size relative to the actor's top-left origin (the sprite's top-left). Decoupled from sprite size: a 32x32 sprite can have a small 16x16 box at its feet.
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#offset_x ⇒ Object
readonly
Returns the value of attribute offset_x.
-
#offset_y ⇒ Object
readonly
Returns the value of attribute offset_y.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
-
.bottom_anchored(sprite_width:, sprite_height:, width:, height:) ⇒ Object
Centre the box horizontally and anchor it to the bottom of the sprite (feet).
Instance Method Summary collapse
-
#aabb(x, y) ⇒ Object
World-space AABB [x, y, w, h] for an actor whose origin is at (x, y).
-
#initialize(offset_x:, offset_y:, width:, height:) ⇒ CollisionBox
constructor
A new instance of CollisionBox.
Constructor Details
#initialize(offset_x:, offset_y:, width:, height:) ⇒ CollisionBox
Returns a new instance of CollisionBox.
21 22 23 24 25 26 |
# File 'lib/rgame/engine/collision_box.rb', line 21 def initialize(offset_x:, offset_y:, width:, height:) @offset_x = offset_x @offset_y = offset_y @width = width @height = height end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
9 10 11 |
# File 'lib/rgame/engine/collision_box.rb', line 9 def height @height end |
#offset_x ⇒ Object (readonly)
Returns the value of attribute offset_x.
9 10 11 |
# File 'lib/rgame/engine/collision_box.rb', line 9 def offset_x @offset_x end |
#offset_y ⇒ Object (readonly)
Returns the value of attribute offset_y.
9 10 11 |
# File 'lib/rgame/engine/collision_box.rb', line 9 def offset_y @offset_y end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
9 10 11 |
# File 'lib/rgame/engine/collision_box.rb', line 9 def width @width end |
Class Method Details
.bottom_anchored(sprite_width:, sprite_height:, width:, height:) ⇒ Object
Centre the box horizontally and anchor it to the bottom of the sprite (feet).
12 13 14 15 16 17 18 19 |
# File 'lib/rgame/engine/collision_box.rb', line 12 def self.bottom_anchored(sprite_width:, sprite_height:, width:, height:) new( offset_x: (sprite_width - width) / 2, offset_y: sprite_height - height, width: width, height: height ) end |
Instance Method Details
#aabb(x, y) ⇒ Object
World-space AABB [x, y, w, h] for an actor whose origin is at (x, y).
29 30 31 |
# File 'lib/rgame/engine/collision_box.rb', line 29 def aabb(x, y) [x + @offset_x, y + @offset_y, @width, @height] end |