Class: RichEngine::Cooldown

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

Overview

Tracks a fixed delay and reports when it has elapsed.

Examples:

shoot_cd = RichEngine::Cooldown.new(0.25) # seconds
shoot_cd.update(dt)
if shoot_cd.ready?
  shoot!
  shoot_cd.reset!
end

Instance Method Summary collapse

Constructor Details

#initialize(target_time) ⇒ Cooldown

Returns a new instance of Cooldown.

Parameters:

  • target_time (Integer, Float)

    the cooldown duration in seconds.



15
16
17
18
# File 'lib/rich_engine/cooldown.rb', line 15

def initialize(target_time)
  @target_time = target_time
  @timer = target_time
end

Instance Method Details

#finished?Boolean Also known as: ready?

Returns whether the cooldown has elapsed.

Returns:

  • (Boolean)

    whether the cooldown has elapsed.



41
42
43
# File 'lib/rich_engine/cooldown.rb', line 41

def finished?
  @timer <= 0
end

#getFloat

Returns the remaining time in seconds (negative once finished).

Returns:

  • (Float)

    the remaining time in seconds (negative once finished).



29
30
31
# File 'lib/rich_engine/cooldown.rb', line 29

def get
  @timer
end

#reset!Integer, Float

Restarts the cooldown at its full duration.

Returns:

  • (Integer, Float)

    the reset remaining time.



36
37
38
# File 'lib/rich_engine/cooldown.rb', line 36

def reset!
  @timer = @target_time
end

#update(dt) ⇒ Float

Counts down by the elapsed time.

Parameters:

  • dt (Float)

    seconds since the last frame.

Returns:

  • (Float)

    the remaining time.



24
25
26
# File 'lib/rich_engine/cooldown.rb', line 24

def update(dt)
  @timer -= dt
end