34 lines
744 B
Lua
34 lines
744 B
Lua
state_manager=klass()
|
|
function state_manager:init()
|
|
self._states={}
|
|
end
|
|
function state_manager:push(state)
|
|
local top=self:peek()
|
|
add(self._states,state)
|
|
if (top) top:suspend(state)
|
|
state:enter(top)
|
|
end
|
|
function state_manager:pop()
|
|
local top=deli(self._states,#self._states)
|
|
if (not top) return
|
|
|
|
local new_top=self:peek()
|
|
top:exit(new_top)
|
|
if (new_top) new_top:reenter(top)
|
|
return top
|
|
end
|
|
function state_manager:peek()
|
|
return self._states[#self._states]
|
|
end
|
|
function state_manager:update()
|
|
local state=self:peek()
|
|
if (state) state:update()
|
|
while true do
|
|
local state=self:peek()
|
|
if state and state.done then self:pop() else break end
|
|
end
|
|
end
|
|
function state_manager:draw()
|
|
local state=self:peek()
|
|
if (state) state:draw()
|
|
end |