-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtick.lua
More file actions
166 lines (136 loc) · 3.39 KB
/
Copy pathtick.lua
File metadata and controls
166 lines (136 loc) · 3.39 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
--
-- tick
--
-- Copyright (c) 2015 rxi
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.
--
local tick = { _version = "0.1.1" }
tick.__index = tick
local iscallable = function(x)
if type(x) == "function" then return true end
local mt = getmetatable(x)
return mt and mt.__call ~= nil
end
local noop = function()
end
local event = {}
event.__index = event
function event.new(parent, fn, delay, recur, err)
err = err or 0
-- Create and return event
return setmetatable({
parent = parent,
delay = delay,
timer = delay + err,
fn = fn,
recur = recur,
}, event)
end
function event:after(fn, delay)
-- Error check
if self.recur then
error("cannot chain a recurring event")
end
-- Chain event
local oldfn = self.fn
local e = event.new(self.parent, fn, delay, false)
self.fn = function()
oldfn()
e.timer = e.timer + self.parent.err
self.parent:add(e)
end
return e
end
function event:stop()
tick.remove(self.parent, self)
end
function tick.group()
return setmetatable({ err = 0 }, tick)
end
function tick:add(e)
self[e] = true
table.insert(self, e)
return e
end
function tick:remove(e)
if type(e) == "number" then
-- Remove and return event
local idx = e
e = self[idx]
self[e] = nil
self[idx] = self[#self]
table.remove(self)
return e
end
self[e] = false
for i, v in ipairs(self) do
if v == e then
return self:remove(i)
end
end
end
function tick:update(dt)
for i = #self, 1, -1 do
local e = self[i]
e.timer = e.timer - dt
while e.timer <= 0 do
if e.recur then
e.timer = e.timer + e.delay
else
self:remove(i)
end
self.err = e.timer
e.fn()
if not e.recur then
break
end
end
end
self.err = 0
end
function tick:event(fn, delay, recur)
delay = tonumber(delay)
-- Error check
if not iscallable(fn) then
error("expected `fn` to be callable")
end
if type(delay) ~= "number" then
error("expected `delay` to be a number")
end
if delay < 0 then
error("expected `delay` of zero or greater")
end
-- If, factoring in the timing error, the event should happen *now* the
-- function is immediately called and the error is temporarily carried
-- through. This assures nested events with delays shorter than the update()
-- delta-time do not accumulate error; several nested events with very small
-- delays may end up being called on the same frame. A dummy event is created
-- and returned so :after() still functions correctly.
local d = delay + self.err
if d < 0 then
local err = self.err
self.err = d
fn()
self.err = err
return self:add(event.new(self, noop, delay, recur, self.err))
end
-- Create, add and return a normal event
return self:add(event.new(self, fn, delay, recur, self.err))
end
function tick:delay(fn, delay)
return self:event(fn, delay, false)
end
function tick:recur(fn, delay)
return self:event(fn, delay, true)
end
local group = tick.group()
local bound = {
update = function(...) return tick.update(group, ...) end,
delay = function(...) return tick.delay (group, ...) end,
recur = function(...) return tick.recur (group, ...) end,
remove = function(...) return tick.remove(group, ...) end,
}
setmetatable(bound, tick)
return bound