Class: RGame::Engine::Components::CircleCollider

Inherits:
RGame::Engine::Component show all
Defined in:
lib/rgame/engine/components/circle_collider.rb

Overview

A circular collision shape on a node. It registers itself with the scene's CollisionWorld system when it enters the tree and unregisters on leaving — the engine fires both hooks, so a spawned/despawned entity can't leak a registration. Its world centre is the node's resolved absolute origin; the layer is an opaque tag the game reads in its on_hit handler to decide what a contact means (bullet-vs-rock, etc.). See docs/api/systems.md.

Instance Attribute Summary collapse

Attributes inherited from RGame::Engine::Component

#node

Instance Method Summary collapse

Methods inherited from RGame::Engine::Component

#context, #control, #draw, #sweep_freed, #update

Methods included from Signal::DSL

#signal

Constructor Details

#initialize(radius:, layer: :default) ⇒ CircleCollider

Returns a new instance of CircleCollider.



22
23
24
25
26
# File 'lib/rgame/engine/components/circle_collider.rb', line 22

def initialize(radius:, layer: :default)
  super()
  @radius = radius
  @layer = layer
end

Instance Attribute Details

#layerObject (readonly)

Returns the value of attribute layer.



20
21
22
# File 'lib/rgame/engine/components/circle_collider.rb', line 20

def layer
  @layer
end

#radiusObject

radius is writable so a pooled entity (e.g. a multi-tier rock) can retune its shape on reset; CollisionWorld reads it fresh each frame, so no re-registration.



19
20
21
# File 'lib/rgame/engine/components/circle_collider.rb', line 19

def radius
  @radius
end

Instance Method Details

#cxObject

World-space centre — the node's resolved absolute origin.



32
# File 'lib/rgame/engine/components/circle_collider.rb', line 32

def cx = node.abs_x

#cyObject



33
# File 'lib/rgame/engine/components/circle_collider.rb', line 33

def cy = node.abs_y

#emit_hit(other) ⇒ Object

Called by CollisionWorld on contact (the signal's emit is otherwise private).



40
# File 'lib/rgame/engine/components/circle_collider.rb', line 40

def emit_hit(other) = on_hit_signal.emit(other)

#on_attachObject



28
# File 'lib/rgame/engine/components/circle_collider.rb', line 28

def on_attach = node.system(CollisionWorld).register(self)

#on_detachObject



29
# File 'lib/rgame/engine/components/circle_collider.rb', line 29

def on_detach = node.system(CollisionWorld)&.unregister(self)

#overlap?(other) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rgame/engine/components/circle_collider.rb', line 35

def overlap?(other)
  Engine::CircleCollider.overlap?(cx, cy, @radius, other.cx, other.cy, other.radius)
end