knucklebones/states/state_main_menu.lua

131 lines
3.4 KiB
Lua
Raw Normal View History

2023-08-12 11:36:14 +02:00
function state_main_menu()
local covering = {}
local falling = {}
local states
local buttons = {
{
hover = false,
label = "Play",
box = { x = 120 - prn_len("Play") / 2, y = 100, w = prn_len("Play"), h = 10 },
cb = function()
trace("play")
for i = 0, 240, 16 do
for j = 0, 135, 16 do
table.insert(covering, Die:new({
value = math.random(1, 6),
angle = 360,
x = i,
y = j,
scale = 0
}))
end
end
end
}
}
local function show_transition()
-- slightly scale up the transition dice
for i, die in ipairs(covering) do
die.scale = die.scale + 0.03 / i * 80
die.angle = die.angle * 0.90
if die.scale > 1 then
die.scale = 1
end
swap_color(12, 15)
swap_color(0, 15)
die:draw(-1)
end
if table.count(covering, function(d) return d.scale == 1 end) == #covering then
state_manager:change_state(states.game, { covering = covering })
end
end
local function falling_dice()
local remove = {}
for i, die in ipairs(falling) do
die.y = die.y + 1
die.angle = die.angle + die.rot_speed
if die.y > 160 then
table.insert(remove, i)
end
end
for _, i in ipairs(remove) do
table.remove(falling, i)
end
end
local function draw_dice()
for _, die in ipairs(falling) do
swap_color(12, 15)
swap_color(13, 15)
swap_color(14, 15)
die:draw()
reset_colors(12)
end
end
local function _enter(self, data)
assert(data.states, "needs a states table")
states = data.states
covering = {}
end
local function update()
-- random sign
if math.random() < 0.05 then
local sign = math.random() < 0.5 and -1 or 1
-- Spawn a random die
local die = Die:new({
value = math.random(1, 6),
x = math.random(240),
y = -20,
angle = math.random(10, 40),
rot_speed = sign * math.random(6, 10) / 10,
})
table.insert(falling, die)
end
falling_dice()
cls()
draw_dice()
-- prn_border("KNUCKLEBONES", 118, 10, 1, ALIGN.Center, 0, 2, 0)
-- prn_border("KNUCKLEBONES", 122, 10, 2, ALIGN.Center, 0, 2, 1)
prn_border("KNUCKLEBONES", 120, 10, 12, ALIGN.Center, 0, 2)
local l = prn_len("A game of risk and reward")
local x = 120 - l / 2
local y = 50
x = x + prn_border("A game of ", x, y, 13, ALIGN.Left, 0)
x = x + prn_border_floaty("risk ", x, y + 5, 12, ALIGN.Left, 1, 1, 20)
x = x + prn_border("and ", x, y, 13, ALIGN.Left, 0)
x = x + prn_border_floaty("reward", x, y - 5, 12, ALIGN.Left, 8)
print_border("Originally from \"The Cult of the Lamb\"", 104, 130, 14, 0, false, 1, true)
local mx, my = mouse()
for i, button in ipairs(buttons) do
-- Print button
local x, y, w, h = button.box.x, button.box.y, button.box.w, button.box.h
local hover = mx >= x and mx <= x + w and my >= y and my <= y + h
button.hover = hover
local color = hover and 12 or 13
-- rect(x, y, w, h, color)
prn_border(button.label, x, y, color, ALIGN.Left, 0)
-- Click button
if hover and Input:mouse_pressed()
then
button.cb()
end
end
if #covering > 0 then
show_transition()
end
end
return { update = update, _enter = _enter }
end