Class: RichEngine::Animation

Inherits:
Object
  • Object
show all
Defined in:
lib/rich_engine/animation.rb

Overview

Plays a sequence of string frames (sprites) at a fixed frames-per-second.

Examples:

frames = [
  "A",
  "B"
]
anim = RichEngine::Animation.new(frames: frames, fps: 8, loop: true)

# Inside your Game loop:
anim.update(elapsed_time)
anim.draw(@canvas, x: 10, y: 2, fg: :yellow)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(frames:, fps: 12, loop: true, fg: :white) ⇒ Animation

Builds an animation from a sequence of string frames.

Examples:

anim = RichEngine::Animation.new(frames: ["A", "B"], fps: 8, loop: true)

Parameters:

  • frames (Array<String>)

    each string is a frame; can be multi-line. Spaces are transparent.

  • fps (Integer, Float) (defaults to: 12)

    how many frames per second to advance.

  • loop (Boolean) (defaults to: true)

    if true, wrap to the first frame after the last; otherwise stop at the last frame.

  • fg (Symbol, String, Array<Integer>, Integer) (defaults to: :white)

    default foreground color when drawing.



37
38
39
40
41
42
43
44
45
# File 'lib/rich_engine/animation.rb', line 37

def initialize(frames:, fps: 12, loop: true, fg: :white)
  @frames = frames
  @fps = fps
  @loop = loop
  @fg = fg
  @frame_index = 0
  @playing = true
  @stepper = RichEngine::Timer.every(seconds: frame_interval)
end

Instance Attribute Details

#fgSymbol, ...

Returns default foreground color used when drawing.

Returns:

  • (Symbol, String, Array<Integer>, Integer)

    default foreground color used when drawing.



24
25
26
# File 'lib/rich_engine/animation.rb', line 24

def fg
  @fg
end

#fpsArray<String>, ...

Returns:

  • (Array<String>)

    the frame sprites, in playback order.

  • (Integer)

    the index of the frame currently shown.

  • (Integer, Float)

    the configured frames per second.



20
21
22
# File 'lib/rich_engine/animation.rb', line 20

def fps
  @fps
end

#frame_indexArray<String>, ... (readonly)

Returns:

  • (Array<String>)

    the frame sprites, in playback order.

  • (Integer)

    the index of the frame currently shown.

  • (Integer, Float)

    the configured frames per second.



20
21
22
# File 'lib/rich_engine/animation.rb', line 20

def frame_index
  @frame_index
end

#framesArray<String>, ... (readonly)

Returns:

  • (Array<String>)

    the frame sprites, in playback order.

  • (Integer)

    the index of the frame currently shown.

  • (Integer, Float)

    the configured frames per second.



20
21
22
# File 'lib/rich_engine/animation.rb', line 20

def frames
  @frames
end

Instance Method Details

#current_frameString

Returns the frame string currently shown.

Returns:

  • (String)

    the frame string currently shown.



93
94
95
# File 'lib/rich_engine/animation.rb', line 93

def current_frame
  @frames[@frame_index]
end

#draw(canvas, x:, y:, fg: nil) ⇒ void

This method returns an undefined value.

Renders the current frame to the canvas.

Parameters:

  • canvas (RichEngine::Canvas)

    the canvas to draw on.

  • x (Integer)

    left column to draw at.

  • y (Integer)

    top row to draw at.

  • fg (Symbol, String, Array<Integer>, Integer, nil) (defaults to: nil)

    foreground color; falls back to the animation's default when nil.



117
118
119
# File 'lib/rich_engine/animation.rb', line 117

def draw(canvas, x:, y:, fg: nil)
  canvas.draw_sprite(current_frame, x: x, y: y, fg: fg || @fg)
end

#loop?Boolean

Returns whether the animation wraps after the last frame.

Returns:

  • (Boolean)

    whether the animation wraps after the last frame.



57
# File 'lib/rich_engine/animation.rb', line 57

def loop? = @loop

#pause!void

This method returns an undefined value.

Pauses playback on the current frame.



73
74
75
# File 'lib/rich_engine/animation.rb', line 73

def pause!
  @playing = false
end

#play!void

This method returns an undefined value.

Resumes playback.



80
81
82
# File 'lib/rich_engine/animation.rb', line 80

def play!
  @playing = true
end

#playing?Boolean

Returns whether the animation is currently advancing frames.

Returns:

  • (Boolean)

    whether the animation is currently advancing frames.



60
# File 'lib/rich_engine/animation.rb', line 60

def playing? = @playing

#reset!void

This method returns an undefined value.

Rewinds to the first frame and restarts the frame timer.



87
88
89
90
# File 'lib/rich_engine/animation.rb', line 87

def reset!
  @frame_index = 0
  @stepper.interval = frame_interval
end

#stop!void

This method returns an undefined value.

Stops playback and rewinds to the first frame.



65
66
67
68
# File 'lib/rich_engine/animation.rb', line 65

def stop!
  @playing = false
  reset!
end

#update(elapsed_time) ⇒ void

This method returns an undefined value.

Advances the internal timer; call once per frame with the elapsed time.

Parameters:

  • elapsed_time (Float)

    seconds since the last frame.



101
102
103
104
105
106
107
# File 'lib/rich_engine/animation.rb', line 101

def update(elapsed_time)
  return unless @playing
  return if @frames.size <= 1

  @stepper.update(elapsed_time)
  @stepper.when_ready { advance_frame }
end