Refactor entity movement and add Math.Nearest function

Simplifies entity movement logic in client_entity.lua by improving coordinate comparison and updating behavior triggers. Adds a new Math.Nearest function in math.lua to round values to the nearest whole number.
This commit is contained in:
Mr.Crowley committed 2025-09-04 11:42:59 -05:00
1 parent debfb06231
commit 18748bd775
2 files changed
+23 -19

No files matched your search

+15 -18
View File
@@ -76,24 +76,21 @@ local function UpdateEntity(_entityData)
end)
end
Behaviors.Trigger("OnUpdate", entityData)
if entityData.oldCoords and entityData.oldCoords ~= entityData.coords then
local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z)
entityData.oldCoords = vector3(entityData.oldCoords.x, entityData.oldCoords.y, entityData.oldCoords.z)
local dist = #(coords.xyz - entityData.oldCoords.xyz)
if dist > 0.5 then
if entityData.spawned then
SetEntityCoords(entityData.spawned, coords.x, coords.y, coords.z, false, false, false, true)
end
-- ClientEntity.UpdateCoords(entityData.id, entityData.coords)
if entityData.OnMove then
pcall(function (...)
return entityData.OnMove(entityData)
end)
end
Behaviors.Trigger("OnMove", entityData, entityData.oldCoords, entityData.coords)
end
entityData.oldCoords = entityData.coords
end
local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z)
entityData.oldCoords = entityData.oldCoords and vector3(entityData.oldCoords.x, entityData.oldCoords.y, entityData.oldCoords.z) or coords
local dist = #(coords - entityData.oldCoords)
if entityData.oldCoords and dist > 0.5 then
if entityData.spawned then
SetEntityCoords(entityData.spawned, coords.x, coords.y, coords.z, false, false, false, true)
end
-- ClientEntity.UpdateCoords(entityData.id, entityData.coords)
if entityData.OnMove then
pcall(function (...)
return entityData.OnMove(entityData)
end)
end
Behaviors.Trigger("OnMove", entityData, entityData.oldCoords, entityData.coords)
end
if not entityData.oldRotation or entityData.oldRotation ~= entityData.rotation then
if entityData.spawned and DoesEntityExist(entityData.spawned) then
+8 -1
View File
@@ -3,7 +3,7 @@ Math = Math or {}
---@class Math
------@field public name string @add name field to class Car, you'll see it in code completion
---@field public name string @add name field to class Car, you'll see it in code completion
Math = {}
@@ -44,6 +44,13 @@ function Math.Smooth(value, min, max)
return value * value * (3 - 2 * value)
end
--- Gets the nearest whole number.
--- @param value number The value to round.
--- @return number The rounded value.
function Math.Nearest(value)
return math.floor(value + 0.5)
end
--- Maps a value from one range to another.
---@param value number The value to map.
---@param inMin number The minimum value for the provided value.