Class: RGame::Engine::Components::ActionTrigger
- Inherits:
-
RGame::Engine::Component
- Object
- RGame::Engine::Component
- RGame::Engine::Components::ActionTrigger
- Defined in:
- lib/rgame/engine/components/action_trigger.rb
Overview
Maps held input actions to an on_triggered(action) signal, rate-limited by a
per-action cooldown. One instance covers several actions (the engine allows
only one component of a class per node), so it emits the action name and lets
listeners filter — reusable for "fire" here, or "jump"/"fire" in a platformer.
trigger = node.add_component(ActionTrigger.new(fire: 0.22, dash: 0.5))
trigger.on_triggered { |action| fire if action == :fire }
Semantics: while an action is held and its cooldown has elapsed, it fires and the cooldown restarts — i.e. auto-repeat at the cooldown rate.
Instance Attribute Summary
Attributes inherited from RGame::Engine::Component
Instance Method Summary collapse
- #control(actions) ⇒ Object
-
#initialize(cooldowns) ⇒ ActionTrigger
constructor
A new instance of ActionTrigger.
- #update(dt) ⇒ Object
Methods inherited from RGame::Engine::Component
#context, #draw, #on_attach, #on_detach, #sweep_freed
Methods included from Signal::DSL
Constructor Details
#initialize(cooldowns) ⇒ ActionTrigger
Returns a new instance of ActionTrigger.
19 20 21 22 23 24 |
# File 'lib/rgame/engine/components/action_trigger.rb', line 19 def initialize(cooldowns) super() @cooldowns = cooldowns # action => seconds remaining until it may fire again (0 = ready). @timers = cooldowns.transform_values { 0.0 } end |
Instance Method Details
#control(actions) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/rgame/engine/components/action_trigger.rb', line 30 def control(actions) @cooldowns.each_key do |action| next unless actions.held?(action) && @timers[action] <= 0.0 @timers[action] = @cooldowns[action] on_triggered_signal.emit(action) end end |
#update(dt) ⇒ Object
26 27 28 |
# File 'lib/rgame/engine/components/action_trigger.rb', line 26 def update(dt) @timers.each { |action, remaining| @timers[action] = remaining - dt if remaining.positive? } end |