Tutorial code 1

This commit is contained in:
Pyrex 2024-02-10 19:42:38 -08:00
parent 2ad6a0bc3a
commit 15e86c7940
7 changed files with 99 additions and 8 deletions

View File

@ -1,6 +1,7 @@
board=klass()
function board:init(ruleset)
function board:init(watcher,ruleset)
self.watcher=watcher
self.ruleset=ruleset
self.cursor=cursor:new(self)
self.animator=animator:new()
@ -45,7 +46,7 @@ function board:init(ruleset)
local seed=seeds:choose(self.ruleset.pool)
printh("chosen seed: "..tostr(seed,2))
self:deal(seeds:choose(self.ruleset.pool))
self:deal(watcher.seed or seeds:choose(self.ruleset.pool))
end
function board:deal(seed)
@ -75,6 +76,7 @@ end
function board:on_idle()
self:find_automove()
self.watcher:update(self)
end
function board:pre_move(card)
@ -83,6 +85,7 @@ end
function board:on_move()
self:find_automove()
self.watcher:update(self)
end
function board:is_won()
@ -177,6 +180,8 @@ function board:draw()
end
end)
end
self.watcher:draw(self)
end
slot=klass()

View File

@ -16,6 +16,9 @@ __lua__
#include state_manager.lua
#include state_gameround.lua
#include state_ironman.lua
#include tutorial_watcher.lua
#include tutorial.lua
#include text.lua
#include main.lua
--[[

View File

@ -1,12 +1,12 @@
state_gameround=klass()
function state_gameround:init(ruleset)
self.board=board:new(ruleset)
function state_gameround:init(watcher,ruleset)
self.board=board:new(watcher,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:suspend() end
function state_gameround:update()
self.board:update()

View File

@ -1,6 +1,7 @@
state_ironman=klass()
function state_ironman:init(ruleset)
function state_ironman:init()
self.level=1
self.tutorial_enabled=true
end
function state_ironman:enter() self:on_enter() end
@ -14,8 +15,10 @@ function state_ironman:on_enter()
local level=self.level
self.level+=1
if level <= #progression then
print("adding another state")
main.state_manager:push(state_gameround:new(progression[level]))
local watcher_fn=tutorial[level]
local watcher=tutorial_watcher:new()
if (self.tutorial_enabled and watcher_fn) watcher=watcher_fn()
main.state_manager:push(state_gameround:new(watcher, progression[level]))
end
end

9
text.lua Normal file
View File

@ -0,0 +1,9 @@
function measure_text(s)
local w=0
local lines=split(s,"\n")
for l in all(lines) do
w=max(w,print(l,0,128))
end
local h=#lines*6
return w,h
end

12
tutorial.lua Normal file
View File

@ -0,0 +1,12 @@
tutorial={
[1]=function()
return tutorial_watcher:new(10,{
tutorial_stage:new(
"slot",1,2,
tutorial_move_slot(1,1),
"stack on 5! (🅾️)",
function(b) return b:can_take_input() end
)
})
end
}

59
tutorial_watcher.lua Normal file
View File

@ -0,0 +1,59 @@
tutorial_watcher=klass()
function tutorial_watcher:init(seed,stages)
self.seed=seed
self._stages=stages
end
function tutorial_watcher:update(board)
local stage=self._stages[1]
if (stage) stage:update(board)
end
function tutorial_watcher:active_stage(board)
local stage=self._stages[1]
if (stage and stage:active()) return stage
end
function tutorial_watcher:draw(board)
local stage=self:active_stage()
if (not stage) return
local layouts=board.ruleset.layouts
-- stop("calling: "..stage._layout_name)
local layout=layouts[stage._layout_name](layouts,stage._layout_arg)
local x,y=layout:place_card(stage._layout_arg_2)
local tx=stage._text
local w,h=measure_text(tx)
x+=4
x-=w\2
y+=25
local lx=x+w\2
line(lx,y-9,lx,y,15)
rectfill(x-2,y-2,x+w,y+h,1)
rect(x-2,y-2,x+w,y+h,15)
print(tx,x,y,15)
end
tutorial_stage=klass()
function tutorial_stage:init(layout_name,layout_arg,layout_arg_2,move,text,requirement_cb)
self._layout_name=layout_name
self._layout_arg=layout_arg
self._layout_arg_2=layout_arg_2
self._move=move
self._text=text
self._requirement_cb=requirement_cb
self._enabled=not self._requirement_cb
end
function tutorial_stage:update(board)
if (not self._enabled and self._requirement_cb(board)) self._enabled=true
end
function tutorial_stage:active()
return self._enabled
end
-- todo: restart?
function tutorial_move_undo()
return "undo"
end
function tutorial_move_slot(slot_src,slot_dst)
return tostr(slot_src).."->"..tostr(slot_dst)
end