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

1. Public Interface collapse

2. Example Factories - Use these as inspiration for creating custom {Zif::Actions::Action}s collapse

3. Private-ish methods collapse

Instance Attribute Details

#actionsArray<Zif::Actions::Action, Zif::Actions::Sequence> (readonly)

Returns The list of running Zif::Actions::Actions and Sequences.

Returns:



8
9
10
# File 'lib/zif/actions/actionable.rb', line 8

def actions
  @actions
end

#dirtyBoolean (readonly)

Returns Did any of the running actions change this object this tick?.

Returns:

  • (Boolean)

    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.

Parameters:

  • y (Integer) (defaults to: 110)

    The center Y position to bounce around

  • distance (Integer) (defaults to: 15)

    The distance from y it will travel above and below

  • duration (Integer) (defaults to: 5.seconds)

    Number of ticks it will take to travel to the top or bottom

Returns:



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.

Parameters:



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

Parameters:

Returns:

See Also:



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

Parameters:

Returns:

See Also:



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

Example factory Returns a new Sequence which combines #fade_in and #fade_out forever

Parameters:

  • duration (Integer) (defaults to: 1.seconds)

    Number of ticks it takes to fade in or out fully

Returns:



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

Returns:

See Also:



43
44
45
# File 'lib/zif/actions/actionable.rb', line 43

def new_action(*args, &block)
  Action.new(self, *args, &block)
end

#perform_actionsObject

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.

Parameters:

Raises:

  • (ArgumentError)


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?.

Returns:

  • (Boolean)

    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.

Parameters:



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