entity adjustments

depricating register / unregister and replacing with Create/Destroy to follow convention
math.Inbounds checks a list of points (table.points = vector3[] minZ = int || -math.huge, maxZ = int || math.huge)
This commit is contained in:
gononono64 committed 2025-06-30 15:18:01 -05:00
1 parent 5dc99e4004
commit 003153be63
3 files changed
+144 -65

No files matched your search

+30 -15
View File
@@ -62,29 +62,35 @@ 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.
-- @param entityData table Data received from the server via 'community_bridge:client:CreateEntity' -- @param entityData table Data received from the server via 'community_bridge:client:CreateEntity'
function ClientEntity.Register(entityData) function ClientEntity.Create(entityData)
if Entities[entityData.id] then return end -- Already registered entityData.id = entityData.id or Ids.CreateUniqueId(Entities)
if Entities[entityData.id] then return Entities[entityData.id] end -- Already registered
Entities[entityData.id] = entityData Entities[entityData.id] = entityData
-- print(string.format("[ClientEntity] Registering %s entity %s", entityData.entityType, entityData.id)) return Point.Register(entityData.id, entityData.coords, entityData.spawnDistance or 50.0, entityData, SpawnEntity, RemoveEntity, function() end)
-- Use Point system for proximity checks
-- print(string.format("[ClientEntity] Registering entity %s at %s", entityData.id, json.encode(entityData)))
return Point.Register(entityData.id, entityData.coords, entityData.spawnDistance or 50.0, entityData, SpawnEntity, RemoveEntity, function() end)
end end
function ClientEntity.RegisterBulk(entities) --Depricated use ClientEntity.Create instead
--- Registers an entity and spawns it in the world if not already spawned.
ClientEntity.Register = ClientEntity.Create
function ClientEntity.CreateBulk(entities)
local registeredEntities = {} local registeredEntities = {}
for _, entityData in pairs(entities) do for _, entityData in pairs(entities) do
ClientEntity.Register(entityData) local entity = ClientEntity.Create(entityData)
registeredEntities[entityData.id] = entityData registeredEntities[entity.id] = entity
end end
return registeredEntities return registeredEntities
end end
-- Depricated use ClientEntity.CreateBulk instead
ClientEntity.RegisterBulk = ClientEntity.CreateBulk
--- Unregisters an entity and removes it from the world if spawned. --- Unregisters an entity and removes it from the world if spawned.
-- @param id string|number The ID of the entity to unregister. -- @param id string|number The ID of the entity to unregister.
function ClientEntity.Unregister(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
@@ -92,6 +98,8 @@ function ClientEntity.Unregister(id)
RemoveEntity(entityData) RemoveEntity(entityData)
Entities[id] = nil Entities[id] = nil
end end
ClientEntity.Unregister = ClientEntity.Destroy
--- Updates the data for a registered entity. --- Updates the data for a registered entity.
-- @param id string|number The ID of the entity to update. -- @param id string|number The ID of the entity to update.
@@ -104,7 +112,6 @@ function ClientEntity.Update(id, data)
local needsPointUpdate = false local needsPointUpdate = false
for key, value in pairs(data) do for key, value in pairs(data) do
if key == 'coords' and #(entityData.coords - value) > 0.1 then if key == 'coords' and #(entityData.coords - value) > 0.1 then
-- print(string.format("[ClientEntity] Updating coords for entity %s", id))
needsPointUpdate = true needsPointUpdate = true
end end
if key == 'spawnDistance' and entityData.spawnDistance ~= value then if key == 'spawnDistance' and entityData.spawnDistance ~= value then
@@ -162,13 +169,21 @@ function ClientEntity.RegisterAction(name, func)
ClientEntityActions.RegisterAction(name, func) ClientEntityActions.RegisterAction(name, func)
end end
function ClientEntity.OnCreate(_type, func)
ClientEntity.OnCreates = ClientEntity.OnCreates or {}
if not ClientEntity.OnCreates[_type] then
ClientEntity.OnCreates[_type] = {}
end
table.insert(ClientEntity.OnCreates[_type], func)
end
-- Network Event Handlers -- Network Event Handlers
RegisterNetEvent("community_bridge:client:CreateEntity", function(entityData) RegisterNetEvent("community_bridge:client:CreateEntity", function(entityData)
ClientEntity.Register(entityData) ClientEntity.Create(entityData)
end) end)
RegisterNetEvent("community_bridge:client:CreateEntities", function(entities) RegisterNetEvent("community_bridge:client:CreateEntities", function(entities)
ClientEntity.RegisterBulk(entities) ClientEntity.CreateBulk(entities)
end) end)
RegisterNetEvent("community_bridge:client:DeleteEntity", function(id) RegisterNetEvent("community_bridge:client:DeleteEntity", function(id)
+57 -50
View File
@@ -125,68 +125,68 @@ local function getMouseWorldPos(depth)
return playerPos + normal * depth return playerPos + normal * depth
end end
local function isInBoundary(pos, boundary) -- local function isInBoundary(pos, boundary)
if not boundary then return true end -- if not boundary then return true end
local x, y, z = table.unpack(pos) -- local x, y, z = table.unpack(pos)
-- Handle legacy min/max boundary format for backwards compatibility -- -- Handle legacy min/max boundary format for backwards compatibility
if boundary.min and boundary.max then -- if boundary.min and boundary.max then
local minX, minY, minZ = table.unpack(boundary.min) -- local minX, minY, minZ = table.unpack(boundary.min)
local maxX, maxY, maxZ = table.unpack(boundary.max) -- local maxX, maxY, maxZ = table.unpack(boundary.max)
return x >= minX and x <= maxX and y >= minY and y <= maxY and z >= minZ and z <= maxZ -- return x >= minX and x <= maxX and y >= minY and y <= maxY and z >= minZ and z <= maxZ
end -- end
-- Handle list of points (polygon boundary) -- -- Handle list of points (polygon boundary)
if boundary.points and #boundary.points > 0 then -- if boundary.points and #boundary.points > 0 then
local points = boundary.points -- local points = boundary.points
local minZ = boundary.minZ or -math.huge -- local minZ = boundary.minZ or -math.huge
local maxZ = boundary.maxZ or math.huge -- local maxZ = boundary.maxZ or math.huge
-- Check Z bounds first -- -- Check Z bounds first
if z < minZ or z > maxZ then -- if z < minZ or z > maxZ then
return false -- return false
end -- end
-- Point-in-polygon test using ray casting algorithm (improved version) -- -- Point-in-polygon test using ray casting algorithm (improved version)
local inside = false -- local inside = false
local n = #points -- local n = #points
for i = 1, n do -- for i = 1, n do
local j = i == n and 1 or i + 1 -- Next point (wrap around) -- local j = i == n and 1 or i + 1 -- Next point (wrap around)
local xi, yi = points[i].x or points[i][1], points[i].y or points[i][2] -- local xi, yi = points[i].x or points[i][1], points[i].y or points[i][2]
local xj, yj = points[j].x or points[j][1], points[j].y or points[j][2] -- local xj, yj = points[j].x or points[j][1], points[j].y or points[j][2]
-- Ensure xi, yi, xj, yj are numbers -- -- Ensure xi, yi, xj, yj are numbers
if not (xi and yi and xj and yj) then -- if not (xi and yi and xj and yj) then
goto continue -- goto continue
end -- end
-- Ray casting test -- -- Ray casting test
if ((yi > y) ~= (yj > y)) then -- if ((yi > y) ~= (yj > y)) then
-- Calculate intersection point -- -- Calculate intersection point
local intersect = (xj - xi) * (y - yi) / (yj - yi) + xi -- local intersect = (xj - xi) * (y - yi) / (yj - yi) + xi
if x < intersect then -- if x < intersect then
inside = not inside -- inside = not inside
end -- end
end -- end
::continue:: -- ::continue::
end -- end
return inside -- return inside
end -- end
-- Fallback to true if boundary format is not recognized -- -- Fallback to true if boundary format is not recognized
return true -- return true
end -- end
local function checkMaterialAndBoundary() local function checkMaterialAndBoundary()
if not state.currentEntity then return true end if not state.currentEntity then return true end
local pos = GetEntityCoords(state.currentEntity) local pos = GetEntityCoords(state.currentEntity)
local inBounds = isInBoundary(pos, state.settings.boundary) local inBounds = Bridge.Math.InBoundary(pos, state.settings.boundary)
-- Check built-in boundary first -- Check built-in boundary first
if state.settings.boundary and not inBounds then return false end if state.settings.boundary and not inBounds then return false end
@@ -217,7 +217,7 @@ local function checkMaterialAndBoundaryDetailed()
if not state.currentEntity then return true, true, true end if not state.currentEntity then return true, true, true end
local pos = GetEntityCoords(state.currentEntity) local pos = GetEntityCoords(state.currentEntity)
local inBounds = isInBoundary(pos, state.settings.boundary) local inBounds = Bridge.Math.InBoundary(pos, state.settings.boundary)
local customCheckPassed = true local customCheckPassed = true
local materialCheckPassed = true local materialCheckPassed = true
@@ -440,7 +440,7 @@ local function handleNormalMode()
end end
if state.currentEntity then if state.currentEntity then
SetEntityCoords(state.currentEntity, pos.x, pos.y, pos.z, false, false, false, true) SetEntityCoords(state.currentEntity, pos.x, pos.y, pos.z, false, false, false, false)
SetEntityHeading(state.currentEntity, state.heading) SetEntityHeading(state.currentEntity, state.heading)
if state.snapToGround then if state.snapToGround then
local slerp = PlaceObjectOnGroundProperly(state.currentEntity) local slerp = PlaceObjectOnGroundProperly(state.currentEntity)
@@ -554,14 +554,21 @@ local function placementLoop()
state.keys.placeObject = false -- Reset the key state state.keys.placeObject = false -- Reset the key state
local canPlace = checkMaterialAndBoundary() local canPlace = checkMaterialAndBoundary()
if canPlace then if canPlace then
Wait(100)
local coords = GetEntityCoords(state.currentEntity) local coords = GetEntityCoords(state.currentEntity)
local heading = GetEntityHeading(state.currentEntity) if not state.settings.allowVertical or state.snapToGround then
local groundZ, _z = GetGroundZFor_3dCoord(coords.x, coords.y, coords.z + 50, false)
if groundZ then
coords = vector3(coords.x, coords.y, _z)
end
end
local rotation = GetEntityRotation(state.currentEntity)
if state.promise then if state.promise then
state.promise:resolve({ state.promise:resolve({
entity = state.currentEntity, entity = state.currentEntity,
coords = coords, coords = coords,
heading = heading, rotation = rotation,
placed = true placed = true
}) })
end end
@@ -692,7 +699,7 @@ function PlaceableObject.Create(model, settings)
local p = promise.new() local p = promise.new()
state.promise = p state.promise = p
local point = Bridge.ClientEntity.Register({ local point = Bridge.ClientEntity.Create({
id = 'placeable_object', id = 'placeable_object',
entityType = 'object', entityType = 'object',
model = model, model = model,
+57
View File
@@ -84,6 +84,63 @@ function Math.GetOffsetFromMatrix(position, rotation, offset)
return position + rotated return position + rotated
end end
function Math.InBoundary(pos, boundary)
if not boundary then return true end
local x, y, z = table.unpack(pos)
-- Handle legacy min/max boundary format for backwards compatibility
if boundary.min and boundary.max then
local minX, minY, minZ = table.unpack(boundary.min)
local maxX, maxY, maxZ = table.unpack(boundary.max)
return x >= minX and x <= maxX and y >= minY and y <= maxY and z >= minZ and z <= maxZ
end
-- Handle list of points (polygon boundary)
if boundary.points and #boundary.points > 0 then
local points = boundary.points
local minZ = boundary.minZ or -math.huge
local maxZ = boundary.maxZ or math.huge
-- Check Z bounds first
if z < minZ or z > maxZ then
return false
end
-- Point-in-polygon test using ray casting algorithm (improved version)
local inside = false
local n = #points
for i = 1, n do
local j = i == n and 1 or i + 1 -- Next point (wrap around)
local xi, yi = points[i].x or points[i][1], points[i].y or points[i][2]
local xj, yj = points[j].x or points[j][1], points[j].y or points[j][2]
-- Ensure xi, yi, xj, yj are numbers
if not (xi and yi and xj and yj) then
goto continue
end
-- Ray casting test
if ((yi > y) ~= (yj > y)) then
-- Calculate intersection point
local intersect = (xj - xi) * (y - yi) / (yj - yi) + xi
if x < intersect then
inside = not inside
end
end
::continue::
end
return inside
end
-- Fallback to true if boundary format is not recognized
return true
end
exports('Math', Math) exports('Math', Math)
return Math return Math