Class: RGame::Engine::Components::Timer

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

Overview

A node-driven interval timer: it rides the node's update tick — so nothing can forget to advance it — and emits on_timeout each time a whole interval elapses. It wraps the pure Engine::Timer, reusing its carry-forward consume (the remainder rolls over, so the cadence doesn't drift); this only adds the per-frame drive and the signal.

timer = node.add_component(Engine::Components::Timer.new(0.8), as: :spawn)
timer.on_timeout { spawn_enemy }

repeating: false makes it a one-shot: it fires on_timeout exactly once and then goes inert — the fixed-board despawn case (a projectile that vanishes after N seconds, where DespawnOffscreen doesn't apply):

life = node.add_component(Engine::Components::Timer.new(2.0, repeating: false), as: :despawn)
life.on_timeout { node.queue_free }

The countdown restarts in on_attach, so a pooled node reacquired and re-added starts fresh — a recycled projectile gets its full interval (and a one-shot can fire again) rather than inheriting the previous life's elapsed time.

When a node needs more than one (a spawn cadence and a wave cadence), give them distinct as: names — a node holds one component per slot.

Instance Attribute Summary

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(interval, repeating: true) ⇒ Timer

Returns a new instance of Timer.



31
32
33
34
35
36
# File 'lib/rgame/engine/components/timer.rb', line 31

def initialize(interval, repeating: true)
  super()
  @timer = Engine::Timer.new(interval)
  @repeating = repeating
  @done = false
end

Instance Method Details

#intervalObject



38
# File 'lib/rgame/engine/components/timer.rb', line 38

def interval = @timer.interval

#interval=(seconds) ⇒ Object



40
41
42
# File 'lib/rgame/engine/components/timer.rb', line 40

def interval=(seconds)
  @timer.interval = seconds
end

#on_attachObject

Start the countdown fresh whenever the node (re-)enters the tree, so a pooled node never inherits a previous life's accumulated time or spent one-shot.



46
# File 'lib/rgame/engine/components/timer.rb', line 46

def on_attach = reset

#resetObject

Back to a fresh timer: drop accumulated time and re-arm a spent one-shot (e.g. after retuning the interval, or when a pooled node is reused). Returns self.



67
68
69
70
71
# File 'lib/rgame/engine/components/timer.rb', line 67

def reset
  @timer.reset
  @done = false
  self
end

#update(dt) ⇒ Object

Advance one step and fire on_timeout once per whole interval that elapsed — so a single long step still emits the right number of times (catch-up, not drift). A one-shot (repeating: false) fires once and then stops accumulating.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rgame/engine/components/timer.rb', line 51

def update(dt)
  return if @done

  @timer.update(dt)
  while @timer.ready?
    @timer.consume
    on_timeout_signal.emit
    unless @repeating
      @done = true
      break
    end
  end
end