好的,我们可以扩展与外卖老板的对话功能,让玩家与老板进行多轮交互,以增加游戏的趣味性。以下是如何修改代码以实现这一点,包括对话框和选择选项的实现。
### config.lua
在配置中添加对话内容,具体实现可以根据需要进行修改:
Config = {}
-- 送餐地点及相应的物品
Config.DeliveryLocations = {
{x = 200.0, y = -1000.0, z = 30.0, item = "pizza"},
{x = 500.0, y = -1500.0, z = 30.0, item = "burger"},
{x = -400.0, y = -2000.0, z = 30.0, item = "drink"}
}
-- 可用车辆配置
Config.Vehicles = {
"pizza",
"bicycle",
"van"
}
-- 外卖老板NPC坐标
Config.DeliveryBossNPC = { x = 250.0, y = -1500.0, z = 30.0 }
-- 外卖老板的对话内容
Config.Dialogues = {
"这个工作好不好做?",
"送餐有时候挺累的,但我就是喜欢。",
"你觉得外卖公司怎么样?",
"希望你能给我带来更多的客户!"
}
### server.lua
我们可以扩展服务器端逻辑来处理对话内容,并在客户端与外卖老板交互时展示对话:
local deliveries = {}
RegisterServerEvent('delivery:startDelivery')
AddEventHandler('delivery:startDelivery', function()
local src = source
local randomIndex = math.random(#Config.DeliveryLocations) -- 随机选择一个送餐地点
local deliveryLocation = Config.DeliveryLocations[randomIndex]
-- 将物品添加到玩家的库存
exports.ox_inventory:AddItem(src, deliveryLocation.item, 1)
TriggerClientEvent('delivery:sendDeliveryLocation', src, deliveryLocation)
end)
RegisterServerEvent('delivery:getDialogue')
AddEventHandler('delivery:getDialogue', function()
local src = source
local randomDialogue = Config.Dialogues[math.random(#Config.Dialogues)]
TriggerClientEvent('delivery:showDialogue', src, randomDialogue)
end)
-- (其余代码保持不变)
### client.lua
在客户端部分,我们扩展了与外卖老板的对话功能:
local delivering = false
local currentDeliveryLocation = nil
-- 创建 NPC 的函数
function CreateDeliveryBoss()
RequestModel("a_m_m_business_01")
while not HasModelLoaded("a_m_m_business_01") do
Wait(500)
end
local boss = CreatePed(4, "a_m_m_business_01", Config.DeliveryBossNPC.x, Config.DeliveryBossNPC.y, Config.DeliveryBossNPC.z, 0.0, false, true)
SetEntityAsMissionEntity(boss, true, true)
-- 使用 ox_target 为 NPC 添加交互
exports.ox_target:addLocalEntity(boss, {
{
name = "TalkToDeliveryBoss",
icon = "fas fa-comment-dots",
label = "与外卖老板交谈",
onSelect = function()
TalkToDeliveryBoss()
end,
},
})
end
-- 和外卖老板交流
function TalkToDeliveryBoss()
TriggerServerEvent('delivery:getDialogue') -- 请求对话
end
-- 接收送餐地点
RegisterNetEvent('delivery:sendDeliveryLocation')
AddEventHandler('delivery:sendDeliveryLocation', function(location)
currentDeliveryLocation = location
delivering = true
SetNewWaypoint(location.x, location.y)
end)
-- 接收并显示对话
RegisterNetEvent('delivery:showDialogue')
AddEventHandler('delivery:showDialogue', function(dialogue)
BeginTextCommandDisplayHelp("STRING")
AddTextComponentSubstringPlayerName(dialogue)
EndTextCommandDisplayHelp(0, false, true, -1)
Citizen.Wait(2000) -- 显示2秒后自动消失
end)
-- 结束送餐
function EndDelivery()
if delivering then
delivering = false
TriggerServerEvent('delivery:endDelivery')
end
end
-- (其余代码保持不变)
### 总结
现在,玩家与外卖老板的对话扩展到了多条信息。在玩家与 NPC 互动时,会随机选择一条对话内容展示给玩家。你可以根据需要在 `config.lua` 中添加更多对话内容,使得交互更加丰富。 |