-
Notifications
You must be signed in to change notification settings - Fork 0
/
spriteanimation.rb
50 lines (40 loc) · 1.02 KB
/
spriteanimation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class SpriteAnimation
attr_accessor :x, :y
def initialize( h )
@window, imagefile, self.x, self.y, @z, width, height, @speed, @stopped_playing_callback, @loop = h.values_at( :window, :imagefile, :x, :y, :z, :width, :height, :speed, :stopped_playing, :loop)
@sprite = Gosu::Image.load_tiles(@window, imagefile, width, height, false)
@playing = false
@last_change = 0
@current_frame = 0
end
def playing?
@playing
end
def play
@playing = true
end
def stop
@playing = false
@stopped_playing_callback.call()
end
def draw
if (Gosu::milliseconds - @last_change) > @speed then
@last_change = Gosu::milliseconds
@current_frame += 1
end
if !@loop then
if @current_frame < @sprite.length then
@sprite[@current_frame].draw(x, y, @z)
else
stop
end
else
@current_frame = @current_frame % @sprite.length
@sprite[@current_frame].draw(x, y, @z)
end
end
def setXY (x, y)
self.x = x
self.y = y
end
end