Class: Bestguigui::Animation
- Inherits:
-
Object
- Object
- Bestguigui::Animation
- Defined in:
- lib/bestguigui/animation.rb
Overview
Fait défiler une liste de frames (indices, images, whatever) au rythme de frame_duration secondes chacune. Ne charge rien elle-même — utilise Gosu::Image.load_tiles pour charger la feuille de sprites, et donne les frames obtenues (ou leurs indices) à Animation pour le timing.
@sheet = Gosu::Image.load_tiles("gfx/player.png", 128, 128, retro: true)
@walk = Bestguigui::Animation.new([3, 4], frame_duration: 0.15)
def update(dt)
@walk.update(dt)
end
def draw
@sheet[@walk.current].draw(x, y, 0)
end
Avec loop: false, l'animation s'arrête sur la dernière frame et #finished? devient true (utile pour une animation d'attaque jouée une fois).
Instance Method Summary collapse
- #current ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(frames, frame_duration:, loop: true) ⇒ Animation
constructor
A new instance of Animation.
- #reset ⇒ Object
- #update(dt) ⇒ Object
Constructor Details
#initialize(frames, frame_duration:, loop: true) ⇒ Animation
Returns a new instance of Animation.
22 23 24 25 26 27 28 29 |
# File 'lib/bestguigui/animation.rb', line 22 def initialize(frames, frame_duration:, loop: true) @frames = frames @frame_duration = frame_duration @loop = loop @index = 0 @elapsed = 0 @finished = false end |
Instance Method Details
#current ⇒ Object
41 42 43 |
# File 'lib/bestguigui/animation.rb', line 41 def current @frames[@index] end |
#finished? ⇒ Boolean
45 46 47 |
# File 'lib/bestguigui/animation.rb', line 45 def finished? @finished end |
#reset ⇒ Object
49 50 51 52 53 |
# File 'lib/bestguigui/animation.rb', line 49 def reset @index = 0 @elapsed = 0 @finished = false end |
#update(dt) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/bestguigui/animation.rb', line 31 def update(dt) return if @finished @elapsed += dt while @elapsed >= @frame_duration @elapsed -= @frame_duration advance end end |