《饥荒联机版》玩家骑行模组编写指南

ChatGPT4o 使用 ChatGPT 4o 回答

问题:

我想用lua语言编写一个游戏模组,该游戏是《饥荒联机版》英文名《Don't Starve Together》,该模组的功能是允许一名玩家骑在另一名玩家的肩膀上,玩家处于骑行状态,可以参与战斗,给被骑的玩家喂食,被骑行的玩家移动速度会减慢10%,被骑行的玩家可以选择将背上的玩家放下,结束骑行状态,骑行的玩家也可以主动选择结束骑行状态,你能帮我编写该模组吗

回答:

当然可以!编写《饥荒联机版》(Don't Starve Together)的模组需要使用Lua语言,并且需要了解游戏的API和事件系统。以下是一个基本的模组代码示例,展示了如何实现一个玩家骑在另一个玩家肩膀上的功能。

首先,创建一个新的模组文件夹,并在其中创建 modinfo.luamodmain.lua 文件。

modinfo.lua

这个文件包含模组的基本信息。

name = "Player Riding Mod"
description = "Allows one player to ride on another player's shoulders."
author = "Your Name"
version = "1.0"

forumthread = ""

api_version = 10

dst_compatible = true
all_clients_require_mod = true
client_only_mod = false

icon_atlas = "modicon.xml"
icon = "modicon.tex"

modmain.lua

这个文件包含模组的主要逻辑。

local function StartRiding(inst, rider)
    if inst.components.rider == nil then
        inst:AddComponent("rider")
    end
    inst.components.rider:SetRider(rider)
    inst.components.locomotor:SetExternalSpeedMultiplier(inst, "rider_speed", 0.9)
end

local function StopRiding(inst)
    if inst.components.rider ~= nil then
        inst.components.rider:SetRider(nil)
    end
    inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "rider_speed")
end

local function OnRiderDeath(inst)
    if inst.components.rider ~= nil and inst.components.rider.rider ~= nil then
        StopRiding(inst)
    end
end

local function OnRiderRemoved(inst)
    if inst.components.rider ~= nil and inst.components.rider.rider ~= nil then
        StopRiding(inst)
    end
end

local function OnRiderAttacked(inst, data)
    if inst.components.rider ~= nil and inst.components.rider.rider ~= nil then
        local rider = inst.components.rider.rider
        rider.components.combat:GetAttacked(data.attacker, data.damage)
    end
end

local function OnRiderFeed(inst, data)
    if inst.components.rider ~= nil and inst.components.rider.rider ~= nil then
        local rider = inst.components.rider.rider
        rider.components.eater:Eat(data.food)
    end
end

local function AddRiderComponent(inst)
    inst:AddComponent("rider")
    inst:ListenForEvent("death", OnRiderDeath)
    inst:ListenForEvent("onremove", OnRiderRemoved)
    inst:ListenForEvent("attacked", OnRiderAttacked)
    inst:ListenForEvent("oneat", OnRiderFeed)
end

AddPrefabPostInit("player", AddRiderComponent)

local function OnPlayerAction(inst, doer, actions, right)
    if doer:HasTag("player") and inst:HasTag("player") then
        table.insert(actions, ACTIONS.RIDE)
    end
end

AddComponentAction("SCENE", "player", OnPlayerAction)

local function RideAction(inst, doer, target, actions, right)
    if right then
        table.insert(actions, ACTIONS.RIDE)
    end
end

AddComponentAction("USEITEM", "player", RideAction)

local function RideFn(act)
    if act.doer ~= nil and act.target ~= nil and act.target:HasTag("player") then
        StartRiding(act.target, act.doer)
        return true
    end
end

local function StopRideFn(act)
    if act.doer ~= nil and act.doer.components.rider ~= nil then
        StopRiding(act.doer)
        return true
    end
end

ACTIONS.RIDE = Action({ priority = 10, rmb = true, distance = 1.5 })
ACTIONS.RIDE.fn = RideFn

ACTIONS.STOPRIDE = Action({ priority = 10, rmb = true, distance = 1.5 })
ACTIONS.STOPRIDE.fn = StopRideFn

AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.RIDE, "doshortaction"))
AddStategraphActionHandler("wilson", ActionHandler(ACTIONS.STOPRIDE, "doshortaction"))

说明

  1. modinfo.lua: 这个文件定义了模组的基本信息,如名称、描述、作者、版本等。
  2. modmain.lua: 这个文件包含了模组的主要逻辑。
    • StartRidingStopRiding 函数用于开始和结束骑行状态。
    • OnRiderDeath, OnRiderRemoved, OnRiderAttacked, OnRiderFeed 函数用于处理骑行状态下的各种事件。
    • AddRiderComponent 函数为玩家添加骑行组件。
    • OnPlayerActionRideAction 函数用于定义玩家之间的骑行动作。
    • RideFnStopRideFn 函数用于执行骑行和停止骑行的动作。

请注意,这只是一个基本的示例,可能需要根据实际需求进行调整和优化。希望这能帮助你开始编写你的模组!