Add state manager and ironman mode

This commit is contained in:
Pyrex 2024-02-10 17:44:38 -08:00
parent 59de5962dc
commit 2ad6a0bc3a
7 changed files with 118 additions and 28 deletions

View File

@ -85,6 +85,14 @@ function board:on_move()
self:find_automove() self:find_automove()
end end
function board:is_won()
if (not self:can_take_input()) return false
for s=1,#self.slots do
if (self.slots[s]:peek()) return false
end
return true
end
function board:find_automove() function board:find_automove()
for s=1,#self.slots do for s=1,#self.slots do
local top=self.slots[s]:peek() local top=self.slots[s]:peek()

View File

@ -3,37 +3,14 @@ add(modules,main)
function main:init() function main:init()
extcmd("rec") extcmd("rec")
self.board=board:new(progression[1]) self.state_manager=state_manager:new() -- instantiate one global
self.state_manager:push(state_ironman:new())
end end
function main:update() function main:update()
self.board:update() self.state_manager:update()
if self.board:can_take_input() then
if (btnp(0)) self.board.cursor:move_x(-1)
if (btnp(1)) self.board.cursor:move_x(1)
if (btnp(2)) self.board.cursor:move_y(-1)
if (btnp(3)) self.board.cursor:move_y(1)
if (btnp(4)) self.board.cursor:toggle_grab()
if (btnp(5)) self.board:undo()
end
end end
function main:draw() function main:draw()
cls(13) self.state_manager:draw()
self.board:draw()
-- bg
pal(13,-3,1)
-- arcana
-- pal(1,0,1)
pal(15,-9,1)
-- suits
pal(4,-11,1) -- first suit
pal(12,12,1)
pal(2,-8,1)
pal(3,-5,1)
-- pal(8,-9,1)
-- pal(14,8,1)
end end

View File

@ -13,6 +13,9 @@ __lua__
#include progression.lua #include progression.lua
#include seed_constants.lua #include seed_constants.lua
#include seeds.lua #include seeds.lua
#include state_manager.lua
#include state_gameround.lua
#include state_ironman.lua
#include main.lua #include main.lua
--[[ --[[

View File

@ -1,5 +1,4 @@
progression={ progression={
ruleset:new(11,5,10,25,"l7"),
-- level 1 -- level 1
ruleset:new(5,1,9,0), -- by test: always winnable ruleset:new(5,1,9,0), -- by test: always winnable
-- level 2 -- level 2

42
state_gameround.lua Normal file
View File

@ -0,0 +1,42 @@
state_gameround=klass()
function state_gameround:init(ruleset)
self.board=board:new(ruleset)
end
function state_gameround:enter() end
function state_gameround:exit() end
function state_gameround:suspend() end
function state_gameround:reenter() end
function state_gameround:update()
self.board:update()
if self.board:can_take_input() then
if (btnp(0)) self.board.cursor:move_x(-1)
if (btnp(1)) self.board.cursor:move_x(1)
if (btnp(2)) self.board.cursor:move_y(-1)
if (btnp(3)) self.board.cursor:move_y(1)
if (btnp(4)) self.board.cursor:toggle_grab()
if (btnp(5)) self.board:undo()
end
if (self.board:is_won()) self.done=true
end
function state_gameround:draw()
cls(13)
self.board:draw()
-- bg
pal(13,-3,1)
-- arcana
-- pal(1,0,1)
pal(15,-9,1)
-- suits
pal(4,-11,1) -- first suit
pal(12,12,1)
pal(2,-8,1)
pal(3,-5,1)
-- pal(8,-9,1)
-- pal(14,8,1)
end

27
state_ironman.lua Normal file
View File

@ -0,0 +1,27 @@
state_ironman=klass()
function state_ironman:init(ruleset)
self.level=1
end
function state_ironman:enter() self:on_enter() end
function state_ironman:exit() end
function state_ironman:reenter() self:on_enter() end
function state_ironman:suspend() end
function state_ironman:on_enter()
self.done=true
local level=self.level
self.level+=1
if level <= #progression then
print("adding another state")
main.state_manager:push(state_gameround:new(progression[level]))
end
end
function state_ironman:update()
assert(false,"wtf")
end
function state_ironman:draw()
assert(false,"wtf2")
end

34
state_manager.lua Normal file
View File

@ -0,0 +1,34 @@
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