1
0
Fork 0
forked from Simnation/Main
Main/resources/[tools]/nordi_talk_to_npc/client/client.lua

143 lines
4.1 KiB
Lua
Raw Normal View History

2025-07-20 20:44:58 +02:00
local QBCore = exports['qb-core']:GetCoreObject()
local spawnedNPCs = {}
-- NPC Spawn Function
local function SpawnNPC(npcId, npcData)
local model = GetHashKey(npcData.model)
RequestModel(model)
while not HasModelLoaded(model) do
Wait(1)
end
local npc = CreatePed(4, model, npcData.coords.x, npcData.coords.y, npcData.coords.z - 1.0, npcData.coords.w, false, true)
SetEntityHeading(npc, npcData.coords.w)
FreezeEntityPosition(npc, true)
SetEntityInvincible(npc, true)
SetBlockingOfNonTemporaryEvents(npc, true)
-- QB-Target hinzufügen
exports['qb-target']:AddTargetEntity(npc, {
options = {
{
type = 'client',
event = 'npc-dialog:client:openDialog',
icon = npcData.targetIcon,
label = npcData.targetLabel,
npcId = npcId
}
},
distance = 2.0
})
spawnedNPCs[npcId] = npc
SetModelAsNoLongerNeeded(model)
end
-- Alle NPCs spawnen
CreateThread(function()
for npcId, npcData in pairs(Config.NPCs) do
SpawnNPC(npcId, npcData)
end
end)
-- Rekursive Funktion zum Erstellen der Dialog-Optionen
local function CreateDialogOptions(options, npcId, parentId)
local contextOptions = {}
for i, option in ipairs(options) do
local optionData = {
title = option.title,
description = option.description,
icon = option.icon,
}
if option.info then
-- Direkte Info anzeigen
optionData.onSelect = function()
lib.alertDialog({
header = option.title,
content = option.info,
centered = true,
cancel = true,
labels = {
cancel = 'Zurück'
}
})
end
elseif option.response then
-- Weiteres Dialog-Level
optionData.onSelect = function()
local contextId = 'npc_dialog_' .. npcId .. '_' .. i .. '_' .. (parentId or 'main')
local responseOptions = CreateDialogOptions(option.response.options, npcId, contextId)
-- Zurück-Option hinzufügen
table.insert(responseOptions, {
title = "← Zurück",
description = "Zum vorherigen Dialog",
icon = 'arrow-left',
onSelect = function()
if parentId then
lib.showContext(parentId)
else
lib.showContext('npc_dialog_' .. npcId)
end
end
})
lib.registerContext({
id = contextId,
title = option.response.title,
options = responseOptions
})
lib.showContext(contextId)
end
end
table.insert(contextOptions, optionData)
end
return contextOptions
end
-- Dialog öffnen
RegisterNetEvent('npc-dialog:client:openDialog', function(data)
local npcId = data.npcId
local npcData = Config.NPCs[npcId]
if not npcData then return end
local options = CreateDialogOptions(npcData.dialog.options, npcId)
-- Verlassen Option hinzufügen
table.insert(options, {
title = "Verlassen",
description = "Dialog beenden",
icon = 'times',
onSelect = function()
-- Dialog schließt sich automatisch
end
})
lib.registerContext({
id = 'npc_dialog_' .. npcId,
title = npcData.dialog.title,
options = options
})
lib.showContext('npc_dialog_' .. npcId)
end)
-- Cleanup beim Resource Stop
AddEventHandler('onResourceStop', function(resourceName)
if GetCurrentResourceName() ~= resourceName then return end
for _, npc in pairs(spawnedNPCs) do
if DoesEntityExist(npc) then
DeleteEntity(npc)
end
end
end)