Class: RichEngine::Timer

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

Overview

Accumulates elapsed time; drive it by calling #update each frame.

Defined Under Namespace

Classes: Every

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



18
19
20
# File 'lib/rich_engine/timer.rb', line 18

def initialize
  @timer = 0
end

Class Method Details

.every(seconds: 1, &block) ⇒ RichEngine::Timer::Every

Returns a small scheduler that fires a block at a fixed interval.

Examples:

spawn = RichEngine::Timer.every(seconds: 0.5)
spawn.update(dt)
spawn.when_ready { spawn_enemy! }

Parameters:

  • seconds (Integer, Float) (defaults to: 1)

    the interval between firings.

Returns:



14
15
16
# File 'lib/rich_engine/timer.rb', line 14

def self.every(seconds: 1, &block)
  Every.new(seconds)
end

Instance Method Details

#getFloat

Returns the accumulated time in seconds.

Returns:

  • (Float)

    the accumulated time in seconds.



31
32
33
# File 'lib/rich_engine/timer.rb', line 31

def get
  @timer
end

#reset!Integer

Resets the accumulated time back to zero.

Returns:

  • (Integer)

    zero.



38
39
40
# File 'lib/rich_engine/timer.rb', line 38

def reset!
  @timer = 0
end

#update(dt) ⇒ Float

Adds the elapsed time to the accumulated total.

Parameters:

  • dt (Float)

    seconds since the last frame.

Returns:

  • (Float)

    the new accumulated time.



26
27
28
# File 'lib/rich_engine/timer.rb', line 26

def update(dt)
  @timer += dt
end