Files
2026-06-07 22:53:58 +08:00

131 lines
4.2 KiB
Lua

---@type RuntimeUi
local runtime_ui = runtime.import("runtime_ui")
---@type RuntimeLayout
local layout = runtime.import("layout")
local theme = runtime.import("theme")
local styles = runtime.import("styles")
local state = runtime.import("state")
local board = runtime.import("board")
local rules = runtime.import("rules")
local ui = {}
local function color_name(owner)
if owner == "red" then return "" end
if owner == "yellow" then return "" end
if owner == "blue" then return "" end
if owner == "green" then return "绿" end
return owner
end
local function piece_size(piece)
if piece.status == "finished" then
return 30
end
return 34
end
function ui.highlight_updates(ids, enabled)
return runtime_ui.batch_update(ids, enabled and styles.plane_highlight or styles.plane_normal)
end
function ui.piece_position_update(piece_id)
local piece = state.pieces[piece_id]
local pos = rules.piece_position(piece)
return runtime_ui.update(piece_id, {
x = pos.x,
y = pos.y,
width = piece_size(piece),
height = piece_size(piece),
scale = 1,
alpha = 0.95
})
end
function ui.piece_position_updates(ids)
local updates = {}
for _, id in ipairs(ids) do
table.insert(updates, ui.piece_position_update(id))
end
return updates
end
function ui.all_piece_position_updates()
return ui.piece_position_updates(rules.all_piece_ids())
end
function ui.create_board_nodes()
local nodes = {
runtime_ui.panel("board_panel", 28, 82, 664, 610, styles.board),
runtime_ui.panel("center_airport", 252, 292, 168, 136, styles.center),
runtime_ui.text("board_title", theme.title, 268, 318, 140, 32, styles.hud_text),
runtime_ui.text("board_tip", "掷 6 起飞 · 撞子回家 · 全部到达获胜", 206, 360, 320, 28, styles.small_text),
runtime_ui.panel("home_red", 62, 98, 128, 92, styles.home_panel("red")),
runtime_ui.panel("home_yellow", 498, 98, 128, 92, styles.home_panel("yellow")),
runtime_ui.panel("home_blue", 498, 516, 128, 92, styles.home_panel("blue")),
runtime_ui.panel("home_green", 62, 516, 128, 92, styles.home_panel("green")),
runtime_ui.text("label_red", "红方", 96, 102, 60, 24, styles.small_text),
runtime_ui.text("label_yellow", "黄方", 532, 102, 60, 24, styles.small_text),
runtime_ui.text("label_blue", "蓝方", 532, 520, 60, 24, styles.small_text),
runtime_ui.text("label_green", "绿方", 96, 520, 60, 24, styles.small_text)
}
for index, cell in ipairs(board.route) do
runtime_ui.append(nodes, runtime_ui.circle("route_" .. tostring(index), cell.x, cell.y, 24, styles.route_cell))
end
for _, owner in ipairs(state.players) do
local start = board.route[board.start[owner]]
runtime_ui.append(nodes, runtime_ui.text("start_" .. owner, color_name(owner), start.x - 14, start.y - 14, 36, 24, {
fontSize = 14,
color = theme.colors[owner],
layer = styles.layers.cell + 1
}))
end
for id, piece in pairs(state.pieces) do
local pos = rules.piece_position(piece)
runtime_ui.append(nodes, runtime_ui.circle(id, pos.x, pos.y, piece_size(piece), styles.plane(piece.owner)))
end
return nodes
end
function ui.create_ui_nodes()
local hud_items = layout.row("top_bar", {
runtime_ui.text("turn_text", "当前: 红方", 0, 0, 120, 32, styles.hud_text),
runtime_ui.text("dice_text", "骰子: -", 0, 0, 100, 32, styles.hud_text),
runtime_ui.text("status_text", rules.status_text(), 0, 0, 260, 32, styles.hud_text),
layout.item(
runtime_ui.button("dice_button", "掷骰子", 0, 0, 120, 48, "roll_dice", styles.dice_button),
{ marginLeft = 18 }
)
}, {
x = 24,
height = 76,
gap = 16,
align = "center"
})
local nodes = { runtime_ui.panel("top_bar", 0, 0, 720, 76, styles.hud) }
return runtime_ui.append_all(nodes, hud_items)
end
function ui.turn_name(owner)
return "当前: " .. color_name(owner) .. ""
end
function ui.dice_and_status_updates()
return {
runtime_ui.text_update("dice_text", "骰子: " .. tostring(state.dice or "-")),
runtime_ui.text_update("turn_text", ui.turn_name(state.current_player)),
runtime_ui.text_update("status_text", rules.status_text())
}
end
function ui.status_update()
return { runtime_ui.text_update("status_text", rules.status_text()) }
end
return ui