Module: Zif::Actions::Actionable
- Included in:
- Layers::Camera, Sprite, UI::Label
- Defined in:
- lib/zif/actions/actionable.rb
Overview
Inspired by developer.apple.com/documentation/spritekit/skaction
A mixin to facilitate Actions and Sequences running on this object
Instance Attribute Summary collapse
- #actions ⇒ Array<Zif::Actions::Action, Zif::Actions::Sequence> readonly
-
#dirty ⇒ Boolean
readonly
Did any of the running actions change this object this tick?.
1. Public Interface collapse
-
#delayed_action(wait, repeat: 1, &block) ⇒ Object
Create an no-op Action targeting
self
- just a wait and a callback. -
#new_action(*args, &block) ⇒ Zif::Actions::Action
A convenience factory for a new Action targeting
self
as thenode
. -
#run_action(action) ⇒ Object
Add an action to the list of actions to run.
-
#running_actions? ⇒ Boolean
Are any actions running?.
-
#stop_action(action) ⇒ Object
Stop a running action.
2. Example Factories - Use these as inspiration for creating custom {Zif::Actions::Action}s collapse
-
#bounce_forever_around(y: 110, distance: 15, duration: 5.seconds) ⇒ Zif::Actions::Sequence
Example factory Returns a new Sequence which eases the sprite up and down around
y
at somedistance
overduration
. -
#fade_in(duration = 3.seconds, &block) ⇒ Zif::Actions::Action
Example factory Returns a new Action which eases the sprite’s
a
alpha to255
overduration
. -
#fade_out(duration = 3.seconds, &block) ⇒ Zif::Actions::Action
Example factory Returns a new Action which eases the sprite’s
a
alpha to0
overduration
. - #fade_out_and_in_forever(duration = 1.seconds) ⇒ Zif::Actions::Sequence
3. Private-ish methods collapse
- #perform_actions ⇒ Object private
Instance Attribute Details
#actions ⇒ Array<Zif::Actions::Action, Zif::Actions::Sequence> (readonly)
Returns The list of running Zif::Actions::Actions and Sequences.
8 9 10 |
# File 'lib/zif/actions/actionable.rb', line 8 def actions @actions end |
#dirty ⇒ Boolean (readonly)
Returns Did any of the running actions change this object this tick?.
11 12 13 |
# File 'lib/zif/actions/actionable.rb', line 11 def dirty @dirty end |
Instance Method Details
#bounce_forever_around(y: 110, distance: 15, duration: 5.seconds) ⇒ Zif::Actions::Sequence
Example factory Returns a new Sequence which eases the sprite up and down around y
at some distance
over duration
. Repeats forever.
64 65 66 67 68 69 70 71 72 |
# File 'lib/zif/actions/actionable.rb', line 64 def bounce_forever_around(y: 110, distance: 15, duration: 5.seconds) Sequence.new( [ new_action({y: y - distance}, duration: duration, easing: :smooth_step), new_action({y: y + distance}, duration: duration, easing: :smooth_step) ], repeat: :forever ) end |
#delayed_action(wait, repeat: 1, &block) ⇒ Object
Create an no-op Zif::Actions::Action targeting self
- just a wait and a callback.
50 51 52 |
# File 'lib/zif/actions/actionable.rb', line 50 def delayed_action(wait, repeat: 1, &block) new_action({}, duration: wait, easing: :linear, rounding: :round, repeat: repeat, &block) end |
#fade_in(duration = 3.seconds, &block) ⇒ Zif::Actions::Action
Example factory Returns a new Zif::Actions::Action which eases the sprite’s a
alpha to 255
over duration
98 99 100 |
# File 'lib/zif/actions/actionable.rb', line 98 def fade_in(duration=3.seconds, &block) new_action({a: 255}, duration: duration, &block) end |
#fade_out(duration = 3.seconds, &block) ⇒ Zif::Actions::Action
Example factory Returns a new Zif::Actions::Action which eases the sprite’s a
alpha to 0
over duration
88 89 90 |
# File 'lib/zif/actions/actionable.rb', line 88 def fade_out(duration=3.seconds, &block) new_action({a: 0}, duration: duration, &block) end |
#fade_out_and_in_forever(duration = 1.seconds) ⇒ Zif::Actions::Sequence
78 79 80 |
# File 'lib/zif/actions/actionable.rb', line 78 def fade_out_and_in_forever(duration=1.seconds) Sequence.new([fade_out(duration), fade_in(duration)], repeat: :forever) end |
#new_action(*args, &block) ⇒ Zif::Actions::Action
A convenience factory for a new Zif::Actions::Action targeting self
as the node
. Sends other params to Zif::Actions::Action#initialize
43 44 45 |
# File 'lib/zif/actions/actionable.rb', line 43 def new_action(*args, &block) Action.new(self, *args, &block) end |
#perform_actions ⇒ 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.
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/zif/actions/actionable.rb', line 106 def perform_actions @dirty = false @actions ||= [] @actions.each do |act| dirty_act = act.perform_tick @dirty ||= dirty_act end @actions.reject!(&:complete?) end |
#run_action(action) ⇒ Object
Add an action to the list of actions to run.
18 19 20 21 22 23 |
# File 'lib/zif/actions/actionable.rb', line 18 def run_action(action) raise ArgumentError, "Invalid action: #{action}" unless action.is_a?(Action) || action.is_a?(Sequence) @actions ||= [] @actions << action end |
#running_actions? ⇒ Boolean
Returns Are any actions running?.
33 34 35 36 |
# File 'lib/zif/actions/actionable.rb', line 33 def running_actions? @actions ||= [] @actions.any? end |
#stop_action(action) ⇒ Object
Stop a running action.
27 28 29 30 |
# File 'lib/zif/actions/actionable.rb', line 27 def stop_action(action) # puts "Stopping action #{action}: #{@actions}" @actions&.delete(action) end |