2022-06-02 20:18:02 +02:00
|
|
|
function state_game()
|
2023-10-07 19:12:53 +02:00
|
|
|
local board
|
|
|
|
local selected_id = 1
|
|
|
|
local timer_clue = 0
|
|
|
|
local mx, mx = 0, 0
|
|
|
|
|
|
|
|
local clues = {}
|
|
|
|
local function show_clues()
|
|
|
|
local issues = board:get_issues(true)
|
|
|
|
clues = {}
|
|
|
|
for issue in all(issues) do
|
|
|
|
local type, err, row, pos, other = unpack(issue)
|
|
|
|
printh(type .. " " .. err .. " " .. pos)
|
|
|
|
add(clues, { type = type, pos = pos, t = 0 })
|
|
|
|
if err == "identical" then
|
|
|
|
add(clues, { type = type, pos = other, t = 0 })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function draw_selected_tile()
|
|
|
|
local x, y = board:draw_coords(selected_id)
|
|
|
|
local w = board:get_tile_width()
|
|
|
|
-- fillp(▒)
|
|
|
|
rect2(x - 1, y - 1, w, w, 6)
|
|
|
|
line()
|
|
|
|
fillp(█)
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function check_endgame()
|
|
|
|
if board:is_complete() and board:is_valid() then
|
|
|
|
set_state(states.endgame, board)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function _enter(_board)
|
|
|
|
-- mouse bound to buttons
|
|
|
|
poke(0x5F2D, 3)
|
|
|
|
|
|
|
|
board = _board
|
|
|
|
-- lock the initial tiles
|
|
|
|
board:lock_tiles()
|
|
|
|
end
|
|
|
|
|
|
|
|
local function _leave()
|
|
|
|
-- stopcoroutine(show_clues)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function _update()
|
|
|
|
update_mouse()
|
|
|
|
|
|
|
|
local size = board:get_size()
|
|
|
|
local x, y = board:idx_xy(selected_id)
|
|
|
|
local moved = false
|
|
|
|
|
|
|
|
if btnp(UP) then
|
|
|
|
moved = true
|
|
|
|
y -= 1
|
|
|
|
elseif btnp(DOWN) then
|
|
|
|
moved = true
|
|
|
|
y += 1
|
|
|
|
elseif btnp(LEFT) then
|
|
|
|
moved = true
|
|
|
|
x -= 1
|
|
|
|
elseif btnp(RIGHT) then
|
|
|
|
moved = true
|
|
|
|
x += 1
|
|
|
|
end
|
2023-10-07 22:14:18 +02:00
|
|
|
|
|
|
|
x = mid(1, x, size)
|
|
|
|
y = mid(1, y, size)
|
|
|
|
|
2023-10-07 19:12:53 +02:00
|
|
|
if moved then
|
|
|
|
selected_id = board:xy_idx(x, y)
|
|
|
|
end
|
|
|
|
|
|
|
|
if btnp(BTN_X) then
|
|
|
|
board:try_flip_tile(selected_id)
|
|
|
|
show_clues()
|
|
|
|
end
|
|
|
|
|
|
|
|
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()
|
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
|
|
|
_enter = _enter,
|
|
|
|
_update = _update,
|
|
|
|
_draw = _draw,
|
|
|
|
_leave = _leave
|
|
|
|
}
|
2022-05-29 18:38:07 +02:00
|
|
|
end
|