-- client.lua
local playerCards = {} -- 存储玩家手牌
local isGameStarted = false -- 游戏是否开始的标志
local currentPlayer = nil -- 当前出牌玩家
local landlord = nil -- 地主
-- 显示手牌
local function showCards()
SendNUIMessage({
type = "updateHandCards",
cards = playerCards
})
end
-- 更新游戏状态
local function updateGameStatus(status)
SendNUIMessage({
type = "updateGameStatus",
status = status
})
end
-- 更新当前出牌玩家
local function updateCurrentPlayer(player)
SendNUIMessage({
type = "updateCurrentPlayer",
player = player
})
end
-- 更新上家出的牌
local function updateLastPlayedCards(cards)
SendNUIMessage({
type = "updateLastPlayedCards",
cards = cards
})
end
-- 游戏开始事件
RegisterNetEvent("doudizhu:startGameClient")
AddEventHandler("doudizhu:startGameClient", function(cards)
playerCards = cards -- 获取到的手牌
isGameStarted = true
showCards() -- 显示手牌
updateGameStatus("游戏进行中...") -- 更新游戏状态
end)
-- 设置地主事件
RegisterNetEvent("doudizhu:setLandlord")
AddEventHandler("doudizhu:setLandlord", function(landlordId)
landlord = landlordId -- 设置地主
if landlordId == GetPlayerServerId(PlayerId()) then
updateGameStatus("你是地主!") -- 自己是地主时显示信息
else
updateGameStatus("玩家 " .. landlordId .. " 是地主。") -- 其他玩家是地主
end
end)
-- 设置当前出牌玩家事件
RegisterNetEvent("doudizhu:setCurrentPlayer")
AddEventHandler("doudizhu:setCurrentPlayer", function(playerId)
currentPlayer = playerId -- 更新当前玩家
if playerId == GetPlayerServerId(PlayerId()) then
updateCurrentPlayer("你") -- 当前玩家是自己
else
updateCurrentPlayer("玩家 " .. playerId) -- 当前玩家是其他人
end
end)
-- 游戏结束事件
RegisterNetEvent("doudizhu:gameOver")
AddEventHandler("doudizhu:gameOver", function(winnerId)
if winnerId == GetPlayerServerId(PlayerId()) then
updateGameStatus("你赢了!") -- 自己赢了
else
updateGameStatus("玩家 " .. winnerId .. " 赢了!") -- 其他玩家赢了
end
isGameStarted = false -- 游戏结束
end)
-- 处理出牌请求
RegisterNUICallback("playCards", function(data, cb)
if isGameStarted and currentPlayer == GetPlayerServerId(PlayerId()) then
TriggerServerEvent("doudizhu:playCards", data.cards) -- 发起出牌请求
else
print("现在不是你的回合!") -- 出牌不在自己的回合
end
cb("ok")
end)
-- 处理超时请求
RegisterNUICallback("timeout", function(data, cb)
if isGameStarted and currentPlayer == GetPlayerServerId(PlayerId()) then
TriggerServerEvent("doudizhu:timeout") -- 通知服务器超时
end
cb("ok")
end)
-- 处理提示出牌请求
RegisterNUICallback("hintCards", function(data, cb)
if isGameStarted and currentPlayer == GetPlayerServerId(PlayerId()) then
TriggerServerEvent("doudizhu:hintCards") -- 请求服务器提供出牌提示
end
cb("ok")
end)
local isSitting = false -- 玩家是否坐下的标志
-- 玩家坐下的函数
local function sitAtTable()
local playerPed = PlayerPedId() -- 获取玩家角色
local tableCoords = vector3(0.0, 0.0, 70.0) -- 桌子的坐标
TaskStartScenarioAtPosition(playerPed, "PROP_HUMAN_SEAT_CHAIR_MP_PLAYER", tableCoords.x, tableCoords.y, tableCoords.z, 0.0, 0, true, true)
isSitting = true -- 标记为已坐下
SendNUIMessage({ type = "showUI" }) -- 显示 UI
end
-- 玩家离开的函数
local function leaveTable()
local playerPed = PlayerPedId()
ClearPedTasks(playerPed) -- 清除当前任务
isSitting = false -- 标记为未坐下
SendNUIMessage({ type = "hideUI" }) -- 隐藏 UI
end
-- 检测玩家是否靠近桌子
local function checkNearTable()
local playerCoords = GetEntityCoords(PlayerPedId())
local tableCoords = vector3(0.0, 0.0, 70.0) -- 桌子的坐标
if #(playerCoords - tableCoords) < 2.0 then
return true -- 玩家在桌子附近
end
return false -- 玩家不在桌子附近
end
-- 按 E 键坐下或离开
Citizen.CreateThread(function()
while true do
Citizen.Wait(0) -- 每帧检查
if checkNearTable() then
if not isSitting then
DrawText3D(tableCoords.x, tableCoords.y, tableCoords.z, "按 E 坐下") -- 提示坐下
if IsControlJustReleased(0, 38) then -- 38 是 E 键
sitAtTable() -- 执行坐下操作
end
else
DrawText3D(tableCoords.x, tableCoords.y, tableCoords.z, "按 E 离开") -- 提示离开
if IsControlJustReleased(0, 38) then -- 38 是 E 键
leaveTable() -- 执行离开操作
end
end
end
end
end)
-- 显示 3D 文字的函数
function DrawText3D(x, y, z, text)
local onScreen, _x, _y = World3dToScreen2d(x, y, z)
if onScreen then
SetTextScale(0.35, 0.35)
SetTextFont(4)
SetTextProportional(1)
SetTextColour(255, 255, 255, 215)
SetTextEntry("STRING")
SetTextCentre(1)
AddTextComponentString(text)
DrawText(_x, _y) -- 在屏幕上绘制文字
end
end
-- 游戏结束事件再注册一次
RegisterNetEvent("doudizhu:gameOver")
AddEventHandler("doudizhu:gameOver", function(winnerId)
if winnerId == GetPlayerServerId(PlayerId()) then
print("你赢了!") -- 打印自己赢了的信息
else
print("玩家 " .. winnerId .. " 赢了!") -- 打印其他玩家赢了的信息
end
isSitting = false -- 游戏结束后标记未坐下
SendNUIMessage({ type = "hideUI" }) -- 隐藏 UI
end)