Refactor client entity management functions
Added freeze position handling in UpdateEntity and ensured OnDestroy behavior is triggered on entity destruction. Removed unused and commented-out Update function, and improved code clarity in Set and Destroy methods.
This commit is contained in:
1 parent
8346130b00
commit
dd08f058c3
1 file changed
+9
-57
@@ -108,6 +108,9 @@ local function UpdateEntity(_entityData)
|
|||||||
entityData.oldRotation = entityData.rotation
|
entityData.oldRotation = entityData.rotation
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if entityData.freeze ~= nil and entityData.spawned and IsEntityPositionFrozen(entityData.spawned) then
|
||||||
|
FreezeEntityPosition(entityData.spawned, entityData.freeze)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Registers an entity received from the server and sets up proximity spawning.
|
--- Registers an entity received from the server and sets up proximity spawning.
|
||||||
@@ -150,7 +153,7 @@ ClientEntity.RegisterBulk = ClientEntity.CreateBulk
|
|||||||
function ClientEntity.Destroy(id)
|
function ClientEntity.Destroy(id)
|
||||||
local entityData = Entities[id]
|
local entityData = Entities[id]
|
||||||
if not entityData then return end
|
if not entityData then return end
|
||||||
|
Behaviors.Trigger("OnDestroy", entityData)
|
||||||
Point.Remove(id)
|
Point.Remove(id)
|
||||||
RemoveEntity(entityData)
|
RemoveEntity(entityData)
|
||||||
Entities[id] = nil
|
Entities[id] = nil
|
||||||
@@ -161,68 +164,17 @@ function ClientEntity.RegisterBehavior(id, behavior)
|
|||||||
Behaviors.Create(id, behavior)
|
Behaviors.Create(id, behavior)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- Sets a key-value pair for a client entity.
|
||||||
|
---@param id any
|
||||||
|
---@param key any
|
||||||
|
---@param value any
|
||||||
|
---@return nil
|
||||||
function ClientEntity.Set(id, key, value)
|
function ClientEntity.Set(id, key, value)
|
||||||
local entityData = ClientEntity.Get(id)
|
local entityData = ClientEntity.Get(id)
|
||||||
if not entityData then return print(string.format("[ClientEntity] SetKey: Entity %s does not exist", id)) end
|
if not entityData then return print(string.format("[ClientEntity] SetKey: Entity %s does not exist", id)) end
|
||||||
entityData[key] = value
|
entityData[key] = value
|
||||||
-- TriggerServerEvent("community_bridge:server:UpdateEntity", id, {[key] = value})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Updates the data for a registered entity.
|
|
||||||
-- @param id string|number The ID of the entity to update.
|
|
||||||
-- @param data table The data fields to update.
|
|
||||||
-- function ClientEntity.Update(id, data)
|
|
||||||
-- local entityData = ClientEntity.Get(id)
|
|
||||||
-- if not entityData then return end
|
|
||||||
|
|
||||||
-- local needsPointUpdate = false
|
|
||||||
-- for key, value in pairs(data) do
|
|
||||||
-- if key == 'coords' and #(entityData.coords - value) > 0.1 then
|
|
||||||
-- needsPointUpdate = true
|
|
||||||
-- end
|
|
||||||
-- if key == 'spawnDistance' and entityData.spawnDistance ~= value then
|
|
||||||
-- needsPointUpdate = true
|
|
||||||
-- end
|
|
||||||
-- entityData[key] = value
|
|
||||||
-- end
|
|
||||||
|
|
||||||
-- -- If entity is currently spawned, apply updates
|
|
||||||
-- if entityData.spawned and DoesEntityExist(entityData.spawned) then
|
|
||||||
-- if data.coords then
|
|
||||||
-- SetEntityCoords(entityData.spawned, entityData.coords.x, entityData.coords.y, entityData.coords.z, false, false, false, true)
|
|
||||||
-- end
|
|
||||||
-- if data.rotation then
|
|
||||||
-- if entityData.entityType == 'object' then
|
|
||||||
-- SetEntityRotation(entityData.spawned, entityData.rotation.x, entityData.rotation.y, entityData.rotation.z, 2, true)
|
|
||||||
-- else -- Ped/Vehicle heading
|
|
||||||
-- SetEntityHeading(entityData.spawned, type(entityData.rotation) == 'number' and entityData.rotation or entityData.rotation.z)
|
|
||||||
-- end
|
|
||||||
-- end
|
|
||||||
-- if data.freeze ~= nil then
|
|
||||||
-- FreezeEntityPosition(entityData.spawned, data.freeze)
|
|
||||||
-- end
|
|
||||||
-- -- Add other updatable properties as needed
|
|
||||||
-- end
|
|
||||||
|
|
||||||
-- -- Update Point registration if coords or distance changed
|
|
||||||
-- if needsPointUpdate then
|
|
||||||
-- Point.Remove(id)
|
|
||||||
-- Point.Register(
|
|
||||||
-- entityData.id,
|
|
||||||
-- entityData.coords,
|
|
||||||
-- entityData.spawnDistance or 50.0,
|
|
||||||
-- entityData,
|
|
||||||
-- SpawnEntity,
|
|
||||||
-- RemoveEntity,
|
|
||||||
-- OnUpdateEntity
|
|
||||||
-- )
|
|
||||||
-- end
|
|
||||||
|
|
||||||
-- if entityData.OnUpdate and type(entityData.OnUpdate) == 'function' then
|
|
||||||
-- entityData.OnUpdate(entityData, data)
|
|
||||||
-- end
|
|
||||||
-- end
|
|
||||||
|
|
||||||
function ClientEntity.Get(id)
|
function ClientEntity.Get(id)
|
||||||
return Entities[id]
|
return Entities[id]
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in new issue
Block a user