Improve entity spawning and target behaviors
Refactored client entity spawning to handle missing models and improved coordinate update sensitivity. Removed model assertion from server entity creation. Enhanced resource start event handling for both server and client. Updated stash and target behaviors to support additional properties and better zone handling for entities without spawned objects.
This commit is contained in:
1 parent
dff4622a94
commit
7443695254
5 files changed
+59
-36
No files matched your search
@@ -12,33 +12,35 @@ ClientEntity.Behaviors = Behaviors
|
|||||||
|
|
||||||
local function SpawnEntity(entityData)
|
local function SpawnEntity(entityData)
|
||||||
if entityData.spawned and DoesEntityExist(entityData.spawned) then return end -- Already spawned
|
if entityData.spawned and DoesEntityExist(entityData.spawned) then return end -- Already spawned
|
||||||
local loaded, model = Utility.LoadModel(entityData.model)
|
if entityData.model then
|
||||||
if not loaded then
|
local loaded, model = Utility.LoadModel(entityData.model)
|
||||||
print(string.format("[ClientEntity] Failed to load model %s for entity %s", entityData.model, entityData.id))
|
if not loaded then
|
||||||
return
|
print(string.format("[ClientEntity] Failed to load model %s for entity %s", entityData.model, entityData.id))
|
||||||
end
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local entity = nil
|
local entity = nil
|
||||||
local coords = entityData.coords
|
local coords = entityData.coords
|
||||||
local rotation = entityData.rotation or vector3(0.0, 0.0, entityData.heading or 0.0) -- Default rotation if not provided
|
local rotation = entityData.rotation or vector3(0.0, 0.0, entityData.heading or 0.0) -- Default rotation if not provided
|
||||||
|
|
||||||
if entityData.entityType == 'object' then
|
if entityData.entityType == 'object' then
|
||||||
entity = CreateObject(model, coords.x, coords.y, coords.z, false, false, false)
|
entity = CreateObject(model, coords.x, coords.y, coords.z, false, false, false)
|
||||||
SetEntityRotation(entity, rotation.x, rotation.y, rotation.z, 2, true)
|
SetEntityRotation(entity, rotation.x, rotation.y, rotation.z, 2, true)
|
||||||
elseif entityData.entityType == 'ped' then
|
elseif entityData.entityType == 'ped' then
|
||||||
entity = CreatePed(4, model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false)
|
entity = CreatePed(4, model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false)
|
||||||
elseif entityData.entityType == 'vehicle' then
|
elseif entityData.entityType == 'vehicle' then
|
||||||
entity = CreateVehicle(model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false)
|
entity = CreateVehicle(model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false)
|
||||||
else
|
else
|
||||||
print(string.format("[ClientEntity] Unknown entity type '%s' for entity %s", entityData.entityType, entityData.id))
|
print(string.format("[ClientEntity] Unknown entity type '%s' for entity %s", entityData.entityType, entityData.id))
|
||||||
end
|
end
|
||||||
if entity and model then
|
if entity and model then
|
||||||
entityData.spawned = entity
|
entityData.spawned = entity
|
||||||
SetModelAsNoLongerNeeded(model)
|
SetModelAsNoLongerNeeded(model)
|
||||||
SetEntityAsMissionEntity(entity, true, true)
|
SetEntityAsMissionEntity(entity, true, true)
|
||||||
FreezeEntityPosition(entity, true)
|
FreezeEntityPosition(entity, true)
|
||||||
else
|
else
|
||||||
SetModelAsNoLongerNeeded(model)
|
SetModelAsNoLongerNeeded(model)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if entityData.OnSpawn then
|
if entityData.OnSpawn then
|
||||||
pcall(function (...)
|
pcall(function (...)
|
||||||
@@ -78,9 +80,8 @@ local function UpdateEntity(_entityData)
|
|||||||
local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z)
|
local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z)
|
||||||
entityData.oldCoords = vector3(entityData.oldCoords.x, entityData.oldCoords.y, entityData.oldCoords.z)
|
entityData.oldCoords = vector3(entityData.oldCoords.x, entityData.oldCoords.y, entityData.oldCoords.z)
|
||||||
local dist = #(coords.xyz - entityData.oldCoords.xyz)
|
local dist = #(coords.xyz - entityData.oldCoords.xyz)
|
||||||
if dist > 1.0 then
|
if dist > 0.5 then
|
||||||
if entityData.spawned then
|
if entityData.spawned then
|
||||||
print("Moving entity:", entityData.id)
|
|
||||||
SetEntityCoords(entityData.spawned, coords.x, coords.y, coords.z, false, false, false, true)
|
SetEntityCoords(entityData.spawned, coords.x, coords.y, coords.z, false, false, false, true)
|
||||||
end
|
end
|
||||||
-- ClientEntity.UpdateCoords(entityData.id, entityData.coords)
|
-- ClientEntity.UpdateCoords(entityData.id, entityData.coords)
|
||||||
@@ -243,6 +244,7 @@ end)
|
|||||||
|
|
||||||
RegisterNetEvent("community_bridge:client:UpdateEntity", function(id, data)
|
RegisterNetEvent("community_bridge:client:UpdateEntity", function(id, data)
|
||||||
for k, v in pairs(data) do
|
for k, v in pairs(data) do
|
||||||
|
print(json.encode({k, v}))
|
||||||
ClientEntity.Set(id, k, v)
|
ClientEntity.Set(id, k, v)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -14,8 +14,6 @@ local Invoked = {}
|
|||||||
-- @param meta table Optional additional data
|
-- @param meta table Optional additional data
|
||||||
-- @return table The created entity data
|
-- @return table The created entity data
|
||||||
function ServerEntity.New(id, entityType, model, coords, rotation, args)
|
function ServerEntity.New(id, entityType, model, coords, rotation, args)
|
||||||
assert(entityType, "EntityType is required")
|
|
||||||
assert(model, "Model is required for entity creation")
|
|
||||||
assert(coords, "Coords are required for entity creation")
|
assert(coords, "Coords are required for entity creation")
|
||||||
local self = args or {}
|
local self = args or {}
|
||||||
self.id = id or Ids.CreateUniqueId(Entities)
|
self.id = id or Ids.CreateUniqueId(Entities)
|
||||||
|
|||||||
@@ -104,8 +104,18 @@ AddEventHandler("onResourceStop", function(resourceName)
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
AddEventHandler("onResourceStart", function(resourceName)
|
if IsDuplicityVersion() then
|
||||||
Behaviors.Init()
|
RegisterNetEvent("onResourceStart", function(resourceName)
|
||||||
end)
|
if resourceName ~= GetCurrentResourceName() then return end
|
||||||
|
Wait(500)
|
||||||
|
Behaviors.Init()
|
||||||
|
end)
|
||||||
|
else
|
||||||
|
RegisterNetEvent("onClientResourceStart", function(resource)
|
||||||
|
if resource ~= GetCurrentResourceName() then return end
|
||||||
|
Wait(500)
|
||||||
|
Behaviors.Init()
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
return Behaviors
|
return Behaviors
|
||||||
@@ -25,6 +25,8 @@ function Stash.OnCreate(entityData)
|
|||||||
entityData.targets['stash'] = {
|
entityData.targets['stash'] = {
|
||||||
label = entityData.stash.target.label or Stash.default.target.label,
|
label = entityData.stash.target.label or Stash.default.target.label,
|
||||||
icon = entityData.stash.target.icon or Stash.default.target.icon,
|
icon = entityData.stash.target.icon or Stash.default.target.icon,
|
||||||
|
description = entityData.stash.target.description,
|
||||||
|
groups = entityData.stash.target.groups,
|
||||||
canInteract = function(_)
|
canInteract = function(_)
|
||||||
local entData = Bridge.Entity.Get(entityData.id)
|
local entData = Bridge.Entity.Get(entityData.id)
|
||||||
return not entData.stash.disable
|
return not entData.stash.disable
|
||||||
|
|||||||
@@ -4,15 +4,17 @@ local Targets = {
|
|||||||
default = {
|
default = {
|
||||||
label = "Default Target Label",
|
label = "Default Target Label",
|
||||||
distance = 2,
|
distance = 2,
|
||||||
|
size = vector3(2.0, 2.0, 2.0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function Targets.OnSpawn(entityData)
|
function Targets.OnSpawn(entityData)
|
||||||
if not entityData.spawned or not entityData.targets then return end
|
if not entityData.targets then return end
|
||||||
if entityData.targets?.label then
|
if entityData.targets?.label then
|
||||||
local temp = entityData.targets
|
local temp = entityData.targets
|
||||||
entityData.targets = {temp}
|
entityData.targets = {temp}
|
||||||
end
|
end
|
||||||
|
print("Spawned targets:", entityData.targets)
|
||||||
local numberedTargets = {}
|
local numberedTargets = {}
|
||||||
for k, v in pairs(entityData.targets) do
|
for k, v in pairs(entityData.targets) do
|
||||||
local onSelect = v.onSelect
|
local onSelect = v.onSelect
|
||||||
@@ -24,18 +26,26 @@ function Targets.OnSpawn(entityData)
|
|||||||
table.insert(numberedTargets, v)
|
table.insert(numberedTargets, v)
|
||||||
end
|
end
|
||||||
table.sort(numberedTargets, function(a, b) return (a.label < b.label) end)
|
table.sort(numberedTargets, function(a, b) return (a.label < b.label) end)
|
||||||
Bridge.Target.AddLocalEntity(entityData.spawned, numberedTargets)
|
if not entityData.spawned then
|
||||||
|
local size = Targets.default.size
|
||||||
|
Bridge.Target.AddBoxZone(entityData.id, entityData.coords, entityData.size, entityData.heading, numberedTargets, entityData.debug)
|
||||||
|
else
|
||||||
|
Bridge.Target.AddLocalEntity(entityData.spawned, numberedTargets)
|
||||||
|
end
|
||||||
entityData.oldTargets = entityData.targets
|
entityData.oldTargets = entityData.targets
|
||||||
Bridge.Entity.Set(entityData.id, "oldTargets", entityData.oldTargets)
|
Bridge.Entity.Set(entityData.id, "oldTargets", entityData.oldTargets)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Targets.OnRemove(entityData)
|
function Targets.OnRemove(entityData)
|
||||||
if not entityData.spawned or not entityData.oldTargets then return end
|
if not entityData.oldTargets then return end
|
||||||
|
if not entityData.spawned then
|
||||||
|
return Bridge.Target.RemoveZone(entityData.id)
|
||||||
|
end
|
||||||
Bridge.Target.RemoveLocalEntity(entityData.spawned)
|
Bridge.Target.RemoveLocalEntity(entityData.spawned)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Targets.OnUpdate(entityData)
|
function Targets.OnUpdate(entityData)
|
||||||
if not entityData.spawned or not entityData.oldTargets then return end
|
if not entityData.oldTargets then return end
|
||||||
local doesntMatch = false
|
local doesntMatch = false
|
||||||
for k, v in pairs(entityData.targets) do
|
for k, v in pairs(entityData.targets) do
|
||||||
if not entityData.oldTargets or not entityData.oldTargets[k] then
|
if not entityData.oldTargets or not entityData.oldTargets[k] then
|
||||||
@@ -52,6 +62,7 @@ function Targets.OnUpdate(entityData)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if doesntMatch then
|
if doesntMatch then
|
||||||
|
print("ASDFASDFASDF")
|
||||||
Targets.OnRemove(entityData)
|
Targets.OnRemove(entityData)
|
||||||
Wait(100)
|
Wait(100)
|
||||||
Targets.OnSpawn(entityData)
|
Targets.OnSpawn(entityData)
|
||||||
|
|||||||
Reference in new issue
Block a user