Skip to content

Commit

Permalink
feat(client): bone check
Browse files Browse the repository at this point in the history
Support the 'bones' field for options.
Takes a string, or array of strings, with the bone names.
Returns the boneId as the last argument for canInteract.
  • Loading branch information
thelindat committed Oct 1, 2022
1 parent b2f37e2 commit 29814f7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
18 changes: 10 additions & 8 deletions client/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ox_target:addGlobalVehicle({
name = 'ox_target:driverF',
icon = 'fa-solid fa-car-side',
label = 'Toggle front driver door',
bones = { 'door_dside_f', 'seat_dside_f' },
canInteract = function(entity, distance, coords, name)
if GetVehicleDoorLockStatus(entity) > 1 then return end

Expand All @@ -39,6 +40,7 @@ ox_target:addGlobalVehicle({
name = 'ox_target:passengerF',
icon = 'fa-solid fa-car-side',
label = 'Toggle front passenger door',
bones = { 'door_pside_f', 'seat_pside_f' },
canInteract = function(entity, distance, coords, name)
if GetVehicleDoorLockStatus(entity) > 1 then return end

Expand All @@ -59,6 +61,7 @@ ox_target:addGlobalVehicle({
name = 'ox_target:driverR',
icon = 'fa-solid fa-car-side',
label = 'Toggle rear driver door',
bones = { 'door_dside_r', 'seat_dside_r' },
canInteract = function(entity, distance, coords, name)
if GetVehicleDoorLockStatus(entity) > 1 then return end

Expand All @@ -79,6 +82,7 @@ ox_target:addGlobalVehicle({
name = 'ox_target:passengerR',
icon = 'fa-solid fa-car-side',
label = 'Toggle rear passenger door',
bones = { 'door_pside_r', 'seat_pside_r' },
canInteract = function(entity, distance, coords, name)
if GetVehicleDoorLockStatus(entity) > 1 then return end

Expand All @@ -100,11 +104,10 @@ ox_target:addGlobalVehicle({
name = 'ox_target:bonnet',
icon = 'fa-solid fa-car',
label = 'Toggle hood',
canInteract = function(entity, distance, coords, name)
bones = 'bonnet',
canInteract = function(entity, distance, coords, name, boneId)
if GetVehicleDoorLockStatus(entity) > 1 then return end

local boneId = GetEntityBoneIndexByName(entity, 'bonnet')
return boneId ~= -1 and #(coords - GetWorldPositionOfEntityBone(entity, boneId)) < 0.9
return #(coords - GetWorldPositionOfEntityBone(entity, boneId)) < 0.9
end,
onSelect = function(data)
toggleDoor(data.entity, 4)
Expand All @@ -117,11 +120,10 @@ ox_target:addGlobalVehicle({
name = 'ox_target:trunk',
icon = 'fa-solid fa-car-rear',
label = 'Toggle trunk',
canInteract = function(entity, distance, coords, name)
bones = 'boot',
canInteract = function(entity, distance, coords, name, boneId)
if GetVehicleDoorLockStatus(entity) > 1 then return end

local boneId = GetEntityBoneIndexByName(entity, 'boot')
return boneId ~= -1 and #(coords - GetWorldPositionOfEntityBone(entity, boneId)) < 0.9
return #(coords - GetWorldPositionOfEntityBone(entity, boneId)) < 0.9
end,
onSelect = function(data)
toggleDoor(data.entity, 5)
Expand Down
43 changes: 41 additions & 2 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ local SendNuiMessage = SendNuiMessage
local GetCurrentZone = GetCurrentZone
local PlayerHasGroups = PlayerHasGroups or function() return true end
local PlayerHasItems = PlayerHasItems or function() return true end
local GetEntityBoneIndexByName = GetEntityBoneIndexByName
local GetWorldPositionOfEntityBone = GetWorldPositionOfEntityBone
local GetEntityModel = GetEntityModel
local GetEntityOptions = GetEntityOptions
local IsDisabledControlJustPressed = IsDisabledControlJustPressed
Expand Down Expand Up @@ -142,8 +144,45 @@ local function enableTargeting()
hide = true
end

local bone = option.bones

if bone then
local _type = type(bone)

if _type == 'string' then
local boneId = GetEntityBoneIndexByName(entityHit, bone)

if boneId ~= -1 then
bone = boneId
else
hide = true
end
elseif _type == 'table' then
local closestBone, boneDistance

for j = 1, #bone do
local boneId = GetEntityBoneIndexByName(entityHit, bone[j])

if boneId ~= -1 then
local dist = #(endCoords - GetWorldPositionOfEntityBone(entityHit, boneId))

if not closestBone or dist < boneDistance then
closestBone = boneId
boneDistance = dist
end
end
end

if closestBone then
bone = closestBone
else
hide = true
end
end
end

if not hide and option.canInteract then
hide = not option.canInteract(entityHit, distance, endCoords, option.name)
hide = not option.canInteract(entityHit, distance, endCoords, option.name, bone)
end

if not newOptions and v[i].hide ~= hide then
Expand All @@ -169,7 +208,7 @@ local function enableTargeting()
end
end

for i = 1, 20 do
for i = 1, 15 do
if not isActive then break end

if Debug then
Expand Down

0 comments on commit 29814f7

Please sign in to comment.