mirror of
https://github.com/bryanthaboi/gen1recomp
synced 2026-09-29 07:00:55 -04:00
initial commit
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
-- Game state stack. The top state updates; all states draw bottom-up
|
||||
-- (so a text box can overlay the overworld, a battle replaces it, etc).
|
||||
-- States are tables with optional enter/exit/update/draw/isOpaque.
|
||||
|
||||
local StateStack = {}
|
||||
|
||||
function StateStack:init()
|
||||
self.states = {}
|
||||
end
|
||||
|
||||
function StateStack:push(state, ...)
|
||||
table.insert(self.states, state)
|
||||
if state.enter then state:enter(...) end
|
||||
end
|
||||
|
||||
function StateStack:pop()
|
||||
local state = table.remove(self.states)
|
||||
if state and state.exit then state:exit() end
|
||||
return state
|
||||
end
|
||||
|
||||
function StateStack:top()
|
||||
return self.states[#self.states]
|
||||
end
|
||||
|
||||
function StateStack:update(dt)
|
||||
local top = self:top()
|
||||
if top and top.update then top:update(dt) end
|
||||
end
|
||||
|
||||
-- index of the lowest state drawn this frame (highest opaque, else 1)
|
||||
function StateStack:visibleBase()
|
||||
for i = #self.states, 1, -1 do
|
||||
if self.states[i].isOpaque then return i end
|
||||
end
|
||||
return 1
|
||||
end
|
||||
|
||||
function StateStack:draw()
|
||||
for i = self:visibleBase(), #self.states do
|
||||
if self.states[i].draw then self.states[i]:draw() end
|
||||
end
|
||||
end
|
||||
|
||||
return StateStack
|
||||
Reference in New Issue
Block a user