Skip to content

Commit

Permalink
feat: separate network entities
Browse files Browse the repository at this point in the history
  • Loading branch information
swkeep committed Jun 22, 2024
1 parent 464b7c9 commit c0ca710
Show file tree
Hide file tree
Showing 3 changed files with 220 additions and 6 deletions.
6 changes: 2 additions & 4 deletions interactionMenu/lua/client/interact.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ MenuTypes = Util.ENUM {
}

local Wait = Wait
local SetDrawOrigin = SetDrawOrigin
local DrawSprite = DrawSprite
local ClearDrawOrigin = ClearDrawOrigin
local PlayerPedId = PlayerPedId
local GetEntityCoords = GetEntityCoords
local SetScriptGfxDrawBehindPausemenu = SetScriptGfxDrawBehindPausemenu
Expand All @@ -29,6 +26,7 @@ local pause = false
local scaleform_initialized = false
local scaleform
local SpatialHashGrid = Util.SpatialHashGrid
--

local grid_zone = SpatialHashGrid:new('zone', 100)
local grid_position = SpatialHashGrid:new('position', 100)
Expand Down Expand Up @@ -114,7 +112,7 @@ function Interact:setVisibility(id, value)
scaleform.send("interactionMenu:menu:setVisibility", { id = id, visibility = value })
end

function Interact:SetDarkMode(value)
function Interact:setDarkMode(value)
scaleform.send("interactionMenu:darkMode", value)
end

Expand Down
29 changes: 27 additions & 2 deletions interactionMenu/lua/client/menuContainer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Container = {
indexes = {
models = {},
entities = {},
netIds = {},
players = {},
bones = {},
globals = {
Expand Down Expand Up @@ -209,6 +210,12 @@ local function classifyMenuInstance(instance)
models[model] = models[model] or {}

table.insert(models[model], instance.id)
elseif instance.type == 'netId' then
local netIds = indexes.netIds
local netId = instance.netId
netIds[netId] = netIds[netId] or {}

table.insert(netIds[netId], instance.id)
elseif instance.player then
local players = indexes.players
players[instance.player] = players[instance.player] or {}
Expand Down Expand Up @@ -308,6 +315,9 @@ function Container.create(t)
elseif t.model then
instance.type = 'model'
instance.model = t.model
elseif t.netId then
instance.type = 'netId'
instance.netId = t.netId
elseif t.bone then
instance.type = 'bone'
instance.bone = t.bone
Expand Down Expand Up @@ -578,6 +588,14 @@ function Container.getMenu(model, entity, menuId)
Util.table_merge(combinedIds, Container.indexes.players[playerId] or {})
end

local networked = NetworkGetEntityIsNetworked(entity)
local netId

if networked then
netId = NetworkGetNetworkIdFromEntity(entity)
Util.table_merge(combinedIds, Container.indexes.netIds[netId] or {})
end

-- bone
local bones = Container.indexes.bones[entity]
if bones and closestBoneName then
Expand Down Expand Up @@ -621,14 +639,21 @@ function Container.getMenuType(t)
local entities = Container.indexes.entities
local models = Container.indexes.models
local players = Container.indexes.players
local netIds = Container.indexes.netIds
local playerId = IdentifyPlayerServerId(entityType, entity)
local networked = NetworkGetEntityIsNetworked(entity)
local netId

if networked then
netId = NetworkGetNetworkIdFromEntity(entity)
end

if t.zone then
return MenuTypes['ON_ZONE']
elseif t.closestPoint and next(t.closestPoint) then
-- onPosition
return MenuTypes['ON_POSITION']
elseif (entityType == 3 or entityType == 2) and models[model] or entities[entity] or players[playerId] or globalsExistsCheck(entity, entityType) then
elseif (entityType == 3 or entityType == 2) and models[model] or entities[entity] or players[playerId] or globalsExistsCheck(entity, entityType) or netIds[netId] then
-- onModel / onEntity / onBone
return MenuTypes['ON_ENTITY']
else
Expand Down Expand Up @@ -1038,7 +1063,7 @@ function Container.syncData(scaleform, menuData, refreshUI)

if Config.features.timeBasedTheme and is_daytime() ~= previous_daytime then
previous_daytime = is_daytime()
Interact:SetDarkMode(previous_daytime)
Interact:setDarkMode(previous_daytime)
end
end

Expand Down
191 changes: 191 additions & 0 deletions interactionMenu/lua/examples/onEntites.lua
Original file line number Diff line number Diff line change
Expand Up @@ -722,4 +722,195 @@ CreateThread(function()
SetTimeout(4000, function()
exports['interactionMenu']:remove(remove_test)
end)

ped_position = vector4(-2006.71, 3167.59, 31.81, 355.17)
ped_ = Util.spawnPed(GetHashKey('a_c_husky'), ped_position)

FreezeEntityPosition(ped_, false)

local function loadAnim(animation)
RequestAnimDict(animation)
while not HasAnimDictLoaded(animation) do
Citizen.Wait(100)
end
return true
end

local function faceMe(entity1, entity2)
local p1 = GetEntityCoords(entity1, true)
local p2 = GetEntityCoords(entity2, true)

local dx = p2.x - p1.x
local dy = p2.y - p1.y

local heading = GetHeadingFromVector_2d(dx, dy)
TaskAchieveHeading(entity1, heading, 1500)
Wait(1000)
end


local function goThere(ped)
local target = vector3(-2022.15, 3176.92, 32.81)
local start_position = GetEntityCoords(ped)

TaskGoToCoordAnyMeans(ped, target, 10.0, 0, 0, 0, 0)
while true do
local current_position = GetEntityCoords(ped)
if #(current_position - target) < 3 then
break
end

Wait(100)
end
TaskGoToCoordAnyMeans(ped, start_position, 10.0, 0, 0, 0, 0)
while true do
local current_position = GetEntityCoords(ped)
if #(current_position - start_position) < 3 then
break
end

Wait(100)
end
faceMe(ped, PlayerPedId())
end

local function SetRelationshipBetweenPed(ped)
if not ped then
return
end
-- note: if we don't do this they will star fighting among themselves!
RemovePedFromGroup(ped)
SetPedRelationshipGroupHash(ped, GetHashKey(ped))
SetCanAttackFriendly(ped, false, false)
end

local function AttackTargetedPed(AttackerPed, targetPed)
ClearPedTasks(AttackerPed)
if not AttackerPed and not targetPed then
return false
end
SetPedCombatAttributes(AttackerPed, 46, 1)
TaskGoToEntityWhileAimingAtEntity(AttackerPed, targetPed, targetPed, 8.0, 1, 0, 15, 1, 1, 1566631136)
TaskCombatPed(AttackerPed, targetPed, 0, 16)
SetRelationshipBetweenPed(AttackerPed)
SetPedCombatMovement(AttackerPed, 3)
end

local sit = false
local lookat_me = false
local previousPos = nil

exports['interactionMenu']:Create {
entity = ped_,
offset = vector3(0, 0, 0.4),
options = {
{
label = "Health",
progress = {
type = "info",
value = 0,
percent = true
},
bind = {
func = function(entity, distance, coords, name, bone)
local max_hp = 100 - GetPedMaxHealth(entity)
local current_hp = 100 - GetEntityHealth(entity)
return math.floor(current_hp * 100 / max_hp)
end
}
},
{
label = 'Show them who\'s boss',
action = {
type = 'async',
func = function(entity)
local target = vector4(-2021.57, 3181.07, 31.81, 247.2)
-- local myTarget = Util.spawnPed(GetHashKey('A_C_shepherd'), target)
local myTarget = Util.spawnPed(GetHashKey('cs_brad'), target)
local start_position = GetEntityCoords(entity)
FreezeEntityPosition(myTarget, false)
AttackTargetedPed(myTarget, entity)
AttackTargetedPed(entity, myTarget)

while not IsPedDeadOrDying(myTarget, true) do
Wait(1000)
end
TaskGoToCoordAnyMeans(entity, start_position, 10.0, 0, 0, 0, 0)
Wait(2000)

DeleteEntity(myTarget)
end
}
},
{
label = 'Face Me',
action = {
type = 'sync',
func = function(entity)
faceMe(entity, PlayerPedId())
end
}
},
{
label = 'Face me until I tell you to stop',
action = {
type = 'sync',
func = function(entity)
if lookat_me then
print('stop')
lookat_me = false
return
end
print('start')
lookat_me = true

CreateThread(function()
while lookat_me do
local currentPos = GetEntityCoords(PlayerPedId(), true)

if not previousPos or (currentPos.x ~= previousPos.x or currentPos.y ~= previousPos.y or currentPos.z ~= previousPos.z) then
faceMe(entity, PlayerPedId())
previousPos = currentPos
end

Wait(600)
end
end)
end
}
},
{
label = 'Sit/Stand',
action = {
type = 'sync',
func = function(entity)
local dict = 'creatures@retriever@amb@world_dog_sitting@idle_a'
local anim = 'idle_c'
if not sit then
loadAnim(dict)
local flag = 0
TaskPlayAnim(entity, dict, anim, 8.0, 0, -1, flag or 1, 0, 0, 0, 0)
print('play')
Wait(1000)
sit = true
else
StopAnimTask(entity, dict, anim, 1.0)
print('stop')
Wait(1000)
sit = false
end
end
}
},
{
label = 'Move',
action = {
type = 'sync',
func = function(entity)
goThere(entity)
end
}
},
}
}
end)

0 comments on commit c0ca710

Please sign in to comment.