Created
February 25, 2017 21:06
-
-
Save dvdfu/93422484ab2ddf11e082c6a3032569a1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local Class = require 'modules.middleclass.middleclass' | |
local Timer = require 'modules.hump.timer' | |
local Event = Class('Event') | |
function Event:initialize(time, callbacks) | |
assert(time and callbacks) | |
self.timer = Timer.new() | |
self.timer:after(time, function() | |
self.dead = true | |
end) | |
self.callbacks = { | |
start = callbacks.start, | |
run = callbacks.run, | |
stop = callbacks.stop | |
} | |
self.dead = false | |
self.running = false | |
end | |
function Event:start() | |
assert(not self.running) | |
self.running = true | |
if self.callbacks.start then self.callbacks.start() end | |
end | |
function Event:run(dt) | |
assert(self.running) | |
if self.callbacks.run then self.callbacks.run() end | |
self.timer:update(dt) | |
end | |
function Event:stop() | |
assert(self.running) | |
self.running = false | |
if self.callbacks.stop then self.callbacks.stop() end | |
end | |
function Event:isDead() | |
return self.dead | |
end | |
-- EventSystem _________________________________________________________________ | |
local EventSystem = {} | |
function EventSystem:init() | |
self.events = {} | |
self.activeEvent = nil | |
end | |
function EventSystem:update(dt) | |
if self.activeEvent then | |
if self.activeEvent:isDead() then | |
EventSystem:next() | |
else | |
self.activeEvent:run(dt) | |
end | |
end | |
end | |
function EventSystem:queue(time, callbacks) | |
local event = Event(time, callbacks) | |
table.insert(self.events, event) | |
if not self.activeEvent then | |
EventSystem:next() | |
end | |
end | |
function EventSystem:next() | |
if self.activeEvent then | |
self.activeEvent:stop() | |
end | |
if #self.events == 0 then | |
self.activeEvent = nil | |
else | |
self.activeEvent = self.events[1] | |
self.activeEvent:start() | |
table.remove(self.events, 1) | |
end | |
end | |
function EventSystem:clear() | |
if self.activeEvent then | |
self.activeEvent:stop() | |
end | |
self.events = {} | |
self.activeEvent = nil | |
end | |
function EventSystem:isRunning() | |
return self.activeEvent ~= nil | |
end | |
return EventSystem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use (in
src/objects/gemini.lua
)