From 18748bd7754c19fd35792d2306012ace3c8c9d7d Mon Sep 17 00:00:00 2001 From: "Mr.Crowley" Date: Thu, 4 Sep 2025 11:42:59 -0500 Subject: [PATCH] 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. --- lib/entities/client/client_entity.lua | 33 ++++++++++++--------------- lib/utility/shared/math.lua | 9 +++++++- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index bde8323..6a627df 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -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 diff --git a/lib/utility/shared/math.lua b/lib/utility/shared/math.lua index 499d8c4..3691d66 100644 --- a/lib/utility/shared/math.lua +++ b/lib/utility/shared/math.lua @@ -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.