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 From 0e9ad14e9fcee242a68f695715fa106af99aa76f Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 14 Sep 2017 17:11:54 +0200 Subject: Timer now inherits after Trigger --- not/Timer.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'not/Timer.lua') diff --git a/not/Timer.lua b/not/Timer.lua index 3e45b11..6fc4a4c 100644 --- a/not/Timer.lua +++ b/not/Timer.lua @@ -1,7 +1,6 @@ -Timer = require "not.Object":extends() +Timer = require "not.Trigger":extends() -function Timer:new (trigger, delay) - self.trigger = trigger +function Timer:new (delay) self.delay = delay self.left = 0 self.active = false @@ -16,7 +15,7 @@ end function Timer:update (dt) if self.active then if self.left < 0 then - self.trigger:emit() + self:emit() self.active = false if self.restart then self:start() -- cgit v1.1 From 2a65a119ef6cb8f99f7f5580439b0b63dcd85a69 Mon Sep 17 00:00:00 2001 From: Aki Date: Thu, 14 Sep 2017 17:13:16 +0200 Subject: Added missing super constructor --- not/Timer.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'not/Timer.lua') diff --git a/not/Timer.lua b/not/Timer.lua index 6fc4a4c..9ae0de8 100644 --- a/not/Timer.lua +++ b/not/Timer.lua @@ -1,6 +1,7 @@ Timer = require "not.Trigger":extends() function Timer:new (delay) + Timer.__super.new(self) self.delay = delay self.left = 0 self.active = false -- cgit v1.1