Class: RGame::Engine::Components::Targeting

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

Overview

Picks an enemy for the owning node (a tower) to aim at: each update it queries the scene's CollisionWorld around the node's world origin and exposes the chosen target via #target (the enemy node, or nil). The owner reads it to fire — Targeting only selects, it never moves or shoots.

The CollisionWorld it queries is the same broadphase enemies register with through their CircleCollider, so targeting needs no enemy list of its own. The layer restricts candidates (a tower wants :enemy, not other towers or projectiles).

Policies (how to choose among the candidates in range):

- :nearest — the closest enemy (the default; one broadphase nearest-lookup).

More policies (e.g. furthest-along-path) arrive with the state they need.

Constant Summary collapse

POLICIES =
%i[nearest].freeze

Instance Attribute Summary collapse

Attributes inherited from RGame::Engine::Component

#node

Instance Method Summary collapse

Methods inherited from RGame::Engine::Component

#context, #control, #draw, #on_detach, #sweep_freed

Methods included from Signal::DSL

#signal

Constructor Details

#initialize(range:, policy: :nearest, layer: nil) ⇒ Targeting

Returns a new instance of Targeting.

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
29
# File 'lib/rgame/engine/components/targeting.rb', line 21

def initialize(range:, policy: :nearest, layer: nil)
  super()
  raise ArgumentError, "unknown targeting policy #{policy.inspect}" unless POLICIES.include?(policy)

  @range = range
  @policy = policy
  @layer = layer
  @target = nil
end

Instance Attribute Details

#targetObject (readonly)

The chosen enemy node, or nil when nothing is in range. Refreshed every update.



32
33
34
# File 'lib/rgame/engine/components/targeting.rb', line 32

def target
  @target
end

Instance Method Details

#on_attachObject

Pull the broadphase once it's reachable (scene scope, resolved up the tree).



35
# File 'lib/rgame/engine/components/targeting.rb', line 35

def on_attach = @world = node.system(CollisionWorld)

#update(_dt) ⇒ Object



37
38
39
40
# File 'lib/rgame/engine/components/targeting.rb', line 37

def update(_dt)
  collider = pick
  @target = collider&.node
end