-- 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("It's not your turn!")
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)
|