Class: RGame::Engine::AnimationSet

Inherits:
Object
  • Object
show all
Defined in:
lib/rgame/engine/animation_set.rb

Overview

Pure animation math: given an atlas's animation table and an elapsed time, resolve which sprite-sheet cell (row, col) to show. No renderer, no images — fully testable.

animations: { name => { row:, col:, frames:, fps:, flip_x: }, ... } row – sheet row col – starting column (default 0); lets an animation begin mid-row, e.g. a 1-frame "stand" pointing at the neutral middle pose frames – number of frames, advancing across columns from col fps – frames per second flip_x – draw mirrored horizontally

Defined Under Namespace

Classes: Anim

Instance Method Summary collapse

Constructor Details

#initialize(animations) ⇒ AnimationSet

Returns a new instance of AnimationSet.



19
20
21
22
23
# File 'lib/rgame/engine/animation_set.rb', line 19

def initialize(animations)
  @anims = animations.each_with_object({}) do |(name, a), table|
    table[name.to_sym] = Anim.new(a[:row], a[:col] || 0, a[:frames], 1.0 / a[:fps], a[:flip_x] || false)
  end
end

Instance Method Details

#col(name, elapsed) ⇒ Object

Sheet column of name at elapsed seconds — the only part that animates.



31
32
33
34
# File 'lib/rgame/engine/animation_set.rb', line 31

def col(name, elapsed)
  anim = @anims.fetch(name)
  anim.first_col + ((elapsed / anim.frame_secs).floor % anim.frames)
end

#flip_x(name) ⇒ Object

Whether name draws mirrored (fixed per animation).



37
38
39
# File 'lib/rgame/engine/animation_set.rb', line 37

def flip_x(name)
  @anims.fetch(name).flip_x
end

#frame(name, elapsed) ⇒ Object

[row, col, flip_x] for the given animation at elapsed seconds. A convenience for tests/inspection — it allocates an Array, so the per-frame draw path reads the row/col/flip_x accessors above directly instead.



44
45
46
# File 'lib/rgame/engine/animation_set.rb', line 44

def frame(name, elapsed)
  [row(name), col(name, elapsed), flip_x(name)]
end

#row(name) ⇒ Object

Sheet row of name (fixed per animation).



26
27
28
# File 'lib/rgame/engine/animation_set.rb', line 26

def row(name)
  @anims.fetch(name).row
end