返回目录
Chapter 12 第十二章:简单的吸血Mod
开发者文档 更新于 2024-03-21 19:31:53

第十二章:简单的吸血Mod

本章节将指导你创建一个名为“KillToHeal”的吸血mod,该模块能让玩家在战斗中每击杀一个敌人时回复一定量的生命值。

创建模块

  1. 创建模块目录
  1. SubModule.xml配置
<?xml version="1.0" encoding="utf-8"?>
<Module>
  <Name value="Kill to heal"/>
  <Id value="KillToHeal"/>
  <Version value="v1.0.0"/>
  <SingleplayerModule value="true"/>
  <MultiplayerModule value="false"/>
  <DependedModules>
    <DependedModule Id="Native"/>
    <DependedModule Id="SandBoxCore"/>
    <DependedModule Id="Sandbox"/>
    <DependedModule Id="CustomBattle"/>
    <DependedModule Id="StoryMode"/>
  </DependedModules>
  <SubModules>
    <SubModule>
      <Name value="KillToHeal"/>
      <DLLName value="KillToHeal.dll"/>
      <SubModuleClassType value="KillToHeal.SubModule"/>
    </SubModule>
  </SubModules>
</Module>

创建C#类库项目:

实现击杀回血逻辑

  1. KillMissionLogic.cs
using TaleWorlds.CampaignSystem;
using TaleWorlds.Core;
using TaleWorlds.MountAndBlade;

namespace KillToHeal
{
    public class KillMissionLogic : MissionLogic
    {
        private const int HealValue = 5; // 每次击杀回血的值

        // 当有角色从战场上移除时触发
        public override void OnAgentRemoved(Agent affectedAgent, Agent affectorAgent, AgentState agentState, KillingBlow blow)
        {
            // 验证击杀是否有效,并且被击杀的是人类角色,且执行者是玩家
            if (IsValidKill(affectedAgent, affectorAgent) && affectedAgent.IsHuman && IsPlayer(affectorAgent))
            {
                affectorAgent.Health += HealValue; // 玩家回血
            }
        }

        // 验证击杀是否有效(被击杀者和击杀者不是同一个人,且都不为空)
        private bool IsValidKill(Agent affectedAgent, Agent affectorAgent)
        {
            return affectedAgent != null && affectorAgent != null && affectedAgent != affectorAgent;
        }

        // 验证是否为玩家
        private bool IsPlayer(Agent agent)
        {
            return agent != null && agent.IsHero && (agent.Character as CharacterObject).HeroObject == Hero.MainHero;
        }
    }
}

SubModule.cs

using TaleWorlds.MountAndBlade;

namespace KillToHeal
{
    public class SubModule : MBSubModuleBase
    {
        // 在任务行为初始化时调用
        public override void OnMissionBehaviorInitialize(Mission mission)
        {
            base.OnMissionBehaviorInitialize(mission);
            mission.AddMissionBehavior(new KillMissionLogic()); // 添加击杀回血逻辑
        }
    }
}

测试模块