《饥荒联机版》模组:所有角色夜晚讲故事功能

ChatGPT4o 使用 ChatGPT 4o 回答

问题:

我想编写一个游戏模组,游戏名是《饥荒联机版》,该游戏英文名是《Don't Starve Together》,该游戏模组由lua语言编写,游戏内有一名叫做沃尔特的角色,英文名是Walter,玩家可以使用该角色在夜晚,用鼠标右键在火坑使用讲故事的能力。请你参考该角色讲故事的功能,帮我编写一个模组,让所有角色,包括模组角色,获得跟沃尔特相同的讲故事能力。故事的内容可以在模组内自定义修改,并且每天晚上都会随机讲一个不同的故事,直到故事列表所有不同的故事都讲完,才能讲之前讲过的故事。请帮我写一个模组实现以上功能

回答:

好的,我可以帮助你编写一个《Don't Starve Together》的模组,使所有角色都能在夜晚使用讲故事的能力。以下是一个基本的模组代码示例,你可以根据需要进行自定义和扩展。

首先,创建一个新的模组文件夹,并在其中创建以下文件和文件夹结构:

modname/
├── modinfo.lua
├── modmain.lua
└── scripts/
    └── components/
        └── storyteller.lua

1. modinfo.lua

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

name = "Universal Storyteller"
description = "All characters can tell stories at night."
author = "Your Name"
version = "1.0"

forumthread = ""

api_version = 10

dst_compatible = true
all_clients_require_mod = true
client_only_mod = false
server_filter_tags = {}

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

2. modmain.lua

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

PrefabFiles = {
    -- Add any custom prefabs here if needed
}

local storyteller = require "components/storyteller"

local function AddStorytellerComponent(inst)
    if not inst.components.storyteller then
        inst:AddComponent("storyteller")
    end
end

local function OnPlayerSpawn(inst)
    AddStorytellerComponent(inst)
end

AddPlayerPostInit(OnPlayerSpawn)

3. scripts/components/storyteller.lua

这个文件定义了讲故事的组件。

local Storyteller = Class(function(self, inst)
    self.inst = inst
    self.stories = {
        "Once upon a time...",
        "In a land far, far away...",
        "There was a brave adventurer...",
        "Long ago, in a forgotten realm...",
        "A tale of mystery and wonder..."
    }
    self.told_stories = {}
    self:ResetStories()
end)

function Storyteller:ResetStories()
    self.told_stories = {}
    for i, story in ipairs(self.stories) do
        table.insert(self.told_stories, story)
    end
    self:ShuffleStories()
end

function Storyteller:ShuffleStories()
    for i = #self.told_stories, 2, -1 do
        local j = math.random(i)
        self.told_stories[i], self.told_stories[j] = self.told_stories[j], self.told_stories[i]
    end
end

function Storyteller:TellStory()
    if #self.told_stories == 0 then
        self:ResetStories()
    end
    local story = table.remove(self.told_stories)
    self.inst.components.talker:Say(story)
end

local function OnRightClick(inst, doer, target, actions, right)
    if right and target and target:HasTag("campfire") and TheWorld.state.isnight then
        table.insert(actions, ACTIONS.TELLSTORY)
    end
end

local function TellStoryAction(act)
    if act.doer and act.doer.components.storyteller then
        act.doer.components.storyteller:TellStory()
        return true
    end
end

AddComponentAction("SCENE", "storyteller", OnRightClick)

ACTIONS.TELLSTORY = GLOBAL.Action({ priority = 10, rmb = true })
ACTIONS.TELLSTORY.id = "TELLSTORY"
ACTIONS.TELLSTORY.str = "Tell Story"
ACTIONS.TELLSTORY.fn = TellStoryAction

AddStategraphActionHandler("wilson", GLOBAL.ActionHandler(ACTIONS.TELLSTORY, "doshortaction"))
AddStategraphActionHandler("wilson_client", GLOBAL.ActionHandler(ACTIONS.TELLSTORY, "doshortaction"))

return Storyteller

自定义故事内容

你可以在 storyteller.lua 文件中的 self.stories 表中添加或修改故事内容。

说明

  1. modinfo.lua 文件定义了模组的基本信息。
  2. modmain.lua 文件初始化了玩家角色,并为每个角色添加了 storyteller 组件。
  3. storyteller.lua 文件定义了讲故事的逻辑,包括故事的存储、随机选择和讲述。

将这些文件放入你的模组文件夹中,并确保文件结构正确。启动游戏并启用模组,所有角色在夜晚都可以在火坑旁使用右键讲故事。

希望这个示例能帮助你实现你的模组