diff options
Diffstat (limited to 'not')
-rw-r--r-- | not/Timer.lua | 30 | ||||
-rw-r--r-- | not/Trigger.lua | 18 |
2 files changed, 48 insertions, 0 deletions
diff --git a/not/Timer.lua b/not/Timer.lua new file mode 100644 index 0000000..3e45b11 --- /dev/null +++ b/not/Timer.lua @@ -0,0 +1,30 @@ +Timer = require "not.Object":extends() + +function Timer:new (trigger, delay) + self.trigger = trigger + self.delay = delay + self.left = 0 + self.active = false + self.restart = false +end + +function Timer:start () + self.left = self.delay + self.active = true +end + +function Timer:update (dt) + if self.active then + if self.left < 0 then + self.trigger:emit() + self.active = false + if self.restart then + self:start() + end + else + self.left = self.left - dt + end + end +end + +return Timer diff --git a/not/Trigger.lua b/not/Trigger.lua new file mode 100644 index 0000000..c6ef7c7 --- /dev/null +++ b/not/Trigger.lua @@ -0,0 +1,18 @@ +Trigger = require "not.Object":extends() + +function Trigger:new () + self.calls = {} +end + +function Trigger:register (func, ...) + local call = {func = func, params = {...}} + table.insert(self.calls, call) +end + +function Trigger:emit () + for _,call in pairs(self.calls) do + call.func(unpack(call.params)) + end +end + +return Trigger |