local sitAnim = {
sit = { "amb@world_human_sunbathe@male@base", "base" },
lie = { "amb@world_human_sunbathe@female@base", "base" }
}
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 furnitureModels = {
"prop_bench_01",
"prop_sofa_01",
"p_new_chair_01",
"prop_chair_01a",
"prop_chair_02",
"prop_table_02",
"p_amb_bench_01",
"prop_lounge_chair",
"prop_office_chair",
"prop_patio_chair"
-- 可以根据需要添加更多模型
}
for _, model in ipairs(furnitureModels) do
local objHandle = GetClosestObjectOfType(playerCoords, 2.0, GetHashKey(model), false)
if objHandle ~= 0 then
return true -- 如果找到适合的座位
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 then
TaskPlayAnim(playerPed, sitAnim.sit[1], sitAnim.sit[2], 1.0, -1.0, -1, 0, 0, false, false, false)
isSitting = true
TriggerEvent("notification", "你坐下了!")
elseif action == 'lie' and isSitting then
TaskPlayAnim(playerPed, sitAnim.lie[1], sitAnim.lie[2], 1.0, -1.0, -1, 0, 0, false, false, false)
isSitting = false
TriggerEvent("notification", "你躺下了!")
end
lastToggleTime = currentTime -- 更新最后执行时间
end
-- 使用 ox_target 显示目标
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
if IsNearFurniture() then
ShowHint("按 E 键切换坐下/躺下") -- 显示提示信息
exports.ox_target:AddTargetEntity(PlayerPedId(), {
options = {
{
event = 'sitlay:toggle',
icon = 'fas fa-chair',
label = '坐下/躺下',
}
},
distance = 2.0
})
if IsControlJustReleased(0, 38) then -- E键
ToggleSitLay(isSitting and 'lie' or 'sit')
end
else
HideHelpText()
end
end
end)
RegisterNetEvent('sitlay:toggle', function()
ToggleSitLay(isSitting and 'lie' or 'sit')
end)
RegisterNetEvent("notification", function(text)
ShowHint(text)
end)