Class: RGame::Engine::Components::Targeting
- Inherits:
-
RGame::Engine::Component
- Object
- RGame::Engine::Component
- RGame::Engine::Components::Targeting
- 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
-
#target ⇒ Object
readonly
The chosen enemy node, or nil when nothing is in range.
Attributes inherited from RGame::Engine::Component
Instance Method Summary collapse
-
#initialize(range:, policy: :nearest, layer: nil) ⇒ Targeting
constructor
A new instance of Targeting.
-
#on_attach ⇒ Object
Pull the broadphase once it's reachable (scene scope, resolved up the tree).
- #update(_dt) ⇒ Object
Methods inherited from RGame::Engine::Component
#context, #control, #draw, #on_detach, #sweep_freed
Methods included from Signal::DSL
Constructor Details
#initialize(range:, policy: :nearest, layer: nil) ⇒ Targeting
Returns a new instance of Targeting.
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
#target ⇒ Object (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_attach ⇒ Object
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 |