knucklebones/board.lua
2023-08-12 11:36:14 +02:00

128 lines
3.9 KiB
Lua

---@diagnostic disable: lowercase-global
include "scoring"
local cell_width = 22
local cell_height = 18
function draw_board()
local players = { "player", "enemy" }
for _, player in ipairs(players) do
-- columns
for col = 1, 3 do
local dice_col = PLACED_DICE[player][col]
for row = 1, 3 do
local pos = get_cell_coords(player, col, row)
-- cell background
rectb(pos.x, pos.y, cell_width, cell_height, 15)
-- dice
local value = dice_col[row]
local combo = table.count(dice_col, value)
if value > 0 then
swap_color(8, 0)
if combo == 2 then
swap_color(12, 4)
swap_color(13, 3)
elseif combo == 3 then
swap_color(12, 3)
swap_color(13, 2)
end
-- dice sprite
spr(die_spr[value], pos.x + 3, pos.y + 1, 0, 1, 0, 0, 2, 2)
reset_colors(8, 12, 13)
end
end
end
end
end
function draw_dice_boxes()
-- small trick to make the corners look better
swap_color(3, 0)
-- player
swap_color(12, COLOR_PLAYER) -- bones color
local r = get_dice_box("player")
draw9box(51, 52, r.x - 4, r.y - 4, r.width + 8, r.height + 8)
-- enemy
swap_color(12, COLOR_ENEMY) -- bones color
r = get_dice_box("enemy")
draw9box(51, 52, r.x - 4, r.y - 4, r.width + 8, r.height + 8)
-- reset colors
swap_color(3, 3)
swap_color(12, 12)
end
function print_names()
prn_border("You", 1, 50, 12, ALIGN.Left, COLOR_PLAYER)
prn_border("Opponent", 239, 135-50-8, 12, ALIGN.Right, COLOR_ENEMY)
end
function print_scores()
local players = { "player", "enemy" }
local sums = {}
for _, player in ipairs(players) do
local scores = SCORES[player]
local y
for col = 1, 3 do
local value = scores[col]
local pos = get_cell_coords(player, col, 1)
pos.y = pos.y + (player == "player" and -9 or 20)
y = pos.y
pos.x = pos.x + 11
prn_border(value, pos.x, pos.y, 12, ALIGN.Center)
end
-- sums
local sum = get_total_score(scores)
local x = player == "player" and 12 or 240 - 12
local align = player == "player" and ALIGN.Left or ALIGN.Right
table.insert(sums, {
sum = sum,
x = x,
y = y,
align = align,
bg = player == "player" and COLOR_PLAYER or COLOR_ENEMY
})
end
for _, sum in ipairs(sums) do
prn_border(sum.sum, sum.x, sum.y, 12, sum.align, sum.bg)
end
end
---comment
---@param player 'player'|'enemy'
---@param col number 1-3
---@param row number 1-3
---@return table
function get_cell_coords(player, col, row)
row = row - 1 -- fix 1-based index for y, makes it easier to calculate coords
local p = 14 -- padding
local cols = { 120 - cell_width - p, 120 - (cell_width / 2), 120 + p }
if player == "enemy" then
row = row == 0 and 2 or row == 2 and 0 or 1 -- "invert" the board for the enemy
return { x = cols[col], y = (cell_height * row + (row * 1)) + 1 }
else
return { x = cols[col], y = (cell_height * row + (row * 1)) + 79 }
end
end
--- @param player 'player'|'enemy'
--- @param column 1|2|3
function get_column_rect(player, column)
local c = get_cell_coords(player, column, player == "player" and 1 or 3)
return { x = c.x - 1, y = c.y - 1, width = cell_width + 2, height = cell_height * 3 + 4 }
end
--- @param player 'player'|'enemy'
function get_dice_box(player)
if player == "player" then
return { x = 10, y = 89, width = 60, height = 36 }
else
return { x = 170, y = 9, width = 60, height = 36 }
end
end