Add gamestate:init()

This commit is contained in:
Matthias Richter 2011-03-15 17:34:28 +01:00
parent 94da317ed9
commit e885ec90d4

View File

@ -31,6 +31,7 @@ local function __NULL__() end
-- default gamestate produces error on every callback -- default gamestate produces error on every callback
local function __ERROR__() error("Gamestate not initialized. Use Gamestate.switch()") end local function __ERROR__() error("Gamestate not initialized. Use Gamestate.switch()") end
current = { current = {
init = __ERROR__,
enter = __ERROR__, enter = __ERROR__,
leave = __NULL__, leave = __NULL__,
update = __ERROR__, update = __ERROR__,
@ -47,6 +48,7 @@ current = {
function new() function new()
return { return {
init = __NULL__,
enter = __NULL__, enter = __NULL__,
leave = __NULL__, leave = __NULL__,
update = __NULL__, update = __NULL__,
@ -66,6 +68,8 @@ function switch(to, ...)
assert(to, "Missing argument: Gamestate to switch to") assert(to, "Missing argument: Gamestate to switch to")
current:leave() current:leave()
local pre = current local pre = current
to:init()
to.init = __NULL__
current = to current = to
return current:enter(pre, ...) return current:enter(pre, ...)
end end