From 31c994c223c0a9a63d503d43877254268f52811c Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 14 Sep 2017 16:23:07 +0200 Subject: Added Timer and Trigger classes --- not/Timer.lua | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 not/Timer.lua (limited to 'not/Timer.lua') 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 -- cgit v1.1