搜索
12
返回列表 发新帖
楼主: lizhuoda
收起左侧

写一个插件

[复制链接]

升级进度: 41%

 楼主| lizhuoda 发表于 2024-8-12 19:45:37 | 显示全部楼层
ChatGPT 发表于 2024-8-12 19:44
好的,我们可以在任务完成后使玩家生成的车辆消失。要实现这个功能,我们可以在检测到玩家到达送餐地点时 ...

与老板可以增加聊天
回复

使用道具 举报

ChatGPT 发表于 2024-8-12 19:46:12 | 显示全部楼层
lizhuoda 发表于 2024-08-12 19:45
与老板可以增加聊天


好的,我们可以在与商店老板的交互中增加更多的聊天内容,以丰富玩家的体验。我们可以为每个任务设置不同的对话选项或者让老板有随机的对话内容。下面是更新后的代码,实现与老板交谈时增加多种聊天内容。

### 更新后的客户端脚本 `client.lua`

以下是代码示例,增加了聊天内容。
local isDelivering = false
local deliveryLocation = nil
local selectedVehicle = nil
local keyIdentifier = 'delivery_temp_key' -- 钥匙标识符
local spawnedVehicle = nil -- 用于存放生成的车辆

-- 创建 NPC 交互目标
Citizen.CreateThread(function()
    for _, npc in ipairs(Config.NPCs) do
        local ped = CreatePed(0, GetHashKey("csb_mweather"), npc.coords.x, npc.coords.y, npc.coords.z, 0.0, false, true)

        -- 使用 ox_target 创建目标
        exports.ox_target:addLocalEntity(ped, {
            {
                name = "与商店老板交谈",
                icon = "fa-solid fa-comment",
                onInteract = function()
                    if not isDelivering then
                        OpenChatMenu()  -- 打开聊天菜单
                    else
                        TriggerEvent('chat:addMessage', { args = { "你已经有一个送餐任务了!" } })
                    end
                end
            }
        })
    end
end)

-- 打开聊天菜单
function OpenChatMenu()
    local chatOptions = {
        {
            title = "请给我一些任务。",
            description = "请求老板提供任务。",
            action = function()
                OpenTaskMenu() -- 请求任务
            end
        },
        {
            title = "你最近怎么样?",
            description = "询问老板的近况。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 我最近的生意不错,谢谢关心!" } })
            end
        },
        {
            title = "你的外卖服务快吗?",
            description = "询问配送速度。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 我们会尽量做到快速送达!" } })
            end
        },
        {
            title = "有其他工作吗?",
            description = "询问其他可能的工作机会。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 现在没有,但欢迎随时来咨询。" } })
            end
        },
        {
            title = "谢谢再见!",
            description = "结束对话。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "你告别了老板,准备去接任务。" } })
            end
        }
    }

    -- 使用 ox_target 显示聊天选择菜单
    exports.ox_target:openSimpleMenu(chatOptions)
end

-- 打开任务菜单
function OpenTaskMenu()
    local menuOptions = {}

    for i, location in ipairs(Config.DeliveryLocations) do
        table.insert(menuOptions, {
            title = "送餐任务 #" .. i,
            description = "前往指定位置送餐",
            action = function()
                OpenVehicleMenu(location)  -- 在选择任务后打开车辆选择菜单
            end
        })
    end

    -- 使用 ox_target 显示任务选择菜单
    exports.ox_target:openSimpleMenu(menuOptions)
end

-- 打开车辆选择菜单
function OpenVehicleMenu(location)
    local vehicleOptions = {}

    for _, vehicle in ipairs(Config.Vehicles) do
        table.insert(vehicleOptions, {
            title = vehicle.name,
            description = "选择这辆车进行送餐",
            action = function()
                StartDelivery(location, vehicle.model)  -- 启动送餐任务并选择车辆
            end
        })
    end

    -- 使用 ox_target 显示车辆选择菜单
    exports.ox_target:openSimpleMenu(vehicleOptions)
end

-- 开始送餐任务
function StartDelivery(location, vehicleModel)
    isDelivering = true
    deliveryLocation = location
    selectedVehicle = vehicleModel
    TriggerEvent('chat:addMessage', { args = { "你已经接到送餐任务,请前往目的地:" .. tostring(deliveryLocation) } })

    -- 给予临时钥匙
    GiveTempKey()
    SpawnVehicle(vehicleModel) -- 生成所选的车辆
end

-- 给予临时钥匙使用 renzu_garage 插件
function GiveTempKey()
    local playerId = PlayerId() -- 获取玩家ID
    TriggerServerEvent('renzu_garage:addKey', playerId, keyIdentifier) -- 向服务器请求发放钥匙
end

-- 生成所选车辆
function SpawnVehicle(vehicleModel)
    local playerPed = PlayerPedId()
    local coords = GetEntityCoords(playerPed)

    RequestModel(vehicleModel)
    while not HasModelLoaded(vehicleModel) do
        Citizen.Wait(500)
    end

    local vehicle = CreateVehicle(vehicleModel, coords.x, coords.y, coords.z, GetEntityHeading(playerPed), true, false)
    TaskWarpPedIntoVehicle(playerPed, vehicle, -1)
    spawnedVehicle = vehicle -- 将生成的车辆存储在变量中
end

-- 监测角色到达送餐地点
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(1000)
        if isDelivering and deliveryLocation then
            local playerCoords = GetEntityCoords(PlayerPedId())
            if Vdist(playerCoords, deliveryLocation) < 5.0 then
                TriggerEvent('chat:addMessage', { args = { "你已到达送餐地点,任务完成!" } })
                -- 撤销钥匙
                RemoveTempKey()
                DeleteSpawnedVehicle() -- 删除生成的车辆
                isDelivering = false
                deliveryLocation = nil
                selectedVehicle = nil
            end
        end
    end
end)

-- 撤销临时钥匙使用 renzu_garage 插件
function RemoveTempKey()
    local playerId = PlayerId()
    TriggerServerEvent('renzu_garage:removeKey', playerId, keyIdentifier) -- 向服务器请求撤销钥匙
end

-- 删除生成的车辆
function DeleteSpawnedVehicle()
    if spawnedVehicle and DoesEntityExist(spawnedVehicle) then
        DeleteVehicle(spawnedVehicle) -- 删除车辆
        spawnedVehicle = nil -- 清空变量
    end
end

### 代码说明

1. **聊天选项**:在 `OpenChatMenu` 函数中定义了一些聊天选项,每个选项都有对应的响应。当玩家选择某个选项时,触发相应的事件显示老板的回应。
   
2. **请求任务**:当玩家选择“请给我一些任务。”时,将调用 `OpenTaskMenu()` 函数让他们选择任务。
   
3. **其他对话**:提供了关于最近情况、配送速度、其他工作机会等的聊天选项。

### 总结

通过这种方式,玩家在与老板交谈时可以选择不同的对话,增加了游戏的互动性和趣味性。如果你还有其他需求或希望增加更多的功能,请随时告诉我!
回复

使用道具 举报

升级进度: 41%

 楼主| lizhuoda 发表于 2024-8-12 19:48:45 | 显示全部楼层
ChatGPT 发表于 2024-8-12 19:46
好的,我们可以在与商店老板的交互中增加更多的聊天内容,以丰富玩家的体验。我们可以为每个任务设置不同 ...

在和老板聊天之后可接取任务
回复

使用道具 举报

ChatGPT 发表于 2024-8-12 19:53:27 | 显示全部楼层
lizhuoda 发表于 2024-08-12 19:48
在和老板聊天之后可接取任务


好的,我们可以将聊天流程与任务接取流程结合起来,使得玩家在与老板聊天后能够选择接取任务。下面是更新后的代码,实现了这一功能。

### 更新后的客户端脚本 `client.lua`

以下是代码示例,增加了与老板聊天后的任务接取逻辑:
local isDelivering = false
local deliveryLocation = nil
local selectedVehicle = nil
local keyIdentifier = 'delivery_temp_key' -- 钥匙标识符
local spawnedVehicle = nil -- 用于存放生成的车辆

-- 创建 NPC 交互目标
Citizen.CreateThread(function()
    for _, npc in ipairs(Config.NPCs) do
        local ped = CreatePed(0, GetHashKey("csb_mweather"), npc.coords.x, npc.coords.y, npc.coords.z, 0.0, false, true)

        -- 使用 ox_target 创建目标
        exports.ox_target:addLocalEntity(ped, {
            {
                name = "与商店老板交谈",
                icon = "fa-solid fa-comment",
                onInteract = function()
                    if not isDelivering then
                        OpenChatMenu()  -- 打开聊天菜单
                    else
                        TriggerEvent('chat:addMessage', { args = { "你已经有一个送餐任务了!" } })
                    end
                end
            }
        })
    end
end)

-- 打开聊天菜单
function OpenChatMenu()
    local chatOptions = {
        {
            title = "请给我一些任务。",
            description = "请求老板提供任务。",
            action = function()
                OpenTaskMenu() -- 请求任务
            end
        },
        {
            title = "你最近怎么样?",
            description = "询问老板的近况。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 我最近的生意不错,谢谢关心!" } })
            end
        },
        {
            title = "你的外卖服务快吗?",
            description = "询问配送速度。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 我们会尽量做到快速送达!" } })
            end
        },
        {
            title = "有其他工作吗?",
            description = "询问其他可能的工作机会。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 现在没有,但欢迎随时来咨询。" } })
            end
        },
        {
            title = "谢谢再见!",
            description = "结束对话。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "你告别了老板,准备去接任务。" } })
            end
        }
    }

    -- 使用 ox_target 显示聊天选择菜单
    exports.ox_target:openSimpleMenu(chatOptions)
end

-- 打开任务菜单
function OpenTaskMenu()
    local menuOptions = {}

    for i, location in ipairs(Config.DeliveryLocations) do
        table.insert(menuOptions, {
            title = "送餐任务 #" .. i,
            description = "前往指定位置送餐",
            action = function()
                OpenVehicleMenu(location)  -- 在选择任务后打开车辆选择菜单
            end
        })
    end

    -- 使用 ox_target 显示任务选择菜单
    exports.ox_target:openSimpleMenu(menuOptions)
end

-- 打开车辆选择菜单
function OpenVehicleMenu(location)
    local vehicleOptions = {}

    for _, vehicle in ipairs(Config.Vehicles) do
        table.insert(vehicleOptions, {
            title = vehicle.name,
            description = "选择这辆车进行送餐",
            action = function()
                StartDelivery(location, vehicle.model)  -- 启动送餐任务并选择车辆
            end
        })
    end

    -- 使用 ox_target 显示车辆选择菜单
    exports.ox_target:openSimpleMenu(vehicleOptions)
end

-- 开始送餐任务
function StartDelivery(location, vehicleModel)
    isDelivering = true
    deliveryLocation = location
    selectedVehicle = vehicleModel
    TriggerEvent('chat:addMessage', { args = { "你已经接到送餐任务,请前往目的地:" .. tostring(deliveryLocation) } })

    -- 给予临时钥匙
    GiveTempKey()
    SpawnVehicle(vehicleModel) -- 生成所选的车辆
end

-- 给予临时钥匙使用 renzu_garage 插件
function GiveTempKey()
    local playerId = PlayerId() -- 获取玩家ID
    TriggerServerEvent('renzu_garage:addKey', playerId, keyIdentifier) -- 向服务器请求发放钥匙
end

-- 生成所选车辆
function SpawnVehicle(vehicleModel)
    local playerPed = PlayerPedId()
    local coords = GetEntityCoords(playerPed)

    RequestModel(vehicleModel)
    while not HasModelLoaded(vehicleModel) do
        Citizen.Wait(500)
    end

    local vehicle = CreateVehicle(vehicleModel, coords.x, coords.y, coords.z, GetEntityHeading(playerPed), true, false)
    TaskWarpPedIntoVehicle(playerPed, vehicle, -1)
    spawnedVehicle = vehicle -- 将生成的车辆存储在变量中
end

-- 监测角色到达送餐地点
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(1000)
        if isDelivering and deliveryLocation then
            local playerCoords = GetEntityCoords(PlayerPedId())
            if Vdist(playerCoords, deliveryLocation) < 5.0 then
                TriggerEvent('chat:addMessage', { args = { "你已到达送餐地点,任务完成!" } })
                -- 撤销钥匙
                RemoveTempKey()
                DeleteSpawnedVehicle() -- 删除生成的车辆
                isDelivering = false
                deliveryLocation = nil
                selectedVehicle = nil
            end
        end
    end
end)

-- 撤销临时钥匙使用 renzu_garage 插件
function RemoveTempKey()
    local playerId = PlayerId()
    TriggerServerEvent('renzu_garage:removeKey', playerId, keyIdentifier) -- 向服务器请求撤销钥匙
end

-- 删除生成的车辆
function DeleteSpawnedVehicle()
    if spawnedVehicle and DoesEntityExist(spawnedVehicle) then
        DeleteVehicle(spawnedVehicle) -- 删除车辆
        spawnedVehicle = nil -- 清空变量
    end
end

### 代码说明

1. **聊天流程**:在 `OpenChatMenu` 函数中,添加了多种聊天选项,其中一个选项是“请给我一些任务。”,当玩家选择这个选项时,会调用 `OpenTaskMenu()` 函数,玩家可以在任务菜单中选择送餐任务。

2. **任务菜单**:采用之前的任务菜单逻辑,允许玩家在选择任务后进一步选择车辆。

3. **互动体验**:通过随机或特定的对话,增强了与商店老板的互动体验,让游戏更具沉浸感。

### 总结

在与老板聊天后,玩家可以更顺利地接取任务,增加了游戏的互动性和趣味性。如果你希望增加更多聊天选项或其他功能,请随时告诉我!
回复

使用道具 举报

升级进度: 41%

 楼主| lizhuoda 发表于 2024-8-12 19:55:37 | 显示全部楼层
ChatGPT 发表于 2024-8-12 19:53
好的,我们可以将聊天流程与任务接取流程结合起来,使得玩家在与老板聊天后能够选择接取任务。下面是更新 ...

玩家结束任务之后会获得500到1000的奖励,可以在config中修改
回复

使用道具 举报

ChatGPT 发表于 2024-8-12 20:00:46 | 显示全部楼层
lizhuoda 发表于 2024-08-12 19:55
玩家结束任务之后会获得500到1000的奖励,可以在confi ...


好的,我们可以为玩家在完成送餐任务后增加奖励机制,通过配置文件定义奖励范围。下面是在现有代码基础上更新的内容,包括增加奖励的逻辑。

### 更新后的配置文件 `config.lua`

我们将添加一个奖励设置,以便方便修改奖励金额的范围。
Config = {}

-- 送餐坐标
Config.DeliveryLocations = {
    vector3(123.4, -200.5, 54.6), -- 示例坐标
    vector3(456.7, -300.8, 34.2)  -- 示例坐标
}

-- NPC 信息
Config.NPCs = {
    {name = "商店老板", coords = vector3(200.0, -250.0, 50.0)}, -- 商店老板 NPC 的坐标
}

-- 可选车辆
Config.Vehicles = {
    {name = "外卖车", model = "vintagecar1"}, -- 示例车辆
    {name = "摩托车", model = "ninja"},       -- 示例车辆
    {name = "货车", model = "boxville"},      -- 示例车辆
}

-- 完成任务奖励范围
Config.RewardMin = 500  -- 最低奖励
Config.RewardMax = 1000 -- 最高奖励

### 更新后的客户端脚本 `client.lua`

我们需要在任务完成后,根据配置文件中的奖励范围随机给玩家发放奖励:
local isDelivering = false
local deliveryLocation = nil
local selectedVehicle = nil
local keyIdentifier = 'delivery_temp_key' -- 钥匙标识符
local spawnedVehicle = nil -- 用于存放生成的车辆

-- 创建 NPC 交互目标
Citizen.CreateThread(function()
    for _, npc in ipairs(Config.NPCs) do
        local ped = CreatePed(0, GetHashKey("csb_mweather"), npc.coords.x, npc.coords.y, npc.coords.z, 0.0, false, true)

        -- 使用 ox_target 创建目标
        exports.ox_target:addLocalEntity(ped, {
            {
                name = "与商店老板交谈",
                icon = "fa-solid fa-comment",
                onInteract = function()
                    if not isDelivering then
                        OpenChatMenu()  -- 打开聊天菜单
                    else
                        TriggerEvent('chat:addMessage', { args = { "你已经有一个送餐任务了!" } })
                    end
                end
            }
        })
    end
end)

-- 打开聊天菜单
function OpenChatMenu()
    local chatOptions = {
        {
            title = "请给我一些任务。",
            description = "请求老板提供任务。",
            action = function()
                OpenTaskMenu() -- 请求任务
            end
        },
        {
            title = "你最近怎么样?",
            description = "询问老板的近况。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 我最近的生意不错,谢谢关心!" } })
            end
        },
        {
            title = "你的外卖服务快吗?",
            description = "询问配送速度。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 我们会尽量做到快速送达!" } })
            end
        },
        {
            title = "有其他工作吗?",
            description = "询问其他可能的工作机会。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 现在没有,但欢迎随时来咨询。" } })
            end
        },
        {
            title = "谢谢再见!",
            description = "结束对话。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "你告别了老板,准备去接任务。" } })
            end
        }
    }

    -- 使用 ox_target 显示聊天选择菜单
    exports.ox_target:openSimpleMenu(chatOptions)
end

-- 打开任务菜单
function OpenTaskMenu()
    local menuOptions = {}

    for i, location in ipairs(Config.DeliveryLocations) do
        table.insert(menuOptions, {
            title = "送餐任务 #" .. i,
            description = "前往指定位置送餐",
            action = function()
                OpenVehicleMenu(location)  -- 在选择任务后打开车辆选择菜单
            end
        })
    end

    -- 使用 ox_target 显示任务选择菜单
    exports.ox_target:openSimpleMenu(menuOptions)
end

-- 打开车辆选择菜单
function OpenVehicleMenu(location)
    local vehicleOptions = {}

    for _, vehicle in ipairs(Config.Vehicles) do
        table.insert(vehicleOptions, {
            title = vehicle.name,
            description = "选择这辆车进行送餐",
            action = function()
                StartDelivery(location, vehicle.model)  -- 启动送餐任务并选择车辆
            end
        })
    end

    -- 使用 ox_target 显示车辆选择菜单
    exports.ox_target:openSimpleMenu(vehicleOptions)
end

-- 开始送餐任务
function StartDelivery(location, vehicleModel)
    isDelivering = true
    deliveryLocation = location
    selectedVehicle = vehicleModel
    TriggerEvent('chat:addMessage', { args = { "你已经接到送餐任务,请前往目的地:" .. tostring(deliveryLocation) } })

    -- 给予临时钥匙
    GiveTempKey()
    SpawnVehicle(vehicleModel) -- 生成所选的车辆
end

-- 给予临时钥匙使用 renzu_garage 插件
function GiveTempKey()
    local playerId = PlayerId() -- 获取玩家ID
    TriggerServerEvent('renzu_garage:addKey', playerId, keyIdentifier) -- 向服务器请求发放钥匙
end

-- 生成所选车辆
function SpawnVehicle(vehicleModel)
    local playerPed = PlayerPedId()
    local coords = GetEntityCoords(playerPed)

    RequestModel(vehicleModel)
    while not HasModelLoaded(vehicleModel) do
        Citizen.Wait(500)
    end

    local vehicle = CreateVehicle(vehicleModel, coords.x, coords.y, coords.z, GetEntityHeading(playerPed), true, false)
    TaskWarpPedIntoVehicle(playerPed, vehicle, -1)
    spawnedVehicle = vehicle -- 将生成的车辆存储在变量中
end

-- 监测角色到达送餐地点
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(1000)
        if isDelivering and deliveryLocation then
            local playerCoords = GetEntityCoords(PlayerPedId())
            if Vdist(playerCoords, deliveryLocation) < 5.0 then
                -- 随机奖励
                local reward = math.random(Config.RewardMin, Config.RewardMax)
                TriggerEvent('chat:addMessage', { args = { "你已到达送餐地点,任务完成!你获得了 $" .. reward .. " 的奖励!" } })
                -- 撤销钥匙
                RemoveTempKey()
                DeleteSpawnedVehicle() -- 删除生成的车辆
                RewardPlayer(reward) -- 发放奖励
                isDelivering = false
                deliveryLocation = nil
                selectedVehicle = nil
            end
        end
    end
end)

-- 发放奖励
function RewardPlayer(amount)
    -- 这里可以添加把奖励加到玩家账户的逻辑,例如:
    TriggerServerEvent('addPlayerMoney', amount) -- 举例,触发服务器事件来增加玩家金钱
end

-- 撤销临时钥匙使用 renzu_garage 插件
function RemoveTempKey()
    local playerId = PlayerId()
    TriggerServerEvent('renzu_garage:removeKey', playerId, keyIdentifier) -- 向服务器请求撤销钥匙
end

-- 删除生成的车辆
function DeleteSpawnedVehicle()
    if spawnedVehicle and DoesEntityExist(spawnedVehicle) then
        DeleteVehicle(spawnedVehicle) -- 删除车辆
        spawnedVehicle = nil -- 清空变量
    end
end

### 代码说明

1. **奖励范围**:在 `config.lua` 中,我们添加了 `RewardMin` 和 `RewardMax` 用于定义奖励的范围。

2. **随机奖励**:在检测到任务完成后(即玩家到达目的地时),代码会随机选择一个奖励金额,并通过 `TriggerEvent` 通知玩家。

3. **奖励函数**:通过 `RewardPlayer` 函数将奖励金额传递给服务器,你可以根据自己的服务器逻辑实现给予玩家金钱的具体方法(如调用一个服务器事件)。在示例中,假设有一个名为 `addPlayerMoney` 的服务器事件,用于将奖励金额加给玩家。

### 总结

通过这种方式,玩家完成任务后将获得一定的金钱奖励,奖励范围可以在配置文件中方便地进行修改。如果你还有其他需求或希望增加更多功能,请随时告诉我!
回复

使用道具 举报

升级进度: 41%

 楼主| lizhuoda 发表于 2024-8-12 20:03:41 | 显示全部楼层
写一个fxmanifest
回复

使用道具 举报

升级进度: 41%

 楼主| lizhuoda 发表于 2024-8-12 20:25:43 | 显示全部楼层
ChatGPT 发表于 2024-8-12 20:00
好的,我们可以为玩家在完成送餐任务后增加奖励机制,通过配置文件定义奖励范围。下面是在现有代码基础上 ...

增加老板在地图上的图标
回复

使用道具 举报

ChatGPT 发表于 2024-8-12 20:26:38 | 显示全部楼层
lizhuoda 发表于 2024-08-12 20:25
增加老板在地图上的图标


要在地图上为老板 NPC 增加图标,我们可以使用 `Blip` 功能来实现。在 `client.lua` 中,我们将创建一个新的 `Blip` 以标记老板的位置。以下是更新的代码,其中包括为老板 NPC 添加图标的逻辑:

### 更新后的客户端脚本 `client.lua`

我们会在代码中添加创建 `Blip` 的部分,并确保老板的位置在游戏地图上以图标的形式出现。
local isDelivering = false
local deliveryLocation = nil
local selectedVehicle = nil
local keyIdentifier = 'delivery_temp_key' -- 钥匙标识符
local spawnedVehicle = nil -- 用于存放生成的车辆

-- 创建 NPC 交互目标和图标
Citizen.CreateThread(function()
    for _, npc in ipairs(Config.NPCs) do
        local ped = CreatePed(0, GetHashKey("csb_mweather"), npc.coords.x, npc.coords.y, npc.coords.z, 0.0, false, true)

        -- 创建老板的 Blip
        local blip = AddBlipForEntity(ped)
        SetBlipSprite(blip, 280) -- 设置图标类型(选择一个合适的 Sprites,280 是一个示例)
        SetBlipDisplay(blip, 4)
        SetBlipScale(blip, 0.8) -- 设置图标大小
        SetBlipColour(blip, 2) -- 设置图标颜色(绿色)
        BeginTextCommandSetBlipName("STRING")
        AddTextComponentString(npc.name) -- 设置图标名称为 NPC 名称
        EndTextCommandSetBlipName(blip)

        -- 使用 ox_target 创建目标
        exports.ox_target:addLocalEntity(ped, {
            {
                name = "与商店老板交谈",
                icon = "fa-solid fa-comment",
                onInteract = function()
                    if not isDelivering then
                        OpenChatMenu()  -- 打开聊天菜单
                    else
                        TriggerEvent('chat:addMessage', { args = { "你已经有一个送餐任务了!" } })
                    end
                end
            }
        })
    end
end)

-- 打开聊天菜单
function OpenChatMenu()
    local chatOptions = {
        {
            title = "请给我一些任务。",
            description = "请求老板提供任务。",
            action = function()
                OpenTaskMenu() -- 请求任务
            end
        },
        {
            title = "你最近怎么样?",
            description = "询问老板的近况。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 我最近的生意不错,谢谢关心!" } })
            end
        },
        {
            title = "你的外卖服务快吗?",
            description = "询问配送速度。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 我们会尽量做到快速送达!" } })
            end
        },
        {
            title = "有其他工作吗?",
            description = "询问其他可能的工作机会。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "老板说: 现在没有,但欢迎随时来咨询。" } })
            end
        },
        {
            title = "谢谢再见!",
            description = "结束对话。",
            action = function()
                TriggerEvent('chat:addMessage', { args = { "你告别了老板,准备去接任务。" } })
            end
        }
    }

    -- 使用 ox_target 显示聊天选择菜单
    exports.ox_target:openSimpleMenu(chatOptions)
end

-- 打开任务菜单
function OpenTaskMenu()
    local menuOptions = {}

    for i, location in ipairs(Config.DeliveryLocations) do
        table.insert(menuOptions, {
            title = "送餐任务 #" .. i,
            description = "前往指定位置送餐",
            action = function()
                OpenVehicleMenu(location)  -- 在选择任务后打开车辆选择菜单
            end
        })
    end

    -- 使用 ox_target 显示任务选择菜单
    exports.ox_target:openSimpleMenu(menuOptions)
end

-- 打开车辆选择菜单
function OpenVehicleMenu(location)
    local vehicleOptions = {}

    for _, vehicle in ipairs(Config.Vehicles) do
        table.insert(vehicleOptions, {
            title = vehicle.name,
            description = "选择这辆车进行送餐",
            action = function()
                StartDelivery(location, vehicle.model)  -- 启动送餐任务并选择车辆
            end
        })
    end

    -- 使用 ox_target 显示车辆选择菜单
    exports.ox_target:openSimpleMenu(vehicleOptions)
end

-- 开始送餐任务
function StartDelivery(location, vehicleModel)
    isDelivering = true
    deliveryLocation = location
    selectedVehicle = vehicleModel
    TriggerEvent('chat:addMessage', { args = { "你已经接到送餐任务,请前往目的地:" .. tostring(deliveryLocation) } })

    -- 给予临时钥匙
    GiveTempKey()
    SpawnVehicle(vehicleModel) -- 生成所选的车辆
end

-- 给予临时钥匙使用 renzu_garage 插件
function GiveTempKey()
    local playerId = PlayerId() -- 获取玩家ID
    TriggerServerEvent('renzu_garage:addKey', playerId, keyIdentifier) -- 向服务器请求发放钥匙
end

-- 生成所选车辆
function SpawnVehicle(vehicleModel)
    local playerPed = PlayerPedId()
    local coords = GetEntityCoords(playerPed)

    RequestModel(vehicleModel)
    while not HasModelLoaded(vehicleModel) do
        Citizen.Wait(500)
    end

    local vehicle = CreateVehicle(vehicleModel, coords.x, coords.y, coords.z, GetEntityHeading(playerPed), true, false)
    TaskWarpPedIntoVehicle(playerPed, vehicle, -1)
    spawnedVehicle = vehicle -- 将生成的车辆存储在变量中
end

-- 监测角色到达送餐地点
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(1000)
        if isDelivering and deliveryLocation then
            local playerCoords = GetEntityCoords(PlayerPedId())
            if Vdist(playerCoords, deliveryLocation) < 5.0 then
                -- 随机奖励
                local reward = math.random(Config.RewardMin, Config.RewardMax)
                TriggerEvent('chat:addMessage', { args = { "你已到达送餐地点,任务完成!你获得了 $" .. reward .. " 的奖励!" } })
                -- 撤销钥匙
                RemoveTempKey()
                DeleteSpawnedVehicle() -- 删除生成的车辆
                RewardPlayer(reward) -- 发放奖励
                isDelivering = false
                deliveryLocation = nil
                selectedVehicle = nil
            end
        end
    end
end)

-- 发放奖励
function RewardPlayer(amount)
    -- 这里可以添加把奖励加到玩家账户的逻辑,例如:
    TriggerServerEvent('addPlayerMoney', amount) -- 举例,触发服务器事件来增加玩家金钱
end

-- 撤销临时钥匙使用 renzu_garage 插件
function RemoveTempKey()
    local playerId = PlayerId()
    TriggerServerEvent('renzu_garage:removeKey', playerId, keyIdentifier) -- 向服务器请求撤销钥匙
end

-- 删除生成的车辆
function DeleteSpawnedVehicle()
    if spawnedVehicle and DoesEntityExist(spawnedVehicle) then
        DeleteVehicle(spawnedVehicle) -- 删除车辆
        spawnedVehicle = nil -- 清空变量
    end
end

### 代码说明

1. **创建 Blip**:在创建 NPC 的同时,我们为该 NPC 创建了一个 `Blip`,通过 `AddBlipForEntity` 函数将其与 NPC 关联。可以使用不同的 `SetBlipSprite` 来更改图标样式,这里使用的是 `280`,你可以根据需要选择任何其他合适的图标编号。

2. **设置图标属性**:通过 `SetBlipDisplay`、`SetBlipScale` 和 `SetBlipColour` 函数对图标的显示、大小和颜色进行了设置,最后用 `BeginTextCommandSetBlipName` 指定图标名称。

### 总结

通过这个更新,你的老板 NPC 将在地图上显示图标,方便玩家在游戏中找到他。如果你还有其他需求或想要添加更多功能,请随时告诉我!
回复

使用道具 举报

升级进度: 41%

 楼主| lizhuoda 发表于 2024-8-12 20:27:39 | 显示全部楼层
ChatGPT 发表于 2024-8-12 20:26
要在地图上为老板 NPC 增加图标,我们可以使用 `Blip` 功能来实现。在 `client.lua` 中,我们将创建一个 ...

图标可以在config中更改
回复

使用道具 举报

升级进度: 41%

 楼主| lizhuoda 发表于 2024-8-12 20:33:59 | 显示全部楼层
ChatGPT 发表于 2024-8-12 20:26
要在地图上为老板 NPC 增加图标,我们可以使用 `Blip` 功能来实现。在 `client.lua` 中,我们将创建一个 ...

创建一个老板的npc
回复

使用道具 举报

ChatGPT 发表于 2024-8-12 20:34:19 | 显示全部楼层
lizhuoda 发表于 2024-08-12 20:33
创建一个老板的npc


为了创建一个老板的 NPC,我们需要在客户端脚本中添加相应的逻辑来生成该 NPC。这里我们将使用 `CreatePed` 函数来创建 NPC,并将其放置在指定的坐标位置。以下是完整的示例代码,包括了创建一个老板 NPC 的部分。

### 更新后的配置文件 `config.lua`

在 `config.lua` 文件中,我们可以定义老板 NPC 的名称和坐标。
Config = {}

-- 送餐坐标
Config.DeliveryLocations = {
    vector3(123.4, -200.5, 54.6),
    vector3(456.7, -300.8, 34.2)
}

-- NPC 信息
Config.NPCs = {
    {name = "外卖员", coords = vector3(100.0, -150.0, 50.0)}, -- 外卖员 NPC
    {name = "商店老板", coords = vector3(200.0, -250.0, 50.0, 90.0)}, -- 老板 NPC,增加了朝向参数
}

### 创建老板 NPC 的客户端脚本 `client.lua`

在客户端脚本中,我们可以写下创建老板 NPC 的逻辑并将其添加到游戏中。
local isDelivering = false
local deliveryLocation = nil

-- 创建 NPC 交互目标和图标
Citizen.CreateThread(function()
    for _, npc in ipairs(Config.NPCs) do
        local ped = CreatePed(0, GetHashKey("csb_mweather"), npc.coords.x, npc.coords.y, npc.coords.z, npc.coords.w, false, true)

        -- 为商店老板创建 Blip
        if npc.name == "商店老板" then 
            local blip = AddBlipForEntity(ped)
            SetBlipSprite(blip, 280) -- 设置图标类型(选择一个合适的 Sprites,280 是一个示例)
            SetBlipDisplay(blip, 4)
            SetBlipScale(blip, 0.8) -- 设置图标大小
            SetBlipColour(blip, 2) -- 设置图标颜色(绿色)
            BeginTextCommandSetBlipName("STRING")
            AddTextComponentString(npc.name) -- 设置图标名称为 NPC 名称
            EndTextCommandSetBlipName(blip)
        end

        -- 使用 ox_target 创建目标
        exports.ox_target:addLocalEntity(ped, {
            {
                name = "与商店老板交谈",
                icon = "fa-solid fa-comment",
                onInteract = function()
                    if not isDelivering then
                        StartDelivery() -- 启动送餐任务
                    else
                        TriggerEvent('chat:addMessage', { args = { "你已经有一个送餐任务了!" } })
                    end
                end
            }
        })
    end
end)

-- 开始送餐任务
function StartDelivery()
    isDelivering = true
    deliveryLocation = Config.DeliveryLocations[math.random(#Config.DeliveryLocations)] -- 随机选择一个送餐地点
    TriggerEvent('chat:addMessage', { args = { "你接到了送餐任务,请前往:" .. tostring(deliveryLocation) } })
end

-- 监测角色到达送餐地点
Citizen.CreateThread(function()
    while true do
        Citizen.Wait(1000)
        if isDelivering and deliveryLocation then
            local playerCoords = GetEntityCoords(PlayerPedId())
            if Vdist(playerCoords, deliveryLocation) < 5.0 then
                TriggerEvent('chat:addMessage', { args = { "你已到达送餐地点,任务完成!" } })
                isDelivering = false
                deliveryLocation = nil
            end
        end
    end
end)

### 代码说明

1. **NPC 坐标和名字**:在配置文件中定义的 NPC 信息中,我们给商店老板和外卖员都设定了位置和名称。

2. **创建老板 NPC**:通过 `CreatePed` 函数创建商店老板的 NPC 实体,并将其放置在配置文件中指定的坐标。

3. **设置 Blip**:在创建 NPC 后,为商店老板设置一个 Blip,这样玩家就可以在地图上看到这个 NPC 的位置。

4. **与 NPC 互动**:使用 `ox_target` 插件提供的功能,使玩家可以与 NPC 互动。如果玩家没有正在进行的送餐任务,NPC 会提示他们接收任务。

5. **随机选择送餐地点**:每当接受任务时,从配置的送餐坐标中随机选择一个作为当前送餐目的地。

### 总结

这样,你就成功创建了一个老板 NPC,并允许玩家与之交互以开始送餐任务。如果你需要更多功能或调整,请随时告诉我!
回复

使用道具 举报

升级进度: 41%

 楼主| lizhuoda 发表于 2024-8-12 20:35:01 | 显示全部楼层
创建了一个老板NPC
回复

使用道具 举报

文明发言,和谐互动
文明发言,和谐互动
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|小黑屋|首页 ( 冀ICP备2021005225号-1 ) |网站地图

GMT+8, 2024-9-20 21:30 , Processed in 0.102350 second(s), 8 queries , Redis On.

Powered by Discuz! X3.4

© 2001-2013 ELITE论坛【技术无价-分享快乐】

快速回复 返回顶部 返回列表