Escolar Documentos
Profissional Documentos
Cultura Documentos
local
local
local
local
local
secondsQueue = {}
framesQueue = {}
inputQueue = {}
signalQueue = {}
eventQueue = {}
local breakQueue = {}
eventu.deltaTime = 0;
eventu.time = 0;
eventu.timeFrozen = 0;
eventu.deltaTimeMax = 0.1;
function eventu.onInitAPI()
registerEvent(eventu,
registerEvent(eventu,
t
registerEvent(eventu,
registerEvent(eventu,
end
function eventu.init()
eventu.time = os.clock();
end
local function resumeCoroutine(co)
local coStatus, coMsg = coroutine.resume(co)
if (not coStatus) then
-- Ignore dead coroutines because that's just the result of a normal
non-error exit
if (coMsg ~= "cannot resume dead coroutine") then
-- Throw error up the chain, including a stack trace of the corou
tine that we otherwise wouldn't get
error(coMsg .. "\n=============\ncoroutine " .. debug.traceback(c
o))
end
end
return coStatus, coMsg;
end
function eventu.update()
eventu.deltaTime = os.clock() - eventu.time;
if(eventu.deltaTime > eventu.deltaTimeMax) then
eventu.timeFrozen = eventu.deltaTime;
eventu.deltaTime = 0;
else
eventu.timeFrozen = 0;
end
for k,v in pairs(secondsQueue) do
secondsQueue[k] = v-eventu.deltaTime;
if(v <= 0) then
secondsQueue[k] = nil;
resumeCoroutine (k)
end
end
framesQueue[co] = frames;
-- And suspend the process
return coroutine.yield (co)
end
function eventu.waitForInput(key)
local co = getCurrentCoroutine();
inputQueue[co] = key;
-- And suspend the process
return coroutine.yield (co)
end
function eventu.waitSignal(name)
local co = getCurrentCoroutine();
if(signalQueue[name] == nil) then
signalQueue[name] = {};
end
table.insert(signalQueue[name], co);
-- And suspend the process
return coroutine.yield (co)
end
function eventu.waitEvent(name)
local co = getCurrentCoroutine();
if(eventQueue[name] == nil) then
eventQueue[name] = {};
end
table.insert(eventQueue[name], co);
-- And suspend the process
return coroutine.yield (co)
end
function eventu.signal(name)
local waketable = {}
for k,v in pairs(signalQueue[name]) do
signalQueue[name][k] = nil;
table.insert(waketable, v);
end
signalQueue[name] = nil;
for _,v in ipairs(waketable) do
resumeCoroutine (v)
end
end
function eventu.setTimer(secs, func, repeated)
repeated = repeated or false;
eventu.run(function()
repeat
eventu.waitSeconds(secs)
;
func();
local co = getCurrentCor
outine();
if(breakQueue[co] == tru
e) then
breakQueue[co] =
nil;
repeated = false
;
end
until repeated == false;
end);
end
function eventu.setFrameTimer(frames, func, repeated)
repeated = repeated or false;
eventu.run(function()
repeat
eventu.waitFrames(frames
);
func();
local co = getCurrentCor
outine();
if(breakQueue[co] == tru
e) then
breakQueue[co] =
nil;
repeated = false
;
end
until repeated == false;
end);
end
function eventu.breakTimer()
local co = getCurrentCoroutine();
breakQueue[co] = true;
end
function eventu.registerKeyEvent(key, func, consume)
consume = consume or false;
eventu.run(function()
repeat
eventu.waitForInput(key)
;
func();
local co = getCurrentCor
outine();
if(breakQueue[co] == tru
e) then
breakQueue[co] =
nil;
consume = true;
end
until consume == true;
end);
end
function eventu.registerSMBXEvent(event, func, repeated)
repeated = repeated or false;
eventu.run(function()
repeat
eventu.waitEvent(event);
func();
local co = getCurrentCor
outine();
if(breakQueue[co] == tru
e) then
breakQueue[co] =
nil;
repeated = false
;
end
until repeated == false;
end);
end
return eventu;