-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprite_utils.lua
More file actions
52 lines (43 loc) · 1.66 KB
/
Copy pathsprite_utils.lua
File metadata and controls
52 lines (43 loc) · 1.66 KB
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
function newAnimation(image, width, height, duration)
local animation = {}
animation.spriteSheet = newSpriteSheet(image, width, height);
animation.duration = duration or 1
animation.currentTime = 0
animation.cycleCount = 0
return animation
end
function resetAnimation(animation)
animation.currentTime = 0
animation.cycleCount = 0
end
function updateAnimation(animation, dt)
animation.currentTime = animation.currentTime + dt
if animation.currentTime >= animation.duration then
animation.currentTime = animation.currentTime - animation.duration
animation.cycleCount = animation.cycleCount + 1
end
end
function drawAnimation(animation, x, y, r, sx, sy, ox, oy)
local spriteNum = math.floor(animation.currentTime / animation.duration * #animation.spriteSheet.quads) + 1
love.graphics.draw(animation.spriteSheet.image, animation.spriteSheet.quads[spriteNum], x, y, r, sx, sy, ox, oy)
end
function drawSprite(spriteSheet, quadsIndex, x, y, r, sx, sr, ox, oy)
love.graphics.draw(spriteSheet.image, spriteSheet.quads[quadsIndex], x, y, r, sx, sr, ox, oy)
end
function newSpriteSheet(image, width, height, padding)
local spriteSheet = {}
spriteSheet.image = image;
spriteSheet.quads = {};
local xStride = width
local yStride = height
if padding ~= nil then
xStride = xStride + padding
yStride = yStride + padding
end
for y = 0, image:getHeight() - height, yStride do
for x = 0, image:getWidth() - width, xStride do
table.insert(spriteSheet.quads, love.graphics.newQuad(x, y, width, height, image:getDimensions()))
end
end
return spriteSheet
end