Class: Metro::OnUpdateOperation

Inherits:
Object
  • Object
show all
Defined in:
lib/metro/animation/on_update_operation.rb

Overview

OnUpdateOperation is an object that executes on the update cycle of the game. This usually take the form of an animation or some operation that needs to execute with each update of the game.

Direct Known Subclasses

ImplicitAnimation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ OnUpdateOperation

Returns a new instance of OnUpdateOperation.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/metro/animation/on_update_operation.rb', line 12

def initialize(options)
  @current_step = 0

  options.each do |key,value|
    send :instance_variable_set, "@#{key}".to_sym, value
    self.class.send :define_method, key do
      instance_variable_get("@#{key}")
    end
  end
  after_initialize
end

Instance Attribute Details

#complete_blockObject (readonly)

Returns the value of attribute complete_block.



55
56
57
# File 'lib/metro/animation/on_update_operation.rb', line 55

def complete_block
  @complete_block
end

#current_stepObject (readonly)

Returns the current step of the animation.

Returns:

  • the current step of the animation.



53
54
55
# File 'lib/metro/animation/on_update_operation.rb', line 53

def current_step
  @current_step
end

#step_blockObject (readonly)

Returns the value of attribute step_block.



55
56
57
# File 'lib/metro/animation/on_update_operation.rb', line 55

def step_block
  @step_block
end

Instance Method Details

#after_initializeObject



10
# File 'lib/metro/animation/on_update_operation.rb', line 10

def after_initialize ; end

#complete!Object

Perform the action that happens when the animation is completed.



89
90
91
# File 'lib/metro/animation/on_update_operation.rb', line 89

def complete!
  context.instance_eval(&@complete_block) if complete_block and context
end

#execute_stepObject

Perform the action that happens with each step of the animation.



82
83
84
# File 'lib/metro/animation/on_update_operation.rb', line 82

def execute_step
  context.instance_eval(&@step_block) if step_block and context
end

#next_stepObject

Move to the next step in the animation.



60
61
62
# File 'lib/metro/animation/on_update_operation.rb', line 60

def next_step
  @current_step = current_step + step_interval
end

#on_complete(&block) ⇒ Object

Sets the action that happens when the animation is completed.



34
35
36
# File 'lib/metro/animation/on_update_operation.rb', line 34

def on_complete(&block)
  @complete_block = block
end

#on_step(&block) ⇒ Object

Sets the action that happens with each step of the animation.



27
28
29
# File 'lib/metro/animation/on_update_operation.rb', line 27

def on_step(&block)
  @step_block = block
end

#step_intervalObject

Returns the interval at which the animation take place.

Returns:

  • the interval at which the animation take place.



67
68
69
# File 'lib/metro/animation/on_update_operation.rb', line 67

def step_interval
  1
end

#updateObject

Perform a step of an animation, if it hasn’t already been completed.



41
42
43
44
45
46
47
48
# File 'lib/metro/animation/on_update_operation.rb', line 41

def update
  return if update_completed?

  execute_step
  next_step

  complete! if update_completed?
end

#update_completed?Boolean

Returns true if the animation has completed all the actions, false if there are remaining actions.

Returns:

  • (Boolean)

    true if the animation has completed all the actions, false if there are remaining actions.



75
76
77
# File 'lib/metro/animation/on_update_operation.rb', line 75

def update_completed?
  current_step >= interval
end