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
+21 -17

No files matched your search

+13 -16
View File
@@ -76,23 +76,20 @@ local function UpdateEntity(_entityData)
end) end)
end end
Behaviors.Trigger("OnUpdate", entityData) 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)
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
entityData.oldCoords = vector3(entityData.oldCoords.x, entityData.oldCoords.y, entityData.oldCoords.z) local dist = #(coords - entityData.oldCoords)
local dist = #(coords.xyz - entityData.oldCoords.xyz) if entityData.oldCoords and dist > 0.5 then
if dist > 0.5 then if entityData.spawned then
if entityData.spawned then 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
-- 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 end
entityData.oldCoords = entityData.coords -- 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 end
if not entityData.oldRotation or entityData.oldRotation ~= entityData.rotation then if not entityData.oldRotation or entityData.oldRotation ~= entityData.rotation then
+8 -1
View File
@@ -3,7 +3,7 @@ Math = Math or {}
---@class Math ---@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 = {} Math = {}
@@ -44,6 +44,13 @@ function Math.Smooth(value, min, max)
return value * value * (3 - 2 * value) return value * value * (3 - 2 * value)
end 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. --- Maps a value from one range to another.
---@param value number The value to map. ---@param value number The value to map.
---@param inMin number The minimum value for the provided value. ---@param inMin number The minimum value for the provided value.