本章节将指导你创建一个名为“KillToHeal”的吸血mod,该模块能让玩家在战斗中每击杀一个敌人时回复一定量的生命值。
创建模块
- 创建模块目录:
- 在游戏的Modules目录下新建一个名为KillToHeal的文件夹。
- SubModule.xml配置:
- 在KillToHeal目录中创建一个名为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#类库项目:
- 新建一个名为KillToHeal的C#类库项目,设置参考第三章教程。
实现击杀回血逻辑
- KillMissionLogic.cs:
- 创建一个名为KillMissionLogic.cs的C#文件,用于定义击杀回血的逻辑。
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:
- 创建SubModule.cs文件,用于将KillMissionLogic逻辑应用到mod。
using TaleWorlds.MountAndBlade;
namespace KillToHeal
{
public class SubModule : MBSubModuleBase
{
// 在任务行为初始化时调用
public override void OnMissionBehaviorInitialize(Mission mission)
{
base.OnMissionBehaviorInitialize(mission);
mission.AddMissionBehavior(new KillMissionLogic()); // 添加击杀回血逻辑
}
}
}测试模块
- 编译项目生成KillToHeal.dll,并将其放置在Modules/KillToHeal/bin/Win64_Shipping_Client目录下。
- 启动游戏,并在模块管理器中激活KillToHeal模块。
- 进入战斗,每次击杀敌人时,玩家应该回复5点生命值。