pico8-0hh1/states/game.lua

134 lines
2.6 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-12 21:47:10 +02:00
local mx,mx = 0,0
2022-07-02 20:45:11 +02:00
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(▒)
2022-07-10 16:39:11 +02:00
rect2(x-1, y-1, w, w, 6)
2022-07-07 23:06:28 +02:00
line()
2022-06-02 22:39:23 +02:00
fillp()
2022-06-01 23:14:22 +02:00
end
2022-07-10 16:39:11 +02:00
local function update_mouse()
-- update mouse position
if mx == mouse_x and my == mouse_y then return end
mx,my = mouse_x, mouse_y
local board_x, board_y = board:draw_coords(1)
local tw = board:get_tile_width()
local bw = board:get_size()
-- pixels coords to grid coords
local x = mid(1, (mouse_x - board_x) \ tw + 1, bw)
local y = mid(1, (mouse_y - board_y) \ tw + 1, bw)
selected_id = board:xy_idx(x,y)
2022-07-12 21:47:10 +02:00
end
local function check_endgame()
if board:is_complete() and board:is_valid() then
set_state(states.endgame, board)
end
2022-07-10 16:39:11 +02:00
end
2022-06-02 22:39:23 +02:00
local function _enter(_board)
2022-07-12 21:47:10 +02:00
-- mouse bound to buttons
poke(0x5F2D, 3)
2022-06-02 22:39:23 +02:00
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 _update()
2023-10-07 18:59:32 +02:00
update_mouse()
2022-06-02 20:18:02 +02:00
local size = board:get_size()
local x, y = board:idx_xy(selected_id)
2022-07-10 16:39:11 +02:00
local moved = false
2022-07-12 21:47:10 +02:00
2022-06-01 23:14:22 +02:00
if btnp(UP) then
2022-07-10 16:39:11 +02:00
moved = true
2022-06-01 23:14:22 +02:00
y -= 1
elseif btnp(DOWN) then
2022-07-10 16:39:11 +02:00
moved = true
2022-06-01 23:14:22 +02:00
y += 1
elseif btnp(LEFT) then
2022-07-10 16:39:11 +02:00
moved = true
2022-06-01 23:14:22 +02:00
x -= 1
elseif btnp(RIGHT) then
2022-07-10 16:39:11 +02:00
moved = true
2022-06-01 23:14:22 +02:00
x += 1
end
2022-07-10 16:39:11 +02:00
if moved then
selected_id = board:xy_idx(x, y)
end
2022-07-01 19:50:08 +02:00
2022-07-10 16:39:11 +02:00
if btnp(BTN_X) then
2022-07-01 19:50:08 +02:00
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-07-12 21:47:10 +02:00
check_endgame()
end
local function _draw()
cls()
local x,y = board:draw_coords(1)
draw_animated_bg(x)
board:draw()
draw_selected_tile()
-- draw clues
for clue in all(clues) do
palt(0,false)
palt(5,true)
local x,y
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)
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
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