Class: RGame::Engine::CollisionBox

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#heightObject (readonly)

Returns the value of attribute height.



9
10
11
# File 'lib/rgame/engine/collision_box.rb', line 9

def height
  @height
end

#offset_xObject (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_yObject (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

#widthObject (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