pico8-0hh1/main.lua

185 lines
2.9 KiB
Lua
Raw Normal View History

2022-05-29 18:38:07 +02:00
poke(0x5F2D, 1)
2022-05-28 17:32:43 +02:00
2022-05-28 11:44:09 +02:00
--
-- constants
--
local BLUE = 1
local YELLOW = 2
--
-- Utils
--
function idx_xy(idx, width)
2022-05-28 12:34:08 +02:00
return (idx - 1) % width + 1, (idx - 1) \ width + 1
2022-05-28 11:44:09 +02:00
end
function xy_idx(x, y, width)
2022-05-28 12:34:08 +02:00
return ((y - 1) * width) + x
2022-05-28 11:44:09 +02:00
end
function map(tbl, f)
2022-05-28 12:34:08 +02:00
local t = {}
for k, v in pairs(tbl) do
t[k] = f(v)
end
return t
2022-05-28 11:44:09 +02:00
end
function filter(tbl, f, keepindex)
2022-05-28 12:34:08 +02:00
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
2022-05-28 11:44:09 +02:00
end
function slice(tbl, first, last, step)
2022-05-28 12:34:08 +02:00
local sliced = {}
for i = (first or 1), (last or #tbl), (step or 1) do
2022-05-29 14:35:28 +02:00
sliced[#sliced + 1] = tbl[i]
2022-05-28 12:34:08 +02:00
end
return sliced
2022-05-28 11:44:09 +02:00
end
local _rectfill = rectfill
function rectfill(x, y, w, h, col)
2022-05-28 12:34:08 +02:00
_rectfill(x, y, x+w, y+h, col)
2022-05-28 11:44:09 +02:00
end
2022-05-29 14:35:28 +02:00
--
-- 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
2022-05-28 11:44:09 +02:00
2022-05-29 14:35:28 +02:00
--
-- Returns the indices of found occurences of `o` within `tbl`
--
2022-05-28 15:02:55 +02:00
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
2022-05-29 14:35:28 +02:00
--
-- Makes a shallow table copy
--
2022-05-28 15:02:55 +02:00
function copy(tbl)
2022-05-29 14:35:28 +02:00
return map(tbl, function (o) return o end)
2022-05-28 15:02:55 +02:00
end
2022-05-29 14:35:28 +02:00
--
-- Table equality - shallow comparison
--
2022-05-28 15:02:55 +02:00
function equal(tbl1, tbl2)
for k, _ in ipairs(tbl1) do
if tbl1[k] ~= tbl2[k] then return false end
end
return true
end
2022-05-29 14:35:28 +02:00
-- 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
2022-05-28 23:58:00 +02:00
2022-05-29 18:38:07 +02:00
function makeButton(x, y, w, h, text, f)
local state = 0 -- 0 = normal, 1 = hovered, 2 = pressed
2022-05-28 11:44:09 +02:00
return {
2022-05-29 18:38:07 +02:00
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()
2022-05-28 17:32:43 +02:00
end
2022-05-29 18:38:07 +02:00
if stat(34)&1 == 1 then
state = 2
2022-05-28 17:32:43 +02:00
else
2022-05-29 18:38:07 +02:00
state = 1
2022-05-28 15:02:55 +02:00
end
2022-05-29 18:38:07 +02:00
else
state = 0
2022-05-28 11:44:09 +02:00
end
end
}
end
2022-05-29 18:38:07 +02:00
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
2022-05-28 11:44:09 +02:00
--
-- main loop
--
2022-05-28 23:58:00 +02:00
local create = true
2022-05-28 11:44:09 +02:00
function _init()
2022-05-28 23:58:00 +02:00
cls()
printh(" ")
printh(" ")
2022-05-29 18:38:07 +02:00
mouse_x = 0
mouse_y = 0
2022-05-28 23:58:00 +02:00
2022-05-29 18:38:07 +02:00
states = {
menu = stateMenu(),
game = stateGame()
}
2022-05-28 23:58:00 +02:00
2022-05-29 18:38:07 +02:00
setState(states.menu)
2022-05-28 23:58:00 +02:00
2022-05-28 11:44:09 +02:00
end
2022-05-28 17:32:43 +02:00
function _update60()
2022-05-29 18:38:07 +02:00
gs._update()
2022-05-29 14:35:28 +02:00
-- if not create then
-- board:solveStep()
-- end
2022-05-29 18:38:07 +02:00
-- mouse
mouse_x = stat(32)
mouse_y = stat(33)
2022-05-28 11:44:09 +02:00
end
function _draw()
2022-05-28 12:34:08 +02:00
cls()
2022-05-29 18:38:07 +02:00
gs._draw()
2022-05-28 17:32:43 +02:00
circfill(stat(32), stat(33), 0, 7)
circ(stat(32), stat(33), 1, 0)
2022-05-28 11:44:09 +02:00
end