knucklebones/states/game/gameover.lua

110 lines
3.1 KiB
Lua
Raw Normal View History

2023-08-12 11:36:14 +02:00
function state_game_gameover()
local box
local event_bus
local y = 90
local buttons = {
-- {
-- hover = false,
-- label = "Play Again",
-- box = { x = 120 - prn_len("Play Again") / 2, y = y, width = prn_len("Play Again"), height = 10 },
-- cb = function()
-- trace("again")
-- end
-- },
{
hover = false,
label = "Main Menu",
box = { x = 120 - prn_len("Main Menu") / 2, y = y + 15, width = prn_len("Main Menu"), height = 10 },
cb = function()
reset()
end
}
}
local function open_box()
while box.w < 180 do
box.w = box.w * 1.1
waitframes(1)
end
if box.w > 180 then
box.w = 180
end
while box.h < 20 do
box.h = box.h + 1
waitframes(1)
end
end
local function show_buttons()
-- show a button "again", and another "main menu"
-- right under the box
for _, button in ipairs(buttons) do
button.hover = false
-- rect(button.box.x - 1, button.box.y - 1, button.box.width + 2, button.box.height + 2, 2)
-- Hover button
if mouse_in_rect(button.box) then
button.hover = true
-- Click button
if Input:mouse_released()
then
button.cb()
end
end
prn_border(button.label,
button.box.x, button.box.y,
button.hover and 12 or 13,
ALIGN.Left,
0)
end
end
local function update(self)
-- draw box
rect(120 - box.w / 2, 67 - box.h / 2, box.w, box.h, 0)
rectb(120 - box.w / 2, 67 - box.h / 2, box.w, box.h, 15)
clip(120 - box.w / 2, 67 - box.h / 2, box.w, box.h)
-- print who won according to score
local text
local color
local score_player = get_total_score(SCORES.player)
local score_enemy = get_total_score(SCORES.enemy)
if score_player > score_enemy then
color = COLOR_PLAYER
text = "YOU WON " .. score_player .. " - " .. score_enemy
elseif score_player < score_enemy then
color = COLOR_ENEMY
text = "YOU LOST " .. score_player .. " - " .. score_enemy
else
color = 14
text = "DRAW " .. score_player .. " - " .. score_enemy
end
prn_border(text, 120, 67 - 3, 12, ALIGN.Center, color)
clip()
show_buttons()
end
---comment
---@param self any
---@param die Die
---@param _event_bus EventBus
---@param turn "player"|"enemy"
local function _enter(self, die, _event_bus, turn)
assert(_event_bus, "event_bus is nil")
assert(turn, "turn is nil")
event_bus = _event_bus
event_bus:emit(EVENT_REMOVE_DIE)
box = {
h = 0, w = 1
}
addcoroutine(open_box)
end
return { update = update, _enter = _enter }
end