Class: Bestguigui::Animation

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

Overview

Anime les frames d'un spritesheet selon un cycle d'indices donne (ex. cycle: [0, 1, 2, 1] pour un aller-retour). Sans cycle, joue les frames dans l'ordre du sheet.

walk = Bestguigui::Animation.new('hero.png', 32, 32, cycle: [0, 1, 2, 1], frame_duration: 120)
walk.update(dt)
walk.draw(x, y)

Instance Method Summary collapse

Constructor Details

#initialize(path, frame_width, frame_height, frame_duration:, cycle: nil, loop: true) ⇒ Animation

rubocop:disable Metrics/ParameterLists -- reprend la signature de Gosu::Image#draw



13
14
15
16
17
18
19
# File 'lib/bestguigui_lab/animation.rb', line 13

def initialize(path, frame_width, frame_height, frame_duration:, cycle: nil, loop: true)
    @frames = Assets.frames(path, frame_width, frame_height)
    @cycle = cycle || (0...@frames.size).to_a
    @frame_duration = frame_duration
    @loop = loop
    @elapsed = 0
end

Instance Method Details

#draw(x, y, z = 0, scale_x = 1, scale_y = 1, color = Gosu::Color::WHITE, mode = :default) ⇒ Object



27
28
29
# File 'lib/bestguigui_lab/animation.rb', line 27

def draw(x, y, z = 0, scale_x = 1, scale_y = 1, color = Gosu::Color::WHITE, mode = :default)
    frame.draw(x, y, z, scale_x, scale_y, color, mode)
end

#finished? ⇒ Boolean

rubocop:enable Metrics/ParameterLists

Returns:

  • (Boolean)


32
# File 'lib/bestguigui_lab/animation.rb', line 32

def finished? = !@loop && raw_step >= @cycle.size

#frame ⇒ Object



25
# File 'lib/bestguigui_lab/animation.rb', line 25

def frame = @frames[@cycle[current_step]]

#reset ⇒ Object



34
35
36
# File 'lib/bestguigui_lab/animation.rb', line 34

def reset
    @elapsed = 0
end

#update(dt) ⇒ Object



21
22
23
# File 'lib/bestguigui_lab/animation.rb', line 21

def update(dt)
    @elapsed += dt unless finished?
end