-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprite.lua
141 lines (116 loc) · 3.06 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
131
132
133
134
135
136
137
138
139
140
141
--sprite class
Sprite = {
r = 0,
alpha = 1,
flipped = false,
visible = true,
user = "",
drawmode = "relative",
depth = 5
}
local sprite_mt = class(Sprite)
function Sprite:new(x, y, image)
local o = Sprite:init(x, y, image)
return setmetatable(o, sprite_mt)
end
function Sprite:init(x, y, image)
local o = {
x = x, y = y,
image = images[image]
}
o.width = o.image:getWidth()
o.height = o.image:getHeight()
return o
end
function Sprite:add(x, y, image)
table.insert(p, Sprite:new(x, y, image))
end
function Sprite:update(dt)
end
function Sprite:draw()
if self.visible then
love.graphics.draw(
self.image, self.x, self.y, self.r,
(self.flipped and -1 or (not self.flipped and 1))
)
end
end
function Sprite:translate()
return { x = self.x - camera.x, y = self.y - camera.y }
end
--animatedsprite class
AnimatedSprite = inherits(Sprite, {
frame = 1,
framedelta = 0,
framerate = 12
})
local animatedsprite_mt = class(AnimatedSprite)
--the skins param should be a sequence of strings referring to animation objects, or a single string
function AnimatedSprite:init(x, y, skins)
local o = {
x = x, y = y,
skins = {}
}
if type(skins) == "string" then
o.skins = { animations[skins] }
elseif type(skins) == "table" then
for i,_ in ipairs(skins) do
o.skins[i] = animations[skins[i]]
end
else
error("skins parameter is not a string or table of strings")
end
o.anim = o.skins[1]
o.width = o.anim.image:getWidth() / o.anim.frames
o.height = o.anim.image:getHeight()
return o
end
function AnimatedSprite:new(x, y, skins)
local o = AnimatedSprite:init(x, y, skins)
return setmetatable(o, animatedsprite_mt)
end
function AnimatedSprite:add(x, y, skins)
table.insert(p, AnimatedSprite:new(x, y, skins))
end
function AnimatedSprite:update(dt)
self:animate(dt)
end
function AnimatedSprite:animate(dt)
self.framedelta = self.framedelta + dt
if self.framedelta > 1 / self.framerate then
self.framedelta = self.framedelta - 1 / self.framerate
self:advanceframe()
end
end
function AnimatedSprite:advanceframe()
self.frame = self.frame + 1
if self.frame > self.anim.frames then
self.frame = 1
end
end
function AnimatedSprite:resetanimation()
self.frame = 1
self.framedelta = 0
end
function AnimatedSprite:swapanimation(anim)
if self.anim ~= anim then
self:resetanimation()
self.anim = anim
end
end
function AnimatedSprite:drawselfanim()
if self.visible then
local xoff = self.flipped and self.width or (not self.flipped and 0)
love.graphics.draw(
self.anim.image, self.anim.quads[self.frame],
self.x + xoff, self.y, self.r,
(self.flipped and -1 or (not self.flipped and 1)), 1
)
end
end
function AnimatedSprite:draw()
self:drawselfanim()
if self.bounds ~= nil and showdebug then
self.bounds:draw()
end
end