Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on May 23, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathCpObject.lua
More file actions
Latest commit
120 lines (105 loc) · 3.36 KB
/
Copy pathCpObject.lua
File metadata and controls
120 lines (105 loc) · 3.36 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
--[[
This file is part of Courseplay (https://github.com/Courseplay/courseplay)
Copyright (C) 2018 Peter Vajko
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
-- Class implementation stolen from http://lua-users.org/wiki/SimpleLuaClasses
functionCpObject(base, init)
localc= {} -- a new class instance
ifnotinitandtype(base) =='function' then
init=base
base=nil
elseiftype(base) =='table' then
-- our new class is a shallow copy of the base class!
fori,vinpairs(base) do
c[i] =v
end
c._base=base
end
-- the class will be the metatable for all its objects,
-- and they will look up their methods in it.
c.__index=c
-- expose a constructor which can be called by <classname>(<args>)
localmt= {}
mt.__call=function(class_tbl, ...)
localobj= {}
setmetatable(obj,c)
ifclass_tbl.initthen
class_tbl.init(obj,...)
else
-- make sure that any stuff from the base class is initialized!
ifbaseandbase.initthen
base.init(obj, ...)
end
end
returnobj
end
c.init=init
c.is_a=function(self, klass)
localm=getmetatable(self)
whilemdo
ifm==klassthenreturntrueend
m=m._base
end
returnfalse
end
setmetatable(c, mt)
returnc
end
--- Object with a time to live.
---@classCpTemporaryObject
CpTemporaryObject=CpObject()
functionCpTemporaryObject:init(valueWhenExpired)
self.valueWhenExpired=valueWhenExpired
self.value=self.valueWhenExpired
self.expiryTime=g_time
end
--- Set temporary value for object
---@value anything the temporary value
---@ttlMs Time To Live, for ttlMs milliseconds, CpTemporaryObject:get() will
--- return this value, otherwise valueWhenExpired
functionCpTemporaryObject:set(value, ttlMs)
self.value=value
self.expiryTime=g_time+ttlMs
end
--- Get the value of the temporary object
--- If expired, returns the default value
functionCpTemporaryObject:get()
ifg_time>self.expiryTimethen
-- value expired, reset it
self.value=self.valueWhenExpired
end
returnself.value
end
--- Object slowly adjusting its value
---@classCpSlowChangingObject
CpSlowChangingObject=CpObject()
functionCpSlowChangingObject:init(targetValue, timeToReachTargetMs)
self.value=targetValue
self:set(targetValue, timeToReachTargetMs)
end
functionCpSlowChangingObject:set(targetValue, timeToReachTargetMs)
self.previousValue=self.value
self.targetValue=targetValue
self.targetValueMs=g_time
self.timeToReachTargetMs=timeToReachTargetMsor1
end
functionCpSlowChangingObject:get()
localage=g_time-self.targetValueMs
ifage<self.timeToReachTargetMsthen
-- not reaped yet, return a value proportional to the time until ripe
self.value=self.previousValue+ (self.targetValue-self.previousValue) *age/self.timeToReachTargetMs
else
self.value=self.targetValue
end
returnself.value
end