pico8-0hh1/main.lua
2022-05-29 18:38:07 +02:00

185 lines
2.9 KiB
Lua

poke(0x5F2D, 1)
--
-- constants
--
local BLUE = 1
local YELLOW = 2
--
-- 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
local _rectfill = rectfill
function rectfill(x, y, w, h, col)
_rectfill(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, f)
local state = 0 -- 0 = normal, 1 = hovered, 2 = pressed
return {
x = x,
y = y,
w = w,
h = h,
text = text,
draw=function(self)
rectfill(self.x, self.y, self.w, self.h, 8)
print(self.text.." "..state, self.x+1, self.y+1, 6)
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 f then
f()
end
if stat(34)&1 == 1 then
state = 2
else
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
states = {
menu = stateMenu(),
game = stateGame()
}
setState(states.menu)
end
function _update60()
gs._update()
-- if not create then
-- board:solveStep()
-- end
-- mouse
mouse_x = stat(32)
mouse_y = stat(33)
end
function _draw()
cls()
gs._draw()
circfill(stat(32), stat(33), 0, 7)
circ(stat(32), stat(33), 1, 0)
end