local sitAnim = {
sit = { "amb@world_human_sunbathe@male@base", "base" },
lie = { "amb@world_human_sunbathe@female@base", "base" }
}
local keyToSit = 38 -- E键
local isSitting = false
local cooldownTime = 5000 -- 动画冷却时间(毫秒)
local lastToggleTime = 0
-- 提示系统
function ShowHint(msg)
BeginTextCommandDisplayHelp("STRING")
AddTextComponentSubstringPlayerName(msg)
EndTextCommandDisplayHelp(0, false, true, -1)
end
-- 隐藏提示信息
function HideHelpText()
ClearAllHelpMessages()
end
-- 检查玩家是否在特定物体附近
function IsNearFurniture()
local playerPed = PlayerPedId()
local playerCoords = GetEntityCoords(playerPed)
-- 所有可以坐下/躺下的对象模型
local furnitureObjects = {
{ model = "prop_bench_01", distance = 2.0 }, -- 长椅
{ model = "prop_sofa_01", distance = 2.0 }, -- 沙发
{ model = "prop_table_02", distance = 2.0 }, -- 桌子
{ model = "p_amb_bench_01", distance = 2.0 }, -- 公园长椅
{ model = "prop_chair_01a", distance = 2.0 }, -- 椅子
{ model = "prop_chair_02", distance = 2.0 }, -- 椅子
{ model = "p_new_chair_01", distance = 2.0 }, -- 新椅子
{ model = "prop_sofa_burnt", distance = 2.0 }, -- 烧毁的沙发
{ model = "prop_couch_01", distance = 2.0 }, -- 沙发
{ model = "prop_lounge_chair", distance = 2.0 }, -- 躺椅
{ model = "prop_office_chair", distance = 2.0 }, -- 办公椅
{ model = "prop_patio_chair", distance = 2.0 }, -- 户外椅子
}
for _, obj in ipairs(furnitureObjects) do
local objHandle = GetClosestObjectOfType(playerCoords, obj.distance, GetHashKey(obj.model), false)
if objHandle ~= 0 then
local objCoords = GetEntityCoords(objHandle)
if Vdist(playerCoords, objCoords) < obj.distance then
return true
end
end
end
return false
end
-- 坐下或躺下的函数
function ToggleSitLay(action)
local playerPed = PlayerPedId()
local currentTime = GetGameTimer()
if currentTime - lastToggleTime < cooldownTime then
return -- 冷却时间内不执行
end
if action == 'sit' and not isSitting and IsNearFurniture() then
TaskPlayAnim(playerPed, sitAnim.sit, sitAnim.sit, 1.0, -1.0, -1, 0, 0, false, false, false)
isSitting = true
elseif action == 'lie' and isSitting and IsNearFurniture() then
TaskPlayAnim(playerPed, sitAnim.lie, sitAnim.lie, 1.0, -1.0, -1, 0, 0, false, false, false)
isSitting = false
end
lastToggleTime = currentTime -- 更新最后执行时间
end
-- 设置目标和提示显示
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
if IsNearFurniture() then
-- 显示提示信息
ShowHint("按 E 键切换坐下/躺下")
-- 添加 ox_target 的目标
exports.ox_target:AddTargetEntity(PlayerPedId(), {
options = {
{
event = 'sitlay:toggle',
icon = 'fas fa-chair',
label = '坐下/躺下',
}
},
distance = 2.0
})
-- 检测键盘输入
if IsControlJustReleased(0, keyToSit) then
ToggleSitLay(isSitting and 'lie' or 'sit')
end
else
HideHelpText()
end
end
end)
RegisterNetEvent('sitlay:toggle', function()
ToggleSitLay(isSitting and 'lie' or 'sit')
end)