-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimationDynamicClass.lua
More file actions
100 lines (83 loc) · 3.41 KB
/
animationDynamicClass.lua
File metadata and controls
100 lines (83 loc) · 3.41 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
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
require("dynamicClass")
Animation = Dynamic:new()
function Animation:new(id, x, y, shape, width, height, baseSpeed, maxSpeed, angle, action, obstacles)
local o = Dynamic:new(id, x, y, shape, width, height, baseSpeed, maxSpeed, angle, action, obstacles)
setmetatable(o, self)
self.__index = self
o.animation = nil
o.animations = {}
o.isStatic = false
o.rotation = 0
o.scale = 1
return o
end
function Animation:setScale(scale)
self.scale = scale
end
function Animation:addAnimation(image, width, height, duration)
local animation = {}
animation.spiteSheet = image
animation.quads = {}
animation.duration = duration or 1
animation.currentTime = 0
for y=0, image:getHeight()-height, height do
for x=0, image:getWidth()-width, width do
table.insert(animation.quads, love.graphics.newQuad(x, y, width, height, image:getDimensions()))
end
end
table.insert(self.animations, animation)
end
function Animation:addStatic(image, width, height)
self.isStatic = true
local animation = {}
animation.spiteSheet = image
table.insert(self.animations, animation)
end
-- function Animation:updateAnimation(self, dt) -- this works too, but it's not obvious to use explicit self with ":" function call
function Animation.updateAnimation(self, dt)
if self.animations then
if self.direction == "right" and #self.animations > 0 then
self.animation = self.animations[1]
elseif self.direction == "left" and #self.animations > 0 then
self.animation = self.animations[2]
elseif #self.animations > 0 then
self.animation = self.animations[1]
end
if self.animation and dt then
self.animation.currentTime = self.animation.currentTime + dt
if self.animation.currentTime >= self.animation.duration then
self.animation.currentTime = self.animation.currentTime - self.animation.duration
end
end
end
end
function Animation:setAnimation(index)
self.animation = self.animations[index]
end
function Animation:update(dt, obstacles, direction)
callbacks = {}
directionChangeCallback = function(s, t)
Animation.updateAnimation(s, t)
end
callbacks["left"] = directionChangeCallback
callbacks["right"] = directionChangeCallback
Dynamic.update(self, dt, obstacles, direction, callbacks)
end
function Animation:draw(isAnimate)
if self.animation and not self.isStatic then
if isAnimate and not self.isJump then
spriteNum = math.floor(self.animation.currentTime/self.animation.duration * #self.animation.quads) + 1
love.graphics.draw(self.animation.spiteSheet, self.animation.quads[spriteNum], self.x, self.y)
elseif isAnimate then
love.graphics.draw(self.animation.spiteSheet, self.animation.quads[1], self.x, self.y)
elseif isAnimate == false then -- should be explicit "false" otherwise there are some frames when it's nil
love.graphics.draw(self.animation.spiteSheet, self.animation.quads[1], self.x, self.y)
end
elseif self.animation then
love.graphics.draw(self.animation.spiteSheet, self.x, self.y, self.rotation, self.scale)
else
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end
-- debug
-- love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end