-
Notifications
You must be signed in to change notification settings - Fork 2
/
ticktimer.lua
98 lines (73 loc) · 2.51 KB
/
ticktimer.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
-- ----------------------------------------------------------------------------
--
-- TickTimer
--
-- counts the time elapsed between a start time and time now
-- the accurancy of this timer object relies on the frequency
-- its methods are called
-- ----------------------------------------------------------------------------
local TickTimer = { }
TickTimer.__index = TickTimer
local _clock = os.clock
-- ----------------------------------------------------------------------------
--
function TickTimer.new(inName)
inName = inName or "ANY"
local t =
{
m_Name = inName, -- a name for the object
m_NextTick = 0, -- next time to fire
m_TickFrame = 0, -- firing delay
m_Enabled = false, -- timer is actually enabled
}
return setmetatable(t, TickTimer)
end
-- ----------------------------------------------------------------------------
--
function TickTimer.Setup(self, inInterval, inEnabled)
self.m_Enabled = inEnabled
self.m_TickFrame = inInterval * 1
self.m_NextTick = _clock() + self.m_TickFrame
end
-- ----------------------------------------------------------------------------
--
function TickTimer.Reset(self)
self.m_NextTick = _clock() + self.m_TickFrame
end
-- ----------------------------------------------------------------------------
--
function TickTimer.Enable(self, inEnable)
self.m_Enabled = inEnable
end
-- ----------------------------------------------------------------------------
--
function TickTimer.IsEnabled(self)
return self.m_Enabled
end
-- ----------------------------------------------------------------------------
--
function TickTimer.HasFired(self)
if self.m_Enabled then return _clock( ) > self.m_NextTick end
-- timer is disabled
--
return false
end
-- ----------------------------------------------------------------------------
--
function TickTimer.ElapsedTime(self)
return self.m_NextTick - _clock()
end
-- ----------------------------------------------------------------------------
--
function TickTimer.ShowInterval(self)
local iEnabled = 0
if self.m_Enabled then iEnabled = 1 end
local sText = string.format("[%s] enable: [%d] elapsed: [%.4f]",
self.m_Name, iEnabled, self:ElapsedTime())
return sText
end
-- ----------------------------------------------------------------------------
--
return TickTimer
-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------