Module: SpriteKit::Animation
- Defined in:
- mygame/lib/sprite_kit/animation.rb
Overview
The animation module is fairly opinionated. A typical sprite would look like this: sprite = { x: 0, y: 0, w: 0, h: 0, animations: { idle: { hold_for: 8, # fallback hold_for if hold_for not present on a frame. Default is 1. repeat: true, frames: [ 0, source_y: 0, source_h: 32, source_w: 32, path: "sprites/foo.png", 0, source_y: 0, source_h: 32, source_w: 32, hold_for: 4, path: "sprites/foo.png", ] } } } args.outputs.sprites << ::SpriteKit::Animation.animate!(sprite, frame.animations.idle)
Class Method Summary collapse
- .animate!(sprite, animation, start_at: 0, tick_count_override: Kernel.tick_count) ⇒ Object
- .current_frame(animation, start_at: 0, tick_count_override: Kernel.tick_count) ⇒ Object
- .frame_index(frames:, start_at: 0, repeat: false, hold_for: 1, tick_count_override: Kernel.tick_count) ⇒ Object
Class Method Details
.animate!(sprite, animation, start_at: 0, tick_count_override: Kernel.tick_count) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'mygame/lib/sprite_kit/animation.rb', line 21 def self.animate!(sprite, animation, start_at: 0, tick_count_override: Kernel.tick_count) frame = current_frame(animation, start_at: start_at, tick_count_override: tick_count_override) return if !frame sprite.source_x = frame.source_x sprite.source_y = frame.source_y sprite.source_w = frame.source_w sprite.source_h = frame.source_h sprite.path = frame.path end |
.current_frame(animation, start_at: 0, tick_count_override: Kernel.tick_count) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'mygame/lib/sprite_kit/animation.rb', line 70 def self.current_frame(animation, start_at: 0, tick_count_override: Kernel.tick_count) return if !animation frames = animation&.frames if frames.is_a?(Array) frame_index = self.frame_index( start_at: start_at, repeat: animation.repeat || false, hold_for: animation.hold_for || 1, tick_count_override: tick_count_override, frames: frames ) frames[frame_index] if frame_index else animation end end |
.frame_index(frames:, start_at: 0, repeat: false, hold_for: 1, tick_count_override: Kernel.tick_count) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'mygame/lib/sprite_kit/animation.rb', line 33 def self.frame_index(frames:, start_at: 0, repeat: false, # repeat_index: 0, hold_for: 1, tick_count_override: Kernel.tick_count ) tick_count = tick_count_override frame_index = nil held_frames = 0 frames.length.times do |index| frame = frames[index] held_frames += frame.hold_for || hold_for || 1 if start_at + held_frames > tick_count frame_index = index break end end if !frame_index && repeat total_duration = held_frames elapsed = (tick_count - start_at) % total_duration held_frames = 0 frames.length.times do |index| frame = frames[index] held_frames += frame.hold_for || hold_for || 1 if held_frames > elapsed frame_index = index break end end end frame_index end |