pico8-0hh1/main.lua
2022-05-30 22:45:12 +02:00

199 lines
3.3 KiB
Lua

poke(0x5F2D, 1)
--
-- 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 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 makeButton(x, y, w, h, text, onClick, onHover)
local state = 0 -- 0 = normal, 1 = hovered, 2 = pressed
return {
x = x,
y = y,
w = w,
h = h,
text = text,
onClick = onClick,
onHover = onHover,
draw=function(self, selected)
-- rectfillw(self.x, self.y, self.w, self.h, 8)
local color = selected and 7 or 5
print(self.text, self.x+1, self.y+1, color)
end,
update=function(self)
if mouse_x >= self.x and mouse_x <= self.x + self.w and
mouse_y >= self.y and mouse_y <= self.y + self.h then
if stat(34)&1 == 0 and state == 2 and self.onClick then
self.onClick()
end
if stat(34)&1 == 1 then
state = 2
else
if onHover then onHover() end
state = 1
end
else
state = 0
end
end
}
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()
cls()
printh(" ")
printh(" ")
mouse_x = 0
mouse_y = 0
frameCount = 0
states = {
rules = stateRules(),
menu = stateMenu(),
game = stateGame()
}
setState(states.rules)
end
function _update60()
frameCount += 1
gs._update()
-- if not create then
-- board:solveStep()
-- end
-- mouse
mouse_x = stat(32)
mouse_y = stat(33)
end
function _draw()
cls()
gs._draw()
spr(1, stat(32), stat(33))
end