Class: RichEngine::Timer::Every

Inherits:
Object
  • Object
show all
Defined in:
lib/rich_engine/timer/every.rb

Overview

A scheduler that fires at a fixed interval, created via every.

Instance Method Summary collapse

Constructor Details

#initialize(interval) ⇒ Every

Returns a new instance of Every.

Parameters:

  • interval (Integer, Float)

    seconds between firings.



8
9
10
11
12
# File 'lib/rich_engine/timer/every.rb', line 8

def initialize(interval)
  @interval = interval
  @ready = false
  @timer = Timer.new
end

Instance Method Details

#interval=(interval) ⇒ void

This method returns an undefined value.

Changes the firing interval and resets the timer.

Parameters:

  • interval (Integer, Float)

    the new interval in seconds.



42
43
44
45
# File 'lib/rich_engine/timer/every.rb', line 42

def interval=(interval)
  @interval = interval
  reset!
end

#update(elapsed_time) ⇒ void

This method returns an undefined value.

Accumulates elapsed time and marks the scheduler ready once the interval is reached.

Parameters:

  • elapsed_time (Float)

    seconds since the last frame.



19
20
21
22
23
24
25
# File 'lib/rich_engine/timer/every.rb', line 19

def update(elapsed_time)
  @timer.update(elapsed_time)

  if @timer.get >= @interval
    @ready = true
  end
end

#when_ready { ... } ⇒ void

This method returns an undefined value.

Runs the block and resets the timer when the interval has elapsed.

Yields:

  • called once each time the interval is reached.



31
32
33
34
35
36
# File 'lib/rich_engine/timer/every.rb', line 31

def when_ready(&block)
  if @ready
    block.call
    reset!
  end
end