Class: Zif::Actions::Sequence

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/zif/actions/sequence.rb

Overview

Inspired by developer.apple.com/documentation/spritekit/skaction

A sequence of Actions that can be repeated as a whole

Meant to be applied to an object using the Actionable mixin, just like Action

Instance Attribute Summary collapse

1. Public Interface collapse

2. Private-ish methods collapse

Methods included from Serializable

#exclude_from_serialize, #inspect, #serialize, #to_s

Constructor Details

#initialize(actions, repeat: 1, &block) ⇒ Sequence

Returns a new instance of Sequence.

Parameters:

  • actions (Array<Zif::Actions::Action>)

    The ordered list of actions in this sequence

  • repeat (Integer, Symbol) (defaults to: 1)

    (see Action::REPEAT_NAMES for valid symbols)

  • block (Block)

    Callback to perform when the entire sequence completes



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/zif/actions/sequence.rb', line 35

def initialize(actions, repeat: 1, &block)
  actions.each do |action|
    raise ArgumentError, "Invalid action: #{action}" unless action.is_a? Action
  end

  @callback = block if block_given?

  @sub_actions = actions
  @repeat = Action::REPEAT_NAMES[repeat] || repeat

  restart
  setup_action
end

Instance Attribute Details

#action_indexInteger (readonly)

Returns Index of the current action for this sequence.

Returns:

  • (Integer)

    Index of the current action for this sequence



12
13
14
# File 'lib/zif/actions/sequence.rb', line 12

def action_index
  @action_index
end

#callbackProc

Returns A callback to run at the end of the entire sequence.

Returns:

  • (Proc)

    A callback to run at the end of the entire sequence



27
28
29
# File 'lib/zif/actions/sequence.rb', line 27

def callback
  @callback
end

#cur_repeatInteger

Returns The number of repeat iterations left.

Returns:

  • (Integer)

    The number of repeat iterations left



21
22
23
# File 'lib/zif/actions/sequence.rb', line 21

def cur_repeat
  @cur_repeat
end

#repeatInteger

Returns The number of times this Sequence should repeat.

Returns:

  • (Integer)

    The number of times this Sequence should repeat



18
19
20
# File 'lib/zif/actions/sequence.rb', line 18

def repeat
  @repeat
end

#sub_actionsArray<Zif::Actions::Action>

Returns The actions this sequence runs through.

Returns:



15
16
17
# File 'lib/zif/actions/sequence.rb', line 15

def sub_actions
  @sub_actions
end

#sub_repeatsArray<Integer> (readonly)

Returns The number of times each Action in #sub_actions should repeat before moving on.

Returns:

  • (Array<Integer>)

    The number of times each Action in #sub_actions should repeat before moving on



24
25
26
# File 'lib/zif/actions/sequence.rb', line 24

def sub_repeats
  @sub_repeats
end

Instance Method Details

#complete?Boolean

Returns Has this sequence run out of repeat iterations?.

Returns:

  • (Boolean)

    Has this sequence run out of repeat iterations?



62
63
64
65
# File 'lib/zif/actions/sequence.rb', line 62

def complete?
  # puts "Complete action! - #{@node.class}" if @repeat.zero?
  @cur_repeat.zero?
end

#cur_actionZif::Actions::Action

Returns the current Action in this sequence.

Returns:



57
58
59
# File 'lib/zif/actions/sequence.rb', line 57

def cur_action
  @sub_actions[@action_index]
end

#next_actionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Advances the sequence to the next action.



81
82
83
84
85
86
87
88
# File 'lib/zif/actions/sequence.rb', line 81

def next_action
  # puts "Sequence#next_action"
  reset_cur_action_repeat
  @action_index = (@action_index + 1) % @sub_actions.length
  @cur_repeat -= 1 if @action_index.zero?

  setup_action unless complete?
end

#perform_callbackObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls the #callback on self



112
113
114
115
# File 'lib/zif/actions/sequence.rb', line 112

def perform_callback
  @callback.call(self)
  # puts "Sequence#perform_callback: Callback triggered"
end

#perform_tickBoolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calls the current action’s Action#perform_tick Conditionally advances to the next action and performs the callback.

Returns:

  • (Boolean)

    Did the current action cause a change on the node during this tick?



100
101
102
103
104
105
106
107
108
# File 'lib/zif/actions/sequence.rb', line 100

def perform_tick
  # puts "Sequence#perform_tick"
  @dirty = cur_action.perform_tick

  next_action if cur_action.complete?
  perform_callback if complete? && @callback

  @dirty
end

#reset_cur_action_repeatObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the current action’s Action#repeat to the value it was initialized with.



92
93
94
# File 'lib/zif/actions/sequence.rb', line 92

def reset_cur_action_repeat
  cur_action.repeat = @sub_repeats[@action_index]
end

#restartObject

Resets the state of the sequence back to the initialized state.



50
51
52
53
54
# File 'lib/zif/actions/sequence.rb', line 50

def restart
  @action_index = 0
  @cur_repeat = @repeat
  @sub_repeats = @sub_actions.map(&:repeat).freeze
end

#setup_actionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resets the current action’s parameters, so it is fresh and ready to run.



72
73
74
75
76
77
# File 'lib/zif/actions/sequence.rb', line 72

def setup_action
  # puts "Sequence#setup_action #{@action_index}"
  reset_cur_action_repeat
  cur_action.reset_start
  cur_action.reset_duration
end