pico8-0hh1/states/game.lua

99 lines
1.8 KiB
Lua
Raw Normal View History

2022-06-02 20:18:02 +02:00
function state_game()
2022-05-29 18:38:07 +02:00
2022-06-02 22:39:23 +02:00
local board
2022-06-02 20:18:02 +02:00
local selected_id = 1
2022-07-02 20:45:11 +02:00
local timer_clue = 0
2022-07-07 23:06:28 +02:00
local clues = {}
2022-07-02 20:45:11 +02:00
local function show_clues()
2022-07-03 19:09:15 +02:00
local issues = board:get_issues(true)
2022-07-07 23:06:28 +02:00
clues = {}
2022-07-03 19:09:15 +02:00
for issue in all(issues) do
local type, err, row, pos, other = unpack(issue)
printh(type .. " " .. err .. " " .. pos)
2022-07-07 23:06:28 +02:00
add(clues, {type=type, pos=pos, t=0})
if err == "identical" then
add(clues, {type=type, pos=other, t=0})
end
2022-07-03 19:09:15 +02:00
end
2022-07-02 20:45:11 +02:00
end
2022-06-01 23:14:22 +02:00
2022-07-01 19:50:08 +02:00
local function draw_selected_tile()
2022-06-02 20:18:02 +02:00
local x, y = board:draw_coords(selected_id)
local w = board:get_tile_width()
2022-07-07 23:06:28 +02:00
-- fillp(▒)
rect2(x-1, y-1, w+1, w+1, 6)
line()
2022-06-02 22:39:23 +02:00
fillp()
2022-06-01 23:14:22 +02:00
end
2022-06-02 22:39:23 +02:00
local function _enter(_board)
board = _board
2022-06-03 23:31:26 +02:00
-- lock the initial tiles
board:lock_tiles()
2022-05-29 18:38:07 +02:00
end
2022-07-02 20:45:11 +02:00
local function _leave()
-- stopcoroutine(show_clues)
end
2022-06-01 23:14:22 +02:00
local function _draw()
2022-06-02 17:55:52 +02:00
cls()
2022-07-07 23:06:28 +02:00
local x,y = board:draw_coords(1)
draw_animated_bg(x)
2022-06-01 23:14:22 +02:00
board:draw()
2022-07-01 19:50:08 +02:00
draw_selected_tile()
2022-07-07 23:06:28 +02:00
-- draw clues
for clue in all(clues) do
palt(0,false)
palt(5,true)
local x,y
2022-07-08 22:47:26 +02:00
if clue.type == "row" then
x,y = board:draw_coords((clue.pos-1) * board:get_size()+1)
x=-32
spr(19, x+clue.t%144, y+1+sin(t())*2)
else -- col
x,y = board:draw_coords(clue.pos)
y=-32
spr(19, x+2+sin(t())*2, y+clue.t%144)
end
pset(x, y, 5)
2022-07-07 23:06:28 +02:00
clue.t += 1
end
palt()
2022-06-01 23:14:22 +02:00
end
2022-05-29 18:38:07 +02:00
2022-06-01 23:14:22 +02:00
local function _update()
2022-06-02 20:18:02 +02:00
local size = board:get_size()
local x, y = board:idx_xy(selected_id)
2022-06-01 23:14:22 +02:00
if btnp(UP) then
y -= 1
elseif btnp(DOWN) then
y += 1
elseif btnp(LEFT) then
x -= 1
elseif btnp(RIGHT) then
x += 1
end
2022-07-01 19:50:08 +02:00
if btnp(BTN_O) then
board:try_flip_tile(selected_id)
2022-07-07 23:06:28 +02:00
show_clues()
2022-07-01 19:50:08 +02:00
end
2022-06-01 23:14:22 +02:00
if (x<1) x=size
if (x>size) x=1
if (y<1) y=size
if (y>size) y=1
2022-06-02 20:18:02 +02:00
selected_id = board:xy_idx(x, y)
2022-06-01 23:14:22 +02:00
end
2022-05-29 18:38:07 +02:00
2022-06-01 23:14:22 +02:00
return {
_enter = _enter,
_update = _update,
_draw = _draw,
2022-07-02 20:45:11 +02:00
_leave = _leave
2022-05-29 18:38:07 +02:00
}
end