From 4d8b2ff2032123ddb312d7c4aded334013314a9b Mon Sep 17 00:00:00 2001 From: gononono64 <96976330+gononono64@users.noreply.github.com> Date: Sun, 4 May 2025 11:11:29 -0500 Subject: [PATCH] Rebound entity stuff added function to get a local offset world position from a point updated entities to automatically process next queued action without needing to call processnextaction Added client freeze, circle, and bobupanddown for client/rebound entity manipulation --- lib/entities/client/client_entity.lua | 24 +++++ lib/entities/client/client_entity_actions.lua | 13 ++- .../client/client_entity_actions_ext.lua | 96 ++++++++++++++++++- lib/entities/server/server_entity.lua | 11 ++- lib/utility/shared/math.lua | 41 +++++++- 5 files changed, 174 insertions(+), 11 deletions(-) diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index 751f6e0..59af37b 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -156,6 +156,10 @@ function ClientEntity.GetAll() return Entities end +function ClientEntity.RegisterAction(name, func) + ClientEntityActions.RegisterAction(name, func) +end + -- Network Event Handlers RegisterNetEvent("community_bridge:client:CreateEntity", function(entityData) ClientEntity.Register(entityData) @@ -190,6 +194,26 @@ RegisterNetEvent("community_bridge:client:TriggerEntityAction", function(entityI end end) +RegisterNetEvent("community_bridge:client:TriggerEntityActions", function(entityId, actions, endPosition) + local entityData = Entities[entityId] + if entityData then + for _, actionData in pairs(actions) do + local actionName = actionData.name + local actionParams = actionData.params + if actionName == "Stop" then + ClientEntityActions.StopAction(entityId) + elseif actionName == "Skip" then + ClientEntityActions.SkipAction(entityId) + else + local currentAction = ClientEntityActions.ActionQueue[entityId] and ClientEntityActions.ActionQueue[entityId][1] + ClientEntityActions.QueueAction(entityData, actionName, table.unpack(actionParams)) + end + end + else + print(string.format("[ClientEntity] Received actions for non-existent entity %s.", entityId)) + end +end) + -- Resource Stop Cleanup AddEventHandler('onResourceStop', function(resourceName) if resourceName == GetCurrentResourceName() then diff --git a/lib/entities/client/client_entity_actions.lua b/lib/entities/client/client_entity_actions.lua index 9cf9f1e..b8ffd67 100644 --- a/lib/entities/client/client_entity_actions.lua +++ b/lib/entities/client/client_entity_actions.lua @@ -32,11 +32,16 @@ function ClientEntityActions.ProcessNextAction(entityId) -- print(string.format("[ClientEntityActions] Starting action '%s' for entity %s", nextAction.name, entityId)) ClientEntityActions.IsActionRunning [entityId] = true -- Call the registered function - actionFunc(entityData, table.unpack(nextAction.args)) + ClientEntityActions.ActionQueue[entityId] = actionFunc(entityData, table.unpack(nextAction.args)) + if not ClientEntityActions.ActionQueue[entityId] then + -- If the action function doesn't return a queue, set it to nil + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) -- Try next action if this one failed immediately + end else print(string.format("[ClientEntityActions] Unknown action '%s' dequeued for entity %s", nextAction.name, entityId)) -- Skip unknown action and try the next one immediately - ClientEntityActions.ProcessNextAction(entityId) + ClientEntityActions.ActionQueue[entityId] = ClientEntityActions.ProcessNextAction(entityId) end end --- Registers a custom action implementation. @@ -50,9 +55,7 @@ function ClientEntityActions.RegisterAction(actionName, actionFunc) print(string.format("[ClientEntityActions] WARNING: Overwriting registered action '%s'", actionName)) end assert(type(actionName) == "string", "actionName must be a string") - assert(type(actionFunc) == "function", "actionFunc must be a function") - print(string.format("[ClientEntityActions] Registered action: %s", actionName)) - ClientEntityActions.RegisteredActions [actionName] = actionFunc + ClientEntityActions.RegisteredActions[actionName] = actionFunc -- print(string.format("[ClientEntityActions] Registered action: %s", actionName)) end diff --git a/lib/entities/client/client_entity_actions_ext.lua b/lib/entities/client/client_entity_actions_ext.lua index 99622f2..73dc7b5 100644 --- a/lib/entities/client/client_entity_actions_ext.lua +++ b/lib/entities/client/client_entity_actions_ext.lua @@ -1,5 +1,7 @@ DefaultActions = {} -ClientEntityActions = ClientEntityActions or Require("lib/entities/client/client_entity_actions.lua") -- Added +ClientEntityActions = ClientEntityActions or Require("lib/entities/client/client_entity_actions.lua") +LA = LA or Require("lib/utility/shared/la.lua") + --- Internal implementation for walking. Registered via RegisterAction. function DefaultActions.WalkTo(entityData, coords, speed, timeout) print("[ClientEntityActions] WalkTo action called", coords, speed, timeout) @@ -196,7 +198,8 @@ function DefaultActions.AttachProp(entityData, propModel, boneName, offsetPos, o offsetRot = offsetRot or vector3(0.0, 0.0, 0.0) print(string.format("[ClientEntityActions] Attaching prop '%s' to entity %s at bone %d", propModel, entityId, boneIndex)) AttachEntityToEntity(prop, entity, boneIndex, offsetPos.x, offsetPos.y, offsetPos.z, offsetRot.x, offsetRot.y, offsetRot.z, false, useSoftPinning or false, collision or false, isPed or false, vertexIndex or 2, fixedRot == nil and true or fixedRot) - + entityData.props = entityData.props or {} -- Ensure props table exists + table.insert(entityData.props, prop) -- Store the prop handle in the entity data -- Store the attached prop handle for later removal if not entityData.attachedProps then entityData.attachedProps = {} end entityData.attachedProps[propModel] = prop -- Store by model name/hash for easy lookup @@ -226,8 +229,8 @@ function DefaultActions.DetachProp(entityData, propModel) end -- This action finishes immediately - ClientEntityActions.IsActionRunning[entityId] = false - ClientEntityActions.ProcessNextAction(entityId) + -- ClientEntityActions.IsActionRunning[entityId] = false + -- ClientEntityActions.ProcessNextAction(entityId) end function DefaultActions.GetInCar(entityData, vehicleData, seatIndex, timeout) @@ -264,8 +267,93 @@ function DefaultActions.GetInCar(entityData, vehicleData, seatIndex, timeout) ClientEntityActions.ActionThreads[entityId] = thread end +function DefaultActions.Freeze(entityData, freeze) + print("[ClientEntityActions] Freeze action called", freeze) + local entity = entityData.spawned + local entityId = entityData.id + + if not entity or not DoesEntityExist(entity) then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) -- Try next action if this one failed immediately + return + end + + FreezeEntityPosition(entity, freeze or true) + + -- This action finishes immediately + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) +end + +function DefaultActions.PlaceOnGround(entityData) + local entity = entityData.spawned + local entityId = entityData.id + + if not entity or not DoesEntityExist(entity) then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) -- Try next action if this one failed immediately + return + end + PlaceObjectOnGroundProperly(entity) + + -- This action finishes immediately + -- ClientEntityActions.IsActionRunning[entityId] = false + -- ClientEntityActions.ProcessNextAction(entityId) +end + +function DefaultActions.BobUpAndDown(entityData, speed, height) + local entity = entityData.spawned + local entityId = entityData.id + if not entity or not DoesEntityExist(entity) then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) -- Try next action if this one failed immediately + return + end + CreateThread(function() + local coords = GetEntityCoords(entity) + local originalZ = coords.z + while DoesEntityExist(entity) do + -- Calculate the new Z coordinate + local newZ = originalZ + math.sin(GetGameTimer() * (speed / 1000)) * height + -- Set the new coordinates + SetEntityCoords(entity, coords.x, coords.y, newZ) + -- Wait for 10 milliseconds + Wait(10) + end + end) + -- ClientEntityActions.IsActionRunning[entityId] = false + -- ClientEntityActions.ProcessNextAction(entityId) -- Try next action if this one failed immediately +end + +function DefaultActions.Circle(entityData, radius, speed) + local entity = entityData.spawned + local entityId = entityData.id + + if not entity or not DoesEntityExist(entity) then + ClientEntityActions.IsActionRunning[entityId] = false + ClientEntityActions.ProcessNextAction(entityId) -- Try next action if this one failed immediately + return + end + + local coords = GetEntityCoords(entity) + local angle = 0.0 + CreateThread(function() + while DoesEntityExist(entity) do + FreezeEntityPosition(entity, false) + local pos = LA.Circle(angle, radius, coords) + SetEntityCoords(entity, pos.x, pos.y, pos.z, false, false, false, false) + angle = angle + speed * GetFrameTime() + FreezeEntityPosition(entity, true) + Wait(0) + end + end) + -- ClientEntityActions.IsActionRunning[entityId] = false + -- ClientEntityActions.ProcessNextAction(entityId) -- Try next action if this one failed immediately +end + for name, func in pairs(DefaultActions) do ClientEntityActions.RegisterAction(name, func) end + return ClientEntityActions diff --git a/lib/entities/server/server_entity.lua b/lib/entities/server/server_entity.lua index b040332..f066bbd 100644 --- a/lib/entities/server/server_entity.lua +++ b/lib/entities/server/server_entity.lua @@ -26,6 +26,7 @@ function ServerEntity.Create(id, entityType, model, coords, rotation, meta) ServerEntity.Add(self) TriggerClientEvent("community_bridge:client:CreateEntity", -1, self) + Wait(1000) return self end @@ -65,10 +66,18 @@ function ServerEntity.TriggerAction(entityId, actionName, endPosition, ...) print(string.format("[ServerEntity] Attempted to trigger action '%s' on non-existent entity %s", actionName, entityId)) return end - print(string.format("[ServerEntity] Triggering action '%s' for entity %s", actionName, entityId)) TriggerClientEvent("community_bridge:client:TriggerEntityAction", -1, entityId, actionName, endPosition, ...) end +function ServerEntity.TriggerActions(entityId, actions, endPosition) + local entity = Entities[entityId] + if not entity then + print(string.format("[ServerEntity] Attempted to trigger actions on non-existent entity %s", entityId)) + return + end + TriggerClientEvent("community_bridge:client:TriggerEntityActions", -1, entityId, actions, endPosition) +end + function ServerEntity.GetAll() return Entities end diff --git a/lib/utility/shared/math.lua b/lib/utility/shared/math.lua index 196e749..09e750b 100644 --- a/lib/utility/shared/math.lua +++ b/lib/utility/shared/math.lua @@ -1,4 +1,4 @@ -Math = {} +Math = Math or {} function Math.Clamp(value, min, max) return math.min(math.max(value, min), max) @@ -46,5 +46,44 @@ function Math.DirectionToTarget(fromV3, toV3) return Math.Normalize3D(toV3.x - fromV3.x, toV3.y - fromV3.y, toV3.z - fromV3.z) end +function Deg2Rad(deg) + return deg * math.pi / 180.0 +end + +function RotVector(pos, rot) + local pitch = Deg2Rad(rot.x) + local roll = Deg2Rad(rot.y) + local yaw = Deg2Rad(rot.z) + + local cosY = math.cos(yaw) + local sinY = math.sin(yaw) + local cosP = math.cos(pitch) + local sinP = math.sin(pitch) + local cosR = math.cos(roll) + local sinR = math.sin(roll) + + local m11 = cosY * cosR + sinY * sinP * sinR + local m12 = sinR * cosP + local m13 = -sinY * cosR + cosY * sinP * sinR + + local m21 = -cosY * sinR + sinY * sinP * cosR + local m22 = cosR * cosP + local m23 = sinR * sinY + cosY * sinP * cosR + + local m31 = sinY * cosP + local m32 = -sinP + local m33 = cosY * cosP + + return vector3(pos.x * m11 + pos.y * m21 + pos.z * m31, pos.x * m12 + pos.y * m22 + pos.z * m32, pos.x * m13 + pos.y * m23 + pos.z * m33) +end + + +function Math.GetOffsetFromMatrix(position, rotation, offset) + local rotated = RotVector(offset, rotation) + print("Rotated: " .. tostring(rotated)) + return position + rotated +end + exports('Math', Math) + return Math \ No newline at end of file