-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
sprite.lua
130 lines (107 loc) · 2.92 KB
/
sprite.lua
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
--=========== Copyright © 2019, Planimeter, All rights reserved. ===========--
--
-- Purpose: Sprite class
--
--==========================================================================--
class( "sprite" )
function sprite:sprite( spriteSheet )
local data = require( spriteSheet )
local image = love.graphics.newImage( data[ "image" ] )
self.spriteSheet = image
self.width = data[ "width" ]
self.height = data[ "height" ]
self.frametime = data[ "frametime" ]
self.animations = data[ "animations" ] or {}
self.events = data[ "events" ] or {}
self.curtime = 0
self.frame = 1
end
function sprite:draw()
local image = self:getSpriteSheet()
love.graphics.draw( image, self:getQuad() )
end
accessor( sprite, "animation" )
accessor( sprite, "animationName" )
accessor( sprite, "animations" )
accessor( sprite, "events" )
accessor( sprite, "frametime" )
function sprite:setFilter( ... )
local image = self:getSpriteSheet()
image:setFilter( ... )
end
function sprite:getQuad()
if ( self.quad == nil ) then
local image = self:getSpriteSheet()
self.quad = love.graphics.newQuad(
0,
0,
self:getWidth(),
self:getHeight(),
image:getWidth(),
image:getHeight()
)
end
return self.quad
end
accessor( sprite, "spriteSheet" )
accessor( sprite, "width" )
accessor( sprite, "height" )
function sprite:onAnimationEnd( animation )
end
function sprite:onAnimationEvent( event )
end
function sprite:setAnimation( animation )
local animations = self:getAnimations()
local name = animation
animation = animations[ name ]
if ( animation == nil ) then
return
end
if ( animation == self:getAnimation() ) then
return
end
self.animation = animation
self.animationName = name
self.frame = animation.from
self:updateFrame()
end
function sprite:update( dt )
local animation = self:getAnimation()
if ( animation == nil ) then
return
end
self.curtime = self.curtime + dt
if ( self.curtime >= self.frametime ) then
self.curtime = 0
self.frame = self.frame + 1
if ( self.frame > animation.to ) then
local name = self:getAnimationName()
self:onAnimationEnd( name )
self.frame = animation.from
end
self:updateFrame()
end
end
function sprite:updateFrame()
local quad = self:getQuad()
local frame = self.frame - 1
local width = self:getWidth()
local height = self:getHeight()
local image = self:getSpriteSheet()
local imageWidth = image:getWidth()
local x = frame * width % imageWidth
local y = math.floor( frame * width / imageWidth ) * height
quad:setViewport( x, y, width, height )
local events = self:getEvents()
local event = events[ frame ]
if ( event ) then
self:onAnimationEvent( event )
end
end
function sprite:__tostring()
local t = getmetatable( self )
setmetatable( self, {} )
local s = string.gsub( tostring( self ), "table", "sprite" )
setmetatable( self, t )
return s
end