Files
community_bridge/lib/entities/shared/behaviors/follow.lua
T
Mr.Crowley 8346130b00 Add modular entity behaviors and update entity logic
Introduces modular behaviors for entities, including anim, attach, clothing, follow, particles, scenarios, shop, stash, targets, and weapon, each in their own files. Updates client_entity.lua and behaviors.lua to support dynamic behavior registration, initialization, and triggering. Refactors entity update logic to improve movement handling and behavior invocation.
2025-09-03 00:28:17 -05:00

75 lines
3.0 KiB
Lua

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.ClientEntity.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.ClientEntity.Set(entityData.id, "anim", entityData.anim)
end
if entityData.scenarios then
entityData.scenarios.disable = true
Bridge.ClientEntity.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.ClientEntity.Set(entityData.id, "coords", entityData.coords)
Bridge.ClientEntity.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.ClientEntity.Set(entityData.id, "follow", entityData.follow)
if entityData.anim then
entityData.anim.disable = false
Bridge.ClientEntity.Set(entityData.id, "anim", entityData.anim)
end
if entityData.scenarios then
entityData.scenarios.disable = false
Bridge.ClientEntity.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.ClientEntity.RegisterBehavior("follow", Follow)
return Follow