pico8-0hh1/main.lua

169 lines
2.6 KiB
Lua

--
-- constants
--
local BLUE = 1
local YELLOW = 2
-- 🅾️ Z[C]N ❎ [X]VM
-- X and C have the same position on QWERTY and AZERTY
local LEFT,RIGHT,UP,DOWN,BTN_O,BTN_X = 0,1,2,3,4,5
--
-- Utils
--
function customFont()
poke(0x5f58,0x81)
end
function standardFont(font)
poke(0x5f58,0x80)
end
function idx_xy(idx, width)
return (idx - 1) % width + 1, (idx - 1) \ width + 1
end
function xy_idx(x, y, width)
return ((y - 1) * width) + x
end
function map(tbl, f)
local t = {}
for k, v in pairs(tbl) do
t[k] = f(v)
end
return t
end
function filter(tbl, f, keepindex)
local ret = {}
for k, v in pairs(tbl) do
if f(v) then
if keepindex
then ret[k] = v
else add(ret, v)
end
end
end
return ret
end
function slice(tbl, first, last, step)
local sliced = {}
for i = (first or 1), (last or #tbl), (step or 1) do
sliced[#sliced + 1] = tbl[i]
end
return sliced
end
function rectfill2(x, y, w, h, col)
rectfill(x, y, x+w, y+h, col)
end
function rect2(x, y, w, h, col)
rect(x, y, x+w, y+h, col)
end
--
-- Overrides count() to accept a callback
--
-- local _count = count
-- function count(tbl, p)
-- if type(p) != "function" then return _count(tbl, p) end
-- local c = 0
-- for v in all(tbl) do
-- if p(v) then c+=1 end
-- end
-- return c
-- end
--
-- Returns the indices of found occurences of `o` within `tbl`
--
function find(tbl, o)
local indices = {}
for k,v in ipairs(tbl) do
if v == o then add(indices, k) end
end
return indices
end
--
-- Makes a shallow table copy
--
function copy(tbl)
return map(tbl, function (o) return o end)
end
--
-- Table equality - shallow comparison
--
function equal(tbl1, tbl2)
for k, _ in ipairs(tbl1) do
if tbl1[k] ~= tbl2[k] then return false end
end
return true
end
-- function tostring(any)
-- if (type(any)~="table") return tostr(any)
-- local str = "{"
-- for k,v in pairs(any) do
-- if (str~="{") str=str..","
-- str=str..tostring(k).."="..tostring(v)
-- end
-- return str.."}"
-- end
function setState(state, ...)
local args = {...}
if gs and gs._leave then gs._leave() end
gs = state
if gs and gs._enter then gs._enter(unpack(args)) end
end
--
-- main loop
--
local create = true
function _init()
printh(" ")
printh(" ")
mouse_x = 0
mouse_y = 0
frameCount = 0
states = {
rules = stateRules(),
menu = stateMenu(),
game = stateGame()
}
setState(states.game)
end
function _update60()
frameCount += 1
_coresolve()
gs._update()
-- if not create then
-- board:solveStep()
-- end
-- mouse
mouse_x = stat(32)
mouse_y = stat(33)
end
function _draw()
gs._draw()
spr(1, stat(32), stat(33))
end