Class: Zyps::TimedAction

Inherits:
Action
  • Object
show all
Defined in:
lib/zyps/actions.rb

Overview

Superclass for actions that need to happen at a specific rate.

Instance Attribute Summary collapse

Attributes inherited from Action

#started

Instance Method Summary collapse

Methods inherited from Action

#do, #started?

Constructor Details

#initialize(rate, *arguments) ⇒ TimedAction

Returns a new instance of TimedAction.



32
33
34
35
# File 'lib/zyps/actions.rb', line 32

def initialize(rate, *arguments)
  self.rate = rate
  @clock = Clock.new
end

Instance Attribute Details

#clockObject

A Clock that tracks time between actions.



28
29
30
# File 'lib/zyps/actions.rb', line 28

def clock
  @clock
end

#rateObject

Units per second for action.



30
31
32
# File 'lib/zyps/actions.rb', line 30

def rate
  @rate
end

Instance Method Details

#copyObject

Make a deep copy.



38
39
40
41
42
43
# File 'lib/zyps/actions.rb', line 38

def copy
  copy = super
  #Copies should have their own Clock.
  copy.clock = Clock.new
  copy
end

#deltaObject

Units to add to the attribute being changed.



46
47
48
# File 'lib/zyps/actions.rb', line 46

def delta
  @clock.elapsed_time * rate
end

#start(actor, targets) ⇒ Object

Begin tracking time between actions.



51
52
53
54
# File 'lib/zyps/actions.rb', line 51

def start(actor, targets)
  super
  @clock.reset_elapsed_time
end

#stop(actor, targets) ⇒ Object

Halt tracking time between actions.



57
58
59
60
# File 'lib/zyps/actions.rb', line 57

def stop(actor, targets)
  super
  @clock.reset_elapsed_time
end