Initial flame_lua_runtime package
This commit is contained in:
153
example/assets/games/flight/scripts/rules.lua
Normal file
153
example/assets/games/flight/scripts/rules.lua
Normal file
@@ -0,0 +1,153 @@
|
||||
local state = runtime.import("state")
|
||||
local board = runtime.import("board")
|
||||
|
||||
local rules = {}
|
||||
|
||||
local function piece_index(piece_id)
|
||||
return piece_id:sub(-1) == "2" and 2 or 1
|
||||
end
|
||||
|
||||
function rules.next_player()
|
||||
for index, player in ipairs(state.players) do
|
||||
if player == state.current_player then
|
||||
local next_index = index + 1
|
||||
if next_index > #state.players then
|
||||
next_index = 1
|
||||
end
|
||||
return state.players[next_index]
|
||||
end
|
||||
end
|
||||
return state.players[1]
|
||||
end
|
||||
|
||||
function rules.next_dice()
|
||||
state.dice_index = state.dice_index + 1
|
||||
if state.dice_index > #state.dice_values then
|
||||
state.dice_index = 1
|
||||
end
|
||||
return state.dice_values[state.dice_index]
|
||||
end
|
||||
|
||||
function rules.can_move(piece, dice)
|
||||
if piece == nil or piece.status == "finished" then
|
||||
return false
|
||||
end
|
||||
if piece.status == "home" then
|
||||
return dice == 6
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function rules.movable_pieces()
|
||||
local result = {}
|
||||
for id, piece in pairs(state.pieces) do
|
||||
if piece.owner == state.current_player and rules.can_move(piece, state.dice) then
|
||||
table.insert(result, id)
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function rules.all_piece_ids()
|
||||
local result = {}
|
||||
for id, _ in pairs(state.pieces) do
|
||||
table.insert(result, id)
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function rules.piece_position(piece)
|
||||
return board.position(piece)
|
||||
end
|
||||
|
||||
function rules.plan_move(piece, dice)
|
||||
local path = {}
|
||||
local final_status = "route"
|
||||
local final_progress = piece.progress
|
||||
|
||||
if piece.status == "home" then
|
||||
final_progress = 1
|
||||
table.insert(path, board.route_position(piece.owner, final_progress))
|
||||
else
|
||||
for step = 1, dice do
|
||||
local progress = piece.progress + step
|
||||
if progress >= board.finish_progress then
|
||||
final_status = "finished"
|
||||
final_progress = board.finish_progress
|
||||
table.insert(path, board.finish_position(piece))
|
||||
break
|
||||
end
|
||||
table.insert(path, board.route_position(piece.owner, progress))
|
||||
final_progress = progress
|
||||
end
|
||||
end
|
||||
|
||||
local captures = {}
|
||||
if final_status == "route" then
|
||||
local landing_index = board.route_index(piece.owner, final_progress)
|
||||
for id, other in pairs(state.pieces) do
|
||||
if id ~= piece.id and other.owner ~= piece.owner and other.status == "route" then
|
||||
if board.route_index(other.owner, other.progress) == landing_index then
|
||||
table.insert(captures, id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
piece_id = piece.id,
|
||||
final_status = final_status,
|
||||
final_progress = final_progress,
|
||||
path = path,
|
||||
captures = captures
|
||||
}
|
||||
end
|
||||
|
||||
function rules.commit_move(plan)
|
||||
local piece = state.pieces[plan.piece_id]
|
||||
piece.status = plan.final_status
|
||||
piece.progress = plan.final_progress
|
||||
|
||||
for _, captured_id in ipairs(plan.captures) do
|
||||
local captured = state.pieces[captured_id]
|
||||
captured.status = "home"
|
||||
captured.progress = 0
|
||||
end
|
||||
end
|
||||
|
||||
function rules.player_finished(owner)
|
||||
for _, piece in pairs(state.pieces) do
|
||||
if piece.owner == owner and piece.status ~= "finished" then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function rules.status_text()
|
||||
if state.winner ~= nil then
|
||||
return "胜利者: " .. state.winner
|
||||
end
|
||||
if state.phase == "waiting_roll" then
|
||||
return "请 " .. state.current_player .. " 掷骰子"
|
||||
end
|
||||
if state.phase == "waiting_piece" then
|
||||
return "请选择 " .. state.current_player .. " 的飞机"
|
||||
end
|
||||
if state.phase == "animating" then
|
||||
return "飞机移动中..."
|
||||
end
|
||||
return "准备中"
|
||||
end
|
||||
|
||||
function rules.piece_label(piece)
|
||||
if piece.status == "home" then
|
||||
return "待飞"
|
||||
end
|
||||
if piece.status == "finished" then
|
||||
return "到达"
|
||||
end
|
||||
return tostring(piece.progress)
|
||||
end
|
||||
|
||||
return rules
|
||||
Reference in New Issue
Block a user