From a7880c5098c8022df8a8b94b9633d000546d4d91 Mon Sep 17 00:00:00 2001 From: Kingdaro Date: Sun, 15 Dec 2013 00:02:09 -0500 Subject: [PATCH 1/5] add all callbacks for 0.9.0 --- gamestate.lua | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/gamestate.lua b/gamestate.lua index 9a9fc09..262c40f 100644 --- a/gamestate.lua +++ b/gamestate.lua @@ -65,9 +65,27 @@ function GS.current() end local all_callbacks = { - 'update', 'draw', 'focus', 'keypressed', 'keyreleased', - 'mousepressed', 'mousereleased', 'joystickpressed', - 'joystickreleased', 'textinput', 'quit', 'textinput' + "update", + "draw", + "focus", + "keypressed", + "keyreleased", + "mousefocus", + "mousepressed", + "mousereleased", + "resize", + "textinput", + "visible", + "quit", + "joystickadded", + "joystickremoved", + "joystickpressed", + "joystickreleased", + "joystickaxis", + "joystickhat", + "gamepadpressed", + "gamepadreleased", + "gamepadaxis" } function GS.registerEvents(callbacks) From d77e0a2324b41893b5dcba25ff620decad717eba Mon Sep 17 00:00:00 2001 From: Kingdaro Date: Mon, 23 May 2016 18:45:20 -0400 Subject: [PATCH 2/5] Fix for https://github.com/vrld/hump/issues/66 --- timer.lua | 56 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/timer.lua b/timer.lua index 2693849..ecd380a 100644 --- a/timer.lua +++ b/timer.lua @@ -31,23 +31,41 @@ local function _nothing_() end function Timer:update(dt) local to_remove = {} - for handle, delay in pairs(self.functions) do - delay = delay - dt - if delay <= 0 then - to_remove[#to_remove+1] = handle + + for handle in pairs(self.functions) do + -- handle: { + -- time = , + -- after = , + -- during = , + -- limit = , + -- count = , + -- } + + handle.time = handle.time + dt + handle.during(dt, handle.limit - handle.time) + + while handle.time >= handle.limit and handle.count > 0 do + if handle.after(handle.after) == false then + handle.count = 0 + break + end + handle.time = handle.time - handle.limit + handle.count = handle.count - 1 + end + + if handle.count == 0 then + table.insert(to_remove, handle) end - self.functions[handle] = delay - handle.func(dt, delay) end - for _,handle in ipairs(to_remove) do - self.functions[handle] = nil - handle.after(handle.after) + + for i = 1, #to_remove do + self.functions[to_remove[i]] = nil end end -function Timer:during(delay, func, after) - local handle = {func = func, after = after or _nothing_} - self.functions[handle] = delay +function Timer:during(delay, during, after) + local handle = { time = 0, during = during, after = after, limit = delay, count = 1 } + self.functions[handle] = true return handle end @@ -55,16 +73,10 @@ function Timer:after(delay, func) return self:during(delay, _nothing_, func) end -function Timer:every(delay, func, count) - local count, handle = count or math.huge -- exploit below: math.huge - 1 = math.huge - - handle = self:after(delay, function(f) - if func(func) == false then return end - count = count - 1 - if count > 0 then - self.functions[handle] = delay - end - end) +function Timer:every(delay, after, count) + local count = count or math.huge -- exploit below: math.huge - 1 = math.huge + local handle = { time = 0, during = _nothing_, after = after, limit = delay, count = count } + self.functions[handle] = true return handle end From 08354c472def35ff1b5610919dea4cf8376ea78b Mon Sep 17 00:00:00 2001 From: Kingdaro Date: Mon, 23 May 2016 19:03:41 -0400 Subject: [PATCH 3/5] Write unit test for timer --- spec/timer_spec.lua | 73 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 spec/timer_spec.lua diff --git a/spec/timer_spec.lua b/spec/timer_spec.lua new file mode 100644 index 0000000..9a7c2ef --- /dev/null +++ b/spec/timer_spec.lua @@ -0,0 +1,73 @@ +local timer = require 'timer'() + +describe('hump.timer', function() + it('runs a function during a specified time', function() + local delta, remaining + + timer:during(10, function(...) delta, remaining = ... end) + + timer:update(2) + assert.are.equal(delta, 2) + assert.are.equal(8, remaining) + + timer:update(5) + assert.are.equal(delta, 5) + assert.are.equal(3, remaining) + + timer:update(10) + assert.are.equal(delta, 10) + assert.are.equal(0, remaining) + end) + + it('runs a function after a specified time', function() + local finished1 = false + local finished2 = false + + timer:after(3, function(...) finished1 = true end) + timer:after(5, function(...) finished2 = true end) + + timer:update(4) + assert.are.equal(true, finished1) + assert.are.equal(false, finished2) + + timer:update(4) + assert.are.equal(true, finished1) + assert.are.equal(true, finished2) + end) + + it('runs a function every so often', function() + local count = 0 + + timer:every(1, function(...) count = count + 1 end) + + timer:update(3) + assert.are.equal(3, count) + + timer:update(7) + assert.are.equal(10, count) + end) + + it('can script timed events', function() + local state + + timer:script(function(wait) + state = 'foo' + wait(1) + state = 'bar' + end) + + assert.are.equal('foo', state) + timer:update(0.5) + assert.are.equal('foo', state) + timer:update(1) + assert.are.equal('bar', state) + end) + + it('cancels and clears timer functions', function() + pending('to be tested...') + end) + + it('tweens', function() + pending('to be tested...') + end) +end) From 6cf4a54d9d8c9e4ac9be531288ae8ff06bb63506 Mon Sep 17 00:00:00 2001 From: Kingdaro Date: Mon, 23 May 2016 19:03:58 -0400 Subject: [PATCH 4/5] Fix for :during() call without an after function --- timer.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/timer.lua b/timer.lua index ecd380a..1846be6 100644 --- a/timer.lua +++ b/timer.lua @@ -64,7 +64,7 @@ function Timer:update(dt) end function Timer:during(delay, during, after) - local handle = { time = 0, during = during, after = after, limit = delay, count = 1 } + local handle = { time = 0, during = during, after = after or _nothing_, limit = delay, count = 1 } self.functions[handle] = true return handle end From 25e6035d54a34b1f8368f94e8b42fd09971e1d8f Mon Sep 17 00:00:00 2001 From: Kingdaro Date: Mon, 23 May 2016 19:05:17 -0400 Subject: [PATCH 5/5] Fixed remaining time on during function When expired, the remaining time should never be less than 0. --- timer.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/timer.lua b/timer.lua index 1846be6..5b4e515 100644 --- a/timer.lua +++ b/timer.lua @@ -42,7 +42,7 @@ function Timer:update(dt) -- } handle.time = handle.time + dt - handle.during(dt, handle.limit - handle.time) + handle.during(dt, math.max(handle.limit - handle.time, 0)) while handle.time >= handle.limit and handle.count > 0 do if handle.after(handle.after) == false then