-
Notifications
You must be signed in to change notification settings - Fork 17
/
animatedtimer.lua
87 lines (72 loc) · 1.8 KB
/
animatedtimer.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
animatedtimer = class("animatedtimer")
animatedtimerlist = {}
function animatedtimer:init(x, y, tileno)
self.x = x
self.y = y
self.quadi = 1
self.timer = 0
self.quadobj = tilequads[tileno]
self.delays = self.quadobj.delays
self.frametimes = {}
self.length = 0
for i = 1, #self.delays do
self.length = self.length + self.delays[i]
self.frametimes[i] = self.length
end
self.dir = 0
table.insert(animatedtimerlist, self)
end
function animatedtimer:update(dt)
local oldi = self:geti()
self.timer = self.timer + dt*self.dir
if self.timer > self.length then
self.timer = self.length
self.dir = 0
elseif self.timer < 0 then
self.timer = 0
self.dir = 0
end
local newi = self:geti()
if oldi ~= newi then
local oldcol = self.quadobj.properties[oldi].collision
local oldportalable = self.quadobj.properties[oldi].portalable
local props = self.quadobj.properties[newi]
if oldcol ~= props.collision then
if props.collision then
objects["tile"][self.x .. "-" .. self.y] = tile:new(self.x-1, self.y-1)
else
objects["tile"][self.x .. "-" .. self.y] = nil
checkportalremove(self.x, self.y)
end
end
if oldportalable ~= props.portalable then
if not props.portalable then
checkportalremove(self.x, self.y)
end
end
end
end
function animatedtimer:input(t)
if t == "on" then
self.dir = 1
elseif t == "off" then
self.dir = -1
elseif t == "toggle" then
self.dir = -self.dir
if self.dir == 0 then
if self.timer == 0 then
self.dir = 1
else
self.dir = -1
end
end
end
end
function animatedtimer:geti()
for i = 2, #self.frametimes do
if self.timer > self.frametimes[i-1] and self.timer <= self.frametimes[i] then
return i
end
end
return 1
end