Module: Zif::Actions::Animatable
- Included in:
- Sprite
- Defined in:
- lib/zif/actions/animatable.rb
Overview
A mixin to assist with sprite animations, to use with an object which already includes Actionable. Under the hood, these are implemented as Sequences which modify the path
over time.
Instance Attribute Summary collapse
-
#animation_sequences ⇒ Hash<(String, Symbol), Zif::Actions::Sequence>
readonly
Registered sequences by name.
-
#cur_animation ⇒ String, Symbol
readonly
Name of last run animation sequence.
1. Public Interface collapse
-
#new_basic_animation(named:, paths_and_durations:, repeat: :forever, &block) ⇒ Object
Creates and registers a new Sequence named
named
to animate across thepaths_and_durations
array. -
#new_tiled_animation(named:, path:, width:, height:, durations:, repeat: :forever, &block) ⇒ Object
Similar to
new_basic_animation
, but for a tiled animation. -
#register_animation_sequence(named:, sequence:) ⇒ Object
Manually register an animation sequence.
-
#run_animation_sequence(name) ⇒ Object
Run an animation sequence which has been previously registered.
-
#stop_animating ⇒ Object
Stop the current animation.
Instance Attribute Details
#animation_sequences ⇒ Hash<(String, Symbol), Zif::Actions::Sequence> (readonly)
Returns Registered sequences by name.
7 8 9 |
# File 'lib/zif/actions/animatable.rb', line 7 def animation_sequences @animation_sequences end |
#cur_animation ⇒ String, Symbol (readonly)
Returns Name of last run animation sequence.
10 11 12 |
# File 'lib/zif/actions/animatable.rb', line 10 def cur_animation @cur_animation end |
Instance Method Details
#new_basic_animation(named:, paths_and_durations:, repeat: :forever, &block) ⇒ Object
Creates and registers a new Sequence named named
to animate across the paths_and_durations
array
45 46 47 48 49 50 |
# File 'lib/zif/actions/animatable.rb', line 45 def new_basic_animation(named:, paths_and_durations:, repeat: :forever, &block) actions = paths_and_durations.map do |(path, duration)| new_action({path: "sprites/#{path}.png"}, duration: duration, easing: :immediate, rounding: :none) end register_animation_sequence(named: named, sequence: Sequence.new(actions, repeat: repeat, &block)) end |
#new_tiled_animation(named:, path:, width:, height:, durations:, repeat: :forever, &block) ⇒ Object
Similar to new_basic_animation
, but for a tiled animation. Use this function when you have a single image with multiple tiles, and you want to animate across them, rather than a separate image per animation frame.
This helper assumes that the spritesheet image is laid out in a single row of tiles, each of the same size and with no spacing/padding between them.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/zif/actions/animatable.rb', line 68 def new_tiled_animation(named:, path:, width:, height:, durations:, repeat: :forever, &block) actions = durations.map_with_index do |duration, tile_index| new_action( { path: "sprites/#{path}.png", source_x: 0 + (tile_index * width), source_y: 0, source_w: width, source_h: height }, duration: duration, easing: :immediate, rounding: :none ) end register_animation_sequence(named: named, sequence: Sequence.new(actions, repeat: repeat, &block)) end |
#register_animation_sequence(named:, sequence:) ⇒ Object
Manually register an animation sequence. If you need more control over your Sequence than #new_basic_animation provides, you can create it manually, and then register it here.
91 92 93 94 95 96 |
# File 'lib/zif/actions/animatable.rb', line 91 def register_animation_sequence(named:, sequence:) # puts "Registering animation #{named} with repeat #{sequence.repeat}" @animation_sequences ||= {} @animation_sequences[named] = sequence end |
#run_animation_sequence(name) ⇒ Object
Run an animation sequence which has been previously registered. Since animations are mutually exclusive, this will #stop_animating any previously running animation. It will also reset the progress of the current action on the invoked sequence.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/zif/actions/animatable.rb', line 103 def run_animation_sequence(name) raise ArgumentError, "No animation sequence named '#{name}' registered" unless @animation_sequences[name] stop_animating @cur_animation = name @animation_sequences[@cur_animation].restart @animation_sequences[@cur_animation].setup_action # puts "Running animation sequence #{@cur_animation} #{@animation_sequences[@cur_animation].inspect}" run_action(@animation_sequences[@cur_animation]) end |
#stop_animating ⇒ Object
Stop the current animation.
117 118 119 120 121 122 |
# File 'lib/zif/actions/animatable.rb', line 117 def stop_animating return unless @cur_animation && @animation_sequences && @animation_sequences[@cur_animation] # puts "Stopping animation #{@cur_animation} -> #{@animation_sequences[@cur_animation]}" stop_action(@animation_sequences[@cur_animation]) end |