Class: Zif::Actions::Sequence
- Inherits:
-
Object
- Object
- Zif::Actions::Sequence
- 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
-
#action_index ⇒ Integer
readonly
Index of the current action for this sequence.
-
#callback ⇒ Proc
A callback to run at the end of the entire sequence.
-
#cur_repeat ⇒ Integer
The number of repeat iterations left.
-
#repeat ⇒ Integer
The number of times this Sequence should repeat.
-
#sub_actions ⇒ Array<Zif::Actions::Action>
The actions this sequence runs through.
-
#sub_repeats ⇒ Array<Integer>
readonly
The number of times each Action in #sub_actions should repeat before moving on.
1. Public Interface collapse
-
#complete? ⇒ Boolean
Has this sequence run out of repeat iterations?.
-
#cur_action ⇒ Zif::Actions::Action
The current Action in this sequence.
-
#initialize(actions, repeat: 1, &block) ⇒ Sequence
constructor
A new instance of Sequence.
-
#restart ⇒ Object
Resets the state of the sequence back to the initialized state.
2. Private-ish methods collapse
-
#next_action ⇒ Object
private
Advances the sequence to the next action.
-
#perform_callback ⇒ Object
private
Calls the #callback on
self
. -
#perform_tick ⇒ Boolean
private
Calls the current action’s Action#perform_tick Conditionally advances to the next action and performs the callback.
-
#reset_cur_action_repeat ⇒ Object
private
Resets the current action’s Action#repeat to the value it was initialized with.
-
#setup_action ⇒ Object
private
Resets the current action’s parameters, so it is fresh and ready to run.
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.
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_index ⇒ Integer (readonly)
Returns 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 |
#callback ⇒ Proc
Returns 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_repeat ⇒ Integer
Returns The number of repeat iterations left.
21 22 23 |
# File 'lib/zif/actions/sequence.rb', line 21 def cur_repeat @cur_repeat end |
#repeat ⇒ Integer
Returns The number of times this Sequence should repeat.
18 19 20 |
# File 'lib/zif/actions/sequence.rb', line 18 def repeat @repeat end |
#sub_actions ⇒ Array<Zif::Actions::Action>
Returns The actions this sequence runs through.
15 16 17 |
# File 'lib/zif/actions/sequence.rb', line 15 def sub_actions @sub_actions end |
#sub_repeats ⇒ Array<Integer> (readonly)
Returns 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?.
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_action ⇒ Zif::Actions::Action
Returns the current Action in this sequence.
57 58 59 |
# File 'lib/zif/actions/sequence.rb', line 57 def cur_action @sub_actions[@action_index] end |
#next_action ⇒ Object
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_callback ⇒ Object
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_tick ⇒ Boolean
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.
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_repeat ⇒ Object
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 |
#restart ⇒ Object
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_action ⇒ Object
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 |