diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index 3544dbb..6a627df 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -8,40 +8,39 @@ ClientEntity = { } -- Renamed from BaseEntity ClientEntity.Behaviors = Behaviors - - - -------------------- local function SpawnEntity(entityData) if entityData.spawned and DoesEntityExist(entityData.spawned) then return end -- Already spawned - local loaded, model = Utility.LoadModel(entityData.model) - if not loaded then - print(string.format("[ClientEntity] Failed to load model %s for entity %s", entityData.model, entityData.id)) - return - end + if entityData.model then + local loaded, model = Utility.LoadModel(entityData.model) + if not loaded then + print(string.format("[ClientEntity] Failed to load model %s for entity %s", entityData.model, entityData.id)) + return + end - local entity = nil - local coords = entityData.coords - local rotation = entityData.rotation or vector3(0.0, 0.0, entityData.heading or 0.0) -- Default rotation if not provided + local entity = nil + local coords = entityData.coords + local rotation = entityData.rotation or vector3(0.0, 0.0, entityData.heading or 0.0) -- Default rotation if not provided - if entityData.entityType == 'object' then - entity = CreateObject(model, coords.x, coords.y, coords.z, false, false, false) - SetEntityRotation(entity, rotation.x, rotation.y, rotation.z, 2, true) - elseif entityData.entityType == 'ped' then - entity = CreatePed(4, model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false) - elseif entityData.entityType == 'vehicle' then - entity = CreateVehicle(model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false) - else - print(string.format("[ClientEntity] Unknown entity type '%s' for entity %s", entityData.entityType, entityData.id)) - end - if entity and model then - entityData.spawned = entity - SetModelAsNoLongerNeeded(model) - SetEntityAsMissionEntity(entity, true, true) - FreezeEntityPosition(entity, true) - else - SetModelAsNoLongerNeeded(model) + if entityData.entityType == 'object' then + entity = CreateObject(model, coords.x, coords.y, coords.z, false, false, false) + SetEntityRotation(entity, rotation.x, rotation.y, rotation.z, 2, true) + elseif entityData.entityType == 'ped' then + entity = CreatePed(4, model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false) + elseif entityData.entityType == 'vehicle' then + entity = CreateVehicle(model, coords.x, coords.y, coords.z, type(rotation) == 'number' and rotation or rotation.z, false, false) + else + print(string.format("[ClientEntity] Unknown entity type '%s' for entity %s", entityData.entityType, entityData.id)) + end + if entity and model then + entityData.spawned = entity + SetModelAsNoLongerNeeded(model) + SetEntityAsMissionEntity(entity, true, true) + FreezeEntityPosition(entity, true) + else + SetModelAsNoLongerNeeded(model) + end end if entityData.OnSpawn then pcall(function (...) @@ -71,20 +70,27 @@ end local function UpdateEntity(_entityData) local entityData = ClientEntity.Get(_entityData.id) if not entityData then return end - if entityData.oldCoords and entityData.oldCoords ~= entityData.coords then - local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) - local dist = #(coords.xyz - entityData.oldCoords.xyz) - if dist > 0.1 then - -- 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 + if entityData.OnUpdate then + pcall(function (...) + return entityData.OnUpdate(entityData) + end) + end + Behaviors.Trigger("OnUpdate", entityData) + 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 @@ -96,12 +102,10 @@ local function UpdateEntity(_entityData) end entityData.oldRotation = entityData.rotation end - if entityData.OnUpdate then - pcall(function (...) - return entityData.OnUpdate(entityData) - end) + + if entityData.freeze ~= nil and entityData.spawned and IsEntityPositionFrozen(entityData.spawned) then + FreezeEntityPosition(entityData.spawned, entityData.freeze) end - Behaviors.Trigger("OnUpdate", entityData) end --- Registers an entity received from the server and sets up proximity spawning. @@ -144,9 +148,9 @@ ClientEntity.RegisterBulk = ClientEntity.CreateBulk function ClientEntity.Destroy(id) local entityData = Entities[id] if not entityData then return end - + Behaviors.Trigger("OnDestroy", entityData) Point.Remove(id) - RemoveEntity(entityData) + RemoveEntity(entityData) Entities[id] = nil end ClientEntity.Unregister = ClientEntity.Destroy @@ -155,68 +159,17 @@ function ClientEntity.RegisterBehavior(id, behavior) Behaviors.Create(id, behavior) end +--- Sets a key-value pair for a client entity. +---@param id any +---@param key any +---@param value any +---@return nil function ClientEntity.Set(id, key, value) local entityData = ClientEntity.Get(id) if not entityData then return print(string.format("[ClientEntity] SetKey: Entity %s does not exist", id)) end entityData[key] = value - -- TriggerServerEvent("community_bridge:server:UpdateEntity", id, {[key] = value}) end ---- Updates the data for a registered entity. --- @param id string|number The ID of the entity to update. --- @param data table The data fields to update. --- function ClientEntity.Update(id, data) --- local entityData = ClientEntity.Get(id) --- if not entityData then return end - --- local needsPointUpdate = false --- for key, value in pairs(data) do --- if key == 'coords' and #(entityData.coords - value) > 0.1 then --- needsPointUpdate = true --- end --- if key == 'spawnDistance' and entityData.spawnDistance ~= value then --- needsPointUpdate = true --- end --- entityData[key] = value --- end - --- -- If entity is currently spawned, apply updates --- if entityData.spawned and DoesEntityExist(entityData.spawned) then --- if data.coords then --- SetEntityCoords(entityData.spawned, entityData.coords.x, entityData.coords.y, entityData.coords.z, false, false, false, true) --- end --- if data.rotation then --- if entityData.entityType == 'object' then --- SetEntityRotation(entityData.spawned, entityData.rotation.x, entityData.rotation.y, entityData.rotation.z, 2, true) --- else -- Ped/Vehicle heading --- SetEntityHeading(entityData.spawned, type(entityData.rotation) == 'number' and entityData.rotation or entityData.rotation.z) --- end --- end --- if data.freeze ~= nil then --- FreezeEntityPosition(entityData.spawned, data.freeze) --- end --- -- Add other updatable properties as needed --- end - --- -- Update Point registration if coords or distance changed --- if needsPointUpdate then --- Point.Remove(id) --- Point.Register( --- entityData.id, --- entityData.coords, --- entityData.spawnDistance or 50.0, --- entityData, --- SpawnEntity, --- RemoveEntity, --- OnUpdateEntity --- ) --- end - --- if entityData.OnUpdate and type(entityData.OnUpdate) == 'function' then --- entityData.OnUpdate(entityData, data) --- end --- end - function ClientEntity.Get(id) return Entities[id] end @@ -258,9 +211,7 @@ function ClientEntity.UpdateCoords(id, coords) return end entityData.coords = coords - Point.UpdateCoords(id, coords) - if not entityData.spawned or not DoesEntityExist(entityData.spawned) then return end - SetEntityCoords(entityData.spawned, coords.x, coords.y, coords.z, false, false, false, true) + Point.UpdateCoords(id, coords) end function ClientEntity.ChangeModel(id, model) @@ -290,6 +241,7 @@ end) RegisterNetEvent("community_bridge:client:UpdateEntity", function(id, data) for k, v in pairs(data) do + print(json.encode({k, v})) ClientEntity.Set(id, k, v) end end) diff --git a/lib/entities/server/server_entity.lua b/lib/entities/server/server_entity.lua index c9950c0..c135432 100644 --- a/lib/entities/server/server_entity.lua +++ b/lib/entities/server/server_entity.lua @@ -14,8 +14,6 @@ local Invoked = {} -- @param meta table Optional additional data -- @return table The created entity data function ServerEntity.New(id, entityType, model, coords, rotation, args) - assert(entityType, "EntityType is required") - assert(model, "Model is required for entity creation") assert(coords, "Coords are required for entity creation") local self = args or {} self.id = id or Ids.CreateUniqueId(Entities) diff --git a/lib/entities/shared/behaviors.lua b/lib/entities/shared/behaviors.lua index 65f56a0..d85b4b1 100644 --- a/lib/entities/shared/behaviors.lua +++ b/lib/entities/shared/behaviors.lua @@ -4,6 +4,27 @@ Behaviors = { invoked = {} } +Behaviors.init = { + "anim", + "attach", + 'clothing', + 'follow', + 'particles', + 'scenarios', + 'shop', + 'stash', + 'targets', + 'weapon' +} + +function Behaviors.Init() + if Behaviors.isSetup then return end + Behaviors.isSetup = true + for k, v in pairs(Behaviors.init) do + Require(string.format("lib/entities/shared/behaviors/%s.lua", v)) + end +end + function Behaviors.Create(behaviorId, behavior) if not behaviorId or not behavior then return end Behaviors.All = Behaviors.All or {} @@ -26,7 +47,6 @@ end function Behaviors.Trigger(actionName, clientEntityData, ...) if not clientEntityData or not actionName then return end - local invoking = clientEntityData.invoked or GetInvokingResource() or "community_bridge" for property, behavior in pairs(Behaviors.All[invoking] or {}) do local hasBehaviorArgs = Behaviors.Has(property, clientEntityData) -- this is everything that's contained inside the object's individual property @@ -37,6 +57,15 @@ function Behaviors.Trigger(actionName, clientEntityData, ...) end end end + for property, behavior in pairs(Behaviors.All['community_bridge'] or {}) do + local hasBehaviorArgs = Behaviors.Has(property, clientEntityData) -- this is everything that's contained inside the object's individual property + if hasBehaviorArgs and behavior[actionName] and not Behaviors.All[invoking]?[actionName] then + local success, result = pcall(behavior[actionName], clientEntityData, hasBehaviorArgs, ...) + if not success then + print(string.format("[ClientEntity] Behavior %s failed: %s", property, result)) + end + end + end end function Behaviors.Inherit(behaviorId, clientEntityData, defaultData) @@ -75,4 +104,18 @@ AddEventHandler("onResourceStop", function(resourceName) end end) +if IsDuplicityVersion() then + RegisterNetEvent("onResourceStart", function(resourceName) + if resourceName ~= GetCurrentResourceName() then return end + Wait(500) + Behaviors.Init() + end) +else + RegisterNetEvent("onClientResourceStart", function(resource) + if resource ~= GetCurrentResourceName() then return end + Wait(500) + Behaviors.Init() + end) +end + return Behaviors diff --git a/lib/entities/shared/behaviors/anim.lua b/lib/entities/shared/behaviors/anim.lua new file mode 100644 index 0000000..ee6e40a --- /dev/null +++ b/lib/entities/shared/behaviors/anim.lua @@ -0,0 +1,62 @@ +if IsDuplicityVersion() then return end +local Animation = { + property = "anim", + default = { + flags = 49, + duration = -1 + } +} +-- Anim.Play(id, entity, animDict, animName, blendIn, blendOut, duration, flag, playbackRate, onComplete) +function Animation.Play(entityData) + if entityData.entityType ~= "ped" then return end + local entity = entityData.spawned + if not entity then return end + local dict = entityData.anim?.dict + local name = entityData.anim?.name + local onComplete = entityData.anim?.onComplete and function() entityData.anim.onComplete(entityData) end + if not dict or not name then return end + local flags = entityData.anim.flags or Animation.default.flags + local duration = entityData.anim.duration or Animation.default.duration + entityData.anim.id = Bridge.Anim.Play(nil, entity, dict, name, 8.0, -8.0, duration, flags, 0.0, onComplete) + Bridge.Entity.Set(entityData.id, "anim", entityData.anim) + entityData.oldAnim = entityData.anim + Bridge.Entity.Set(entityData.id, "oldAnim", entityData.oldAnim) + -- SetTimeout(1000, function() + -- if entityData.anim then + -- entityData.anim.disable = false + -- Bridge.Entity.Set(entityData.id, "anim", entityData.anim) + -- end + -- end) +end + +function Animation.OnSpawn(entityData) + print("Spawning animation for entity:", entityData.id) + if not entityData.spawned or not entityData.anim then return end + Animation.Play(entityData) + entityData.anim.disable = false + TaskSetBlockingOfNonTemporaryEvents(entityData.spawned, true) + SetBlockingOfNonTemporaryEvents(entityData.spawned, true) + Bridge.Entity.Set(entityData.id, "anim", entityData.anim) +end + +function Animation.OnRemove(entityData) + if not entityData.spawned or not entityData.anim then return end + entityData.oldAnim = nil + entityData.anim.disable = true + Bridge.Entity.Set(entityData.id, "anim", entityData.anim) + Bridge.Entity.Set(entityData.id, "oldAnim", entityData.oldAnim) +end + +function Animation.OnUpdate(entityData) + if not entityData.spawned or not entityData.anim then return end + if entityData.anim.disable then return end + if not IsEntityPlayingAnim(entityData.spawned, entityData.anim.dict, entityData.anim.name, 3) then + Bridge.Anim.Stop(entityData.anim?.id) + ClearPedTasksImmediately(entityData.spawned) + Wait(100) + Animation.Play(entityData) + end +end + +Bridge.Entity.RegisterBehavior("anim", Animation) +return Animation \ No newline at end of file diff --git a/lib/entities/shared/behaviors/attach.lua b/lib/entities/shared/behaviors/attach.lua new file mode 100644 index 0000000..8b3f66f --- /dev/null +++ b/lib/entities/shared/behaviors/attach.lua @@ -0,0 +1,52 @@ +if IsDuplicityVersion() then return end +local Attach = { + property = "attach", + default = { + bone = 0, + offset = vector3(0.0, 0.0, 0.0), + rotation = vector3(0.0, 0.0, 0.0) + } +} + + +function Attach.OnSpawn(entityData) + if not entityData.spawned or not entityData.attach then return end + local entity = entityData.spawned + local targetEntity = entityData.attach.target + if type(targetEntity) == "string" then + targetEntity = Bridge.Entity.Get(targetEntity)?.spawned or targetEntity + else + local player = GetPlayerFromServerId(targetEntity) + if player and player ~= -1 then + targetEntity = GetPlayerPed(player) + end + end + targetEntity = tonumber(targetEntity) + if not targetEntity then return end + local bone = entityData.attach.bone or Attach.default.bone + local offset = entityData.attach.offset or Attach.default.offset + local rotation = entityData.attach.rotation or Attach.default.rotation + AttachEntityToEntity(entity, targetEntity, GetPedBoneIndex(targetEntity, bone), offset.x, offset.y, offset.z, rotation.x, rotation.y, rotation.z, true, true, false, true, 1, true) + Bridge.Entity.Set(entityData.id, "attach", entityData.attach) +end + +function Attach.Detach(entityData) + if not entityData.spawned or not IsEntityAttached(entityData.spawned) then return end + DetachEntity(entityData.spawned, true, true) + Bridge.Entity.Set(entityData.id, "attach", entityData.attach) +end + +function Attach.OnUpdate(entityData) + if not entityData.spawned then return end + if entityData.attach.disable then + return Attach.Detach(entityData) + end + local entity = entityData.spawned + local coords = GetEntityCoords(entity) + Bridge.Entity.Set(entityData.id, "coords", vector3(coords.x, coords.y, coords.z)) + if IsEntityAttached(entity) then return end + Attach.OnSpawn(entityData) +end + +Bridge.Entity.RegisterBehavior("attach", Attach) +return Attach \ No newline at end of file diff --git a/lib/entities/shared/behaviors/clothing.lua b/lib/entities/shared/behaviors/clothing.lua new file mode 100644 index 0000000..4ae11db --- /dev/null +++ b/lib/entities/shared/behaviors/clothing.lua @@ -0,0 +1,82 @@ +if IsDuplicityVersion() then return end +local Clothing = { + property = "clothing", + default = { + components = {}, + props = {} + } +} + +function Clothing.ChangeClothes(entityData) + if entityData.entityType ~= "ped" then return end + local entity = entityData.spawned + if not entity or not DoesEntityExist(entity) then return end + local clothing = entityData.clothing.target + if clothing then + if type(clothing) == "number" then + local player = GetPlayerFromServerId(clothing) + if player and player ~= -1 then + clothing = Bridge.Clothing.GetAppearance(GetPlayerPed(player)) + end + elseif type(clothing) == "string" then + local targetEntityData = Bridge.Entity.Get(clothing) + if targetEntityData then + clothing = Bridge.Clothing.GetAppearance(targetEntityData.spawned) + end + end + else + clothing = entityData.clothing + end + + Bridge.Clothing.SetAppearance(entity, clothing) + Bridge.Entity.Set(entityData.id, "clothing", clothing) + entityData.oldClothing = clothing + Bridge.Entity.Set(entityData.id, "oldClothing", clothing) + +end + + +function Clothing.OnSpawn(entityData) + if not entityData.spawned or not entityData.clothing then return end + Clothing.ChangeClothes(entityData) + entityData.clothing.updating = false +end + +function Clothing.OnRemove(entityData) + if not entityData.spawned or not entityData.clothing then return end + entityData.oldClothing = nil + entityData.clothing.updating = true +end + +function Clothing.OnUpdate(entityData) + if not entityData.spawned or not entityData.clothing then return end + if entityData.clothing.updating then return end + entityData.clothing.updating = true + if not entityData.oldClothing then + Clothing.ChangeClothes(entityData) + return + end + for componentId, component in pairs(entityData.clothing.components or {}) do + local oldComponent = entityData.oldClothing.components and entityData.oldClothing.components[componentId] + if not oldComponent or oldComponent.drawable ~= component.drawable or oldComponent.texture ~= component.texture then + Clothing.ChangeClothes(entityData) + return + end + end + for propId, prop in pairs(entityData.clothing.props or {}) do + local oldProp = entityData.oldClothing.props and entityData.oldClothing.props[propId] + if not oldProp or oldProp.drawable ~= prop.drawable or oldProp.texture ~= prop.texture then + Clothing.ChangeClothes(entityData) + return + end + end + SetTimeout(1000, function() + if entityData.clothing then + entityData.clothing.updating = false + Bridge.Entity.Set(entityData.id, "clothing", entityData.clothing) + end + end) +end + +Bridge.Entity.RegisterBehavior("clothing", Clothing) +return Clothing \ No newline at end of file diff --git a/lib/entities/shared/behaviors/follow.lua b/lib/entities/shared/behaviors/follow.lua new file mode 100644 index 0000000..addefb3 --- /dev/null +++ b/lib/entities/shared/behaviors/follow.lua @@ -0,0 +1,75 @@ +if IsDuplicityVersion() then return end +local Follow = { + All = {}, + property = "follow", + default = { + speed = 1.0, + distance = 2.0 + } +} + +function Follow.PedWalkToPos(entityData) + if not entityData?.follow?.target or entityData?.entityType ~= "ped" then return end + local entity = entityData.spawned + local targetCoords = entityData.follow.target + if not targetCoords then return end + if type(targetCoords) == "number" then + local player = GetPlayerFromServerId(targetCoords) + if player and player ~= -1 then + targetCoords = GetEntityCoords(GetPlayerPed(player)) + else + targetCoords = GetEntityCoords(targetCoords) + end + elseif type(targetCoords) == "string" then + local targetEntityData = Bridge.Entity.Get(targetCoords) + if targetEntityData then + targetCoords = GetEntityCoords(targetEntityData.spawned) + end + end + local entityPos = GetEntityCoords(entity) + local distance = #(targetCoords - entityPos) + if distance > (entityData.follow.distance or Follow.default.distance) then + if entityData.anim then + entityData.anim.disable = true + Bridge.Entity.Set(entityData.id, "anim", entityData.anim) + end + if entityData.scenarios then + entityData.scenarios.disable = true + Bridge.Entity.Set(entityData.id, "scenarios", entityData.scenarios) + end + local speed = entityData.follow.speed or Follow.default.speed + local heading = GetHeadingFromVector_2d(targetCoords.x - entityPos.x, targetCoords.y - entityPos.y) + TaskGoStraightToCoord(entity, targetCoords.x, targetCoords.y, targetCoords.z, speed, -1, heading, 0.0) + entityData.coords = GetEntityCoords(entity) + entityData.follow.walking = true + Bridge.Entity.Set(entityData.id, "coords", entityData.coords - vector3(0,0,1)) + Bridge.Entity.Set(entityData.id, "follow", entityData.follow) + if entityData.follow.OnExit then entityData.follow.OnExit(entityData) end + return false + end + if not entityData.follow.walking then return end + entityData.follow.walking = false + Bridge.Entity.Set(entityData.id, "follow", entityData.follow) + if entityData.anim then + entityData.anim.disable = false + Bridge.Entity.Set(entityData.id, "anim", entityData.anim) + end + if entityData.scenarios then + entityData.scenarios.disable = false + Bridge.Entity.Set(entityData.id, "scenarios", entityData.scenarios) + end + ClearPedTasks(entity) + if entityData.follow.OnEnter then entityData.follow.OnEnter(entityData) end + return true +end + + +function Follow.OnUpdate(entityData) + assert(entityData?.id, "Entity ID is required") + local entity = entityData.spawned + if not entity or not DoesEntityExist(entity) then return end + return Follow.PedWalkToPos(entityData) +end + +Bridge.Entity.RegisterBehavior("follow", Follow) +return Follow \ No newline at end of file diff --git a/lib/entities/shared/behaviors/particles.lua b/lib/entities/shared/behaviors/particles.lua new file mode 100644 index 0000000..1160360 --- /dev/null +++ b/lib/entities/shared/behaviors/particles.lua @@ -0,0 +1,80 @@ +if IsDuplicityVersion() then return end +Utility = Utility or Bridge.Utility +local ParticlesBehaviors = { + property = "particles", + default = { + dict = "cut_exile3", + ptfx = "cs_ex3_wheel_spin", + offset = vector3(0, 0, 0), + rotation = vector3(0, 0, 0), + size = 1.0, + color = vector3(255, 255, 255), + loopLength = 5000, + } + +} + +function ParticlesBehaviors.Start(entityData) + for _, particleData in ipairs(entityData.particles) do + local position = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) + local rotation = vector3(entityData.rotation.x, entityData.rotation.y, entityData.rotation.z) + position = position + (particleData.offset?.xyz or vector3(0, 0, 0)) + rotation = rotation + (particleData.rotation?.xyz or vector3(0, 0, 0)) + particleData.playing = Bridge.Particle.Play( + particleData.dict or ParticlesBehaviors.default.dict, + particleData.ptfx or ParticlesBehaviors.default.ptfx, + position, + rotation, + particleData.size or ParticlesBehaviors.default.size, + particleData.color or ParticlesBehaviors.default.color, + particleData.looped, + particleData.loopLength or ParticlesBehaviors.default.loopLength + ) + end +end + +function ParticlesBehaviors.OnSpawn(entityData) + if not entityData.particles then return end + if entityData.particles.dict then -- single particle + entityData.particles = {entityData.particles} + end + ParticlesBehaviors.Start(entityData) +end + + +function ParticlesBehaviors.OnRemove(entityData) + if not entityData.particles then return end + for _, particleData in ipairs(entityData.particles) do + if particleData.playing then + Bridge.Particle.Stop(particleData.playing) + particleData.playing = nil + end + end +end + +function ParticlesBehaviors.OnMove(entityData) + if not entityData.particles then return end + for _, particleData in ipairs(entityData.particles) do + if particleData.playing then + Particle.Stop(particleData.playing) + particleData.playing = nil + local position = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) + local rotation = vector3(entityData.rotation.x, entityData.rotation.y, entityData.rotation.z) + position = position + (particleData.offset?.xyz or vector3(0, 0, 0)) + rotation = rotation + (particleData.rotation?.xyz or vector3(0, 0, 0)) + particleData.playing = Bridge.Particle.Play( + particleData.dict or ParticlesBehaviors.default.dict, + particleData.ptfx or ParticlesBehaviors.default.ptfx, + position, + rotation, + particleData.size or ParticlesBehaviors.default.size, + particleData.color or ParticlesBehaviors.default.color, + particleData.looped, + particleData.loopLength or ParticlesBehaviors.default.loopLength + ) + end + end +end + +Bridge.Entity.RegisterBehavior("particles", ParticlesBehaviors) +return ParticlesBehaviors \ No newline at end of file diff --git a/lib/entities/shared/behaviors/scenarios.lua b/lib/entities/shared/behaviors/scenarios.lua new file mode 100644 index 0000000..760af6c --- /dev/null +++ b/lib/entities/shared/behaviors/scenarios.lua @@ -0,0 +1,64 @@ + +if IsDuplicityVersion() then return end +local Scenarios = { + property = "scenarios", + default = { + introClip = true, + duration = -1 + } +} +-- scenarios.Play(id, entity, scenariosDict, scenariosName, blendIn, blendOut, duration, flag, playbackRate, onComplete) +function Scenarios.Play(entityData) + if entityData.entityType ~= "ped" then return end + local entity = entityData.spawned + if not entity then return end + local name = entityData.scenarios?.name + if not name then return end + local duration = entityData.scenarios?.duration or Scenarios.default.duration + local introClip = entityData.scenarios?.introClip or Scenarios.default.introClip + TaskStartScenarioInPlace(entity, name, duration, introClip) + if duration < 0 then return end + SetTimeout(duration, function() + if not entityData.scenarios then return end + ClearPedTasks(entity) + if not entityData.scenarios.onComplete then return end + entityData.scenarios.disabled = true + Bridge.Entity.Set(entityData.id, "scenarios", entityData.scenarios) + entityData.scenarios.onComplete(entityData) + end) + +end + +function Scenarios.OnSpawn(entityData) + if not entityData.spawned or not entityData.scenarios then return end + Scenarios.Play(entityData) +end + +function Scenarios.OnRemove(entityData) + if not entityData.spawned or not entityData.scenarios then return end + ClearPedTasks(entityData.spawned) +end + +function Scenarios.OnUpdate(entityData) + if not entityData.spawned or not entityData.scenarios then return end + if entityData.scenarios.disable then return end + if IsPedUsingScenario(entityData.spawned, entityData.scenarios.name) then return end + local entities = GetGamePool('CObject') + for _, obj in ipairs(entities or {}) do + obj = tonumber(obj) + local entityCoords = GetEntityCoords(obj) + local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) + if entityCoords then + local distance = #(entityCoords - coords) + if distance < 2.0 then + SetEntityAsMissionEntity(obj, false, false) + DeleteObject(obj) + break + end + end + end + Scenarios.Play(entityData) +end + +Bridge.Entity.RegisterBehavior("scenarios", Scenarios) +return Scenarios \ No newline at end of file diff --git a/lib/entities/shared/behaviors/shop.lua b/lib/entities/shared/behaviors/shop.lua new file mode 100644 index 0000000..306752c --- /dev/null +++ b/lib/entities/shared/behaviors/shop.lua @@ -0,0 +1,58 @@ +local Shop = { + property = "shop", + default = { + label = "Default Shop Label", + items = { + {item = "water", price = 10} + }, + target = { + label = "Open Shop", + icon = "fa-solid fa-box" + } + } +} + +function Shop.OnCreate(entityData) + local shop = entityData.shop + if not shop then return end + if IsDuplicityVersion() then + local label = shop.label or Shop.default.label + local items = shop.items or Shop.default.items + local coords = entityData.coords + local groups = shop.groups + Bridge.Inventory.RegisterShop(label, items, nil, groups) + return + end + entityData.targets = entityData.targets or {} + print(json.encode(entityData.targets)) + table.insert(entityData.targets, + { + label = shop.target.label or "Shop", + icon = shop.target.icon or "fa-solid fa-box", + onSelect = function() + TriggerServerEvent("community_bridge:server:OpenShop", entityData.id) + end + } + ) +end + +if not IsDuplicityVersion() then return Bridge.Entity.RegisterBehavior("shop", Shop) end + +RegisterNetEvent("community_bridge:server:OpenShop", function(id) + local src = source + if not src then return end + local entityData = Bridge.Entity.Get(id) + if not entityData or not entityData.shop then + return print(string.format("[Shop] OpenShop: Entity %s does not exist or has no shop", id)) + end + local coords = entityData.coords + if not coords then return end + local distance = #(GetEntityCoords(GetPlayerPed(src)) - coords) + if distance > 3.0 then + return print(string.format("[Shop] OpenShop: Player %s is too far from entity %s", src, id)) + end + Bridge.Inventory.OpenShop(src, id) +end) + + +return Shop \ No newline at end of file diff --git a/lib/entities/shared/behaviors/stash.lua b/lib/entities/shared/behaviors/stash.lua new file mode 100644 index 0000000..badbebc --- /dev/null +++ b/lib/entities/shared/behaviors/stash.lua @@ -0,0 +1,61 @@ +local Stash = { + property = "stash", + default = { + label = "default_stash_label", + slots = 20, + maxWeight = 100000, + target = { + label = "Open Stash", + icon = "fa-solid fa-box", + } + } +} + +function Stash.OnCreate(entityData) + if not entityData.stash then return end + if IsDuplicityVersion() then + local label = entityData.stash.label or Stash.default.label + local slots = entityData.stash.slots or Stash.default.slots + local weight = entityData.stash.maxWeight or Stash.default.maxWeight + local owner = entityData.stash.owner + local groups = entityData.stash.groups + Bridge.Inventory.RegisterStash(entityData.id, label, slots, weight, owner, groups) + return + end + entityData.targets = entityData.targets or {} + entityData.targets['stash'] = { + label = entityData.stash.target.label or Stash.default.target.label, + icon = entityData.stash.target.icon or Stash.default.target.icon, + description = entityData.stash.target.description, + groups = entityData.stash.target.groups, + canInteract = function(_) + local entData = Bridge.Entity.Get(entityData.id) + return not entData.stash.disable + end, + onSelect = function() + TriggerServerEvent("community_bridge:server:OpenStash", entityData.id) + end + } + + Bridge.Entity.Set(entityData.id, 'targets', entityData.targets) +end + +if not IsDuplicityVersion() then return Bridge.Entity.RegisterBehavior("stash", Stash) end + +RegisterNetEvent("community_bridge:server:OpenStash", function(id) + local src = source + if not src then return end + local entityData = Bridge.Entity.Get(id) + if not entityData or not entityData.stash then + return print(string.format("[Stash] OpenStash: Entity %s does not exist or has no stash", id)) + end + local coords = vector3(entityData.coords.x, entityData.coords.y, entityData.coords.z) + if not coords then return end + local distance = #(GetEntityCoords(GetPlayerPed(src)) - coords) + if distance > 3.0 then + return print(string.format("[Stash] OpenStash: Player %s is too far from entity %s", src, id)) + end + Bridge.Inventory.OpenStash(src, "stash", id) +end) + +Bridge.Entity.RegisterBehavior("stash", Stash) \ No newline at end of file diff --git a/lib/entities/shared/behaviors/targets.lua b/lib/entities/shared/behaviors/targets.lua new file mode 100644 index 0000000..c11afc4 --- /dev/null +++ b/lib/entities/shared/behaviors/targets.lua @@ -0,0 +1,78 @@ +if IsDuplicityVersion() then return end +local Targets = { + property = "targets", + default = { + label = "Default Target Label", + distance = 2, + size = vector3(2.0, 2.0, 2.0), + } +} + +function Targets.OnSpawn(entityData) + if not entityData.targets then return end + if entityData.targets?.label then + local temp = entityData.targets + entityData.targets = {temp} + end + print("Spawned targets:", entityData.targets) + local numberedTargets = {} + for k, v in pairs(entityData.targets) do + local onSelect = v.onSelect + if onSelect then + v.onSelect = function(entity) + onSelect(entityData, entity) + end + end + table.insert(numberedTargets, v) + end + table.sort(numberedTargets, function(a, b) return (a.label < b.label) end) + if not entityData.spawned then + local size = Targets.default.size + Bridge.Target.AddBoxZone(entityData.id, entityData.coords, entityData.size, entityData.heading, numberedTargets, entityData.debug) + else + Bridge.Target.AddLocalEntity(entityData.spawned, numberedTargets) + end + entityData.oldTargets = entityData.targets + Bridge.Entity.Set(entityData.id, "oldTargets", entityData.oldTargets) +end + +function Targets.OnRemove(entityData) + if not entityData.oldTargets then return end + if not entityData.spawned then + return Bridge.Target.RemoveZone(entityData.id) + end + Bridge.Target.RemoveLocalEntity(entityData.spawned) +end + +function Targets.OnUpdate(entityData) + if not entityData.oldTargets then return end + local doesntMatch = false + for k, v in pairs(entityData.targets) do + if not entityData.oldTargets or not entityData.oldTargets[k] then + doesntMatch = true + break + end + local old = entityData.oldTargets[k] + if old.label ~= v.label + or old.distance ~= v.distance + or old.description ~= v.description + then + doesntMatch = true + break + end + end + + for k, v in pairs(entityData.oldTargets) do + if not entityData.targets or not entityData.targets[k] then + doesntMatch = true + break + end + end + if doesntMatch then + Targets.OnRemove(entityData) + Wait(100) + Targets.OnSpawn(entityData) + end +end +Bridge.Entity.RegisterBehavior("targets", Targets) +return Targets \ No newline at end of file diff --git a/lib/entities/shared/behaviors/weapon.lua b/lib/entities/shared/behaviors/weapon.lua new file mode 100644 index 0000000..24c6b7c --- /dev/null +++ b/lib/entities/shared/behaviors/weapon.lua @@ -0,0 +1,54 @@ +if IsDuplicityVersion() then return end +local Weapon = { + property = "weapon", + default = { + name = "WEAPON_UNARMED", + ammo = 0 + } +} + +function Weapon.GiveWeapon(entityData) + if entityData.entityType ~= "ped" then return end + local entity = entityData.spawned + + if not entity or not DoesEntityExist(entity) then return end + if entityData.weapon?.name then + local ammo = entityData.weapon.ammo or 0 + local hash = GetHashKey(entityData.weapon.name) + GiveWeaponToPed(entity, hash, ammo, false, true) + TaskSwapWeapon(entity, true) + end + entityData.oldWeapon = entityData.weapon + Bridge.Entity.Set(entityData.id, "oldWeapon", entityData.oldWeapon) +end + +function Weapon.OnSpawn(entityData) + if not entityData.spawned or not entityData.weapon then return end + Weapon.GiveWeapon(entityData) + entityData.weapon.updating = false +end + +function Weapon.OnRemove(entityData) + if not entityData.spawned or not entityData.weapon then return end + entityData.oldWeapon = nil + entityData.weapon.updating = true +end + +function Weapon.OnUpdate(entityData) + if not entityData.spawned or not entityData.weapon then return end + if entityData.weapon.updating then return end + entityData.weapon.updating = true + if not entityData.oldWeapon or entityData.oldWeapon.name ~= entityData.weapon.name then + Weapon.GiveWeapon(entityData) + end + SetTimeout(1000, function() + if entityData.weapon then + entityData.weapon.updating = false + Bridge.Entity.Set(entityData.id, "weapon", entityData.weapon) + end + end) + Bridge.Entity.Set(entityData.id, "weapon", entityData.weapon) +end + +Bridge.Entity.RegisterBehavior("weapon", Weapon) +return Weapon \ No newline at end of file diff --git a/lib/init.lua b/lib/init.lua index e8676e0..48cbce8 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -59,7 +59,11 @@ cLib.Cache = Cache or Require("lib/cache/shared/cache.lua") cLib.Marker = Marker or Require("lib/markers/server/server.lua") cLib.Particle = Particle or Require("lib/particles/server/particles.lua") cLib.Shell = Shells or Require("lib/shells/server/shells.lua") -cLib.ServerEntity = ServerEntity or Require("lib/entities/server/server_entity.lua") +cLib.Entity = ServerEntity or Require("lib/entities/server/server_entity.lua") + +-- Depricated +cLib.ServerEntity = cLib.Entity + if IsDuplicityVersion() then return cLib end ::client:: @@ -71,10 +75,13 @@ cLib.Raycast = Raycast or Require("lib/raycast/client/raycast.lua") cLib.Point = Point or Require("lib/points/client/points.lua") cLib.Particle = Particle or Require("lib/particles/client/particles.lua") cLib.Cache = Cache or Require("lib/cache/client/cache.lua") -cLib.ClientEntity = ClientEntity or Require("lib/entities/client/client_entity.lua") cLib.Marker = Marker or Require("lib/markers/client/markers.lua") cLib.Anim = Anim or Require("lib/anim/client/client.lua") cLib.Cutscene = Cutscene or Require("lib/cutscenes/client/cutscene.lua") cLib.Particle = Particle or Require("lib/particles/client/particles.lua") +cLib.Entity = ClientEntity or Require("lib/entities/client/client_entity.lua") + +-- Deprecated +cLib.ClientEntity = cLib.Entity return cLib 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.