137 lines
3.8 KiB
C#
137 lines
3.8 KiB
C#
using FunGame.Core.Api;
|
|
using FunGame.Core.Entity;
|
|
using FunGame.Server.Contracts.Plugins;
|
|
|
|
namespace FunGame.Server.Modules.Example;
|
|
|
|
/// <summary>
|
|
/// 示例角色模组:注册工厂(工厂注册保持模块化)+ 提供实体集合<para/>
|
|
/// 通过工厂注册后,实体 JSON 配置(EntityModuleConfig)通道也能实例化本模组的角色
|
|
/// </summary>
|
|
public class ExampleCharacterModule : ICharacterModule
|
|
{
|
|
public string Name => "example.characters";
|
|
public string Version => "1.0.0";
|
|
public string Author => "FunGame.Server";
|
|
public string Description => "示例角色模组:战士 / 法师";
|
|
|
|
private IReadOnlyDictionary<string, Character>? _entities;
|
|
|
|
public IReadOnlyDictionary<string, Character> Entities
|
|
{
|
|
get
|
|
{
|
|
_entities ??= new Dictionary<string, Character>
|
|
{
|
|
["example_warrior"] = new ExampleWarrior(),
|
|
["example_mage"] = new ExampleMage()
|
|
};
|
|
return _entities;
|
|
}
|
|
}
|
|
|
|
public void OnLoad(IServiceProvider services)
|
|
{
|
|
// 工厂注册:id -> 构造函数,供 Factory.GetInstance / GetGameModuleInstances 使用
|
|
Factory.OpenFactory.RegisterFactory<Character>((id, name, args) => id switch
|
|
{
|
|
90001 => new ExampleWarrior(),
|
|
90002 => new ExampleMage(),
|
|
_ => null
|
|
});
|
|
}
|
|
|
|
public void OnUnload()
|
|
{
|
|
// 从工厂注销(保持工厂可被热重载模组替换)
|
|
Factory.OpenFactory.UnRegisterFactory<Character>((id, name, args) => id switch
|
|
{
|
|
90001 => new ExampleWarrior(),
|
|
90002 => new ExampleMage(),
|
|
_ => null
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>示例技能模组</summary>
|
|
public class ExampleSkillModule : ISkillModule
|
|
{
|
|
public string Name => "example.skills";
|
|
public string Version => "1.0.0";
|
|
public string Author => "FunGame.Server";
|
|
public string Description => "示例技能模组:示例打击";
|
|
|
|
private IReadOnlyDictionary<string, Skill>? _entities;
|
|
|
|
public IReadOnlyDictionary<string, Skill> Entities
|
|
{
|
|
get
|
|
{
|
|
_entities ??= new Dictionary<string, Skill>
|
|
{
|
|
["example_strike"] = new ExampleStrike()
|
|
};
|
|
return _entities;
|
|
}
|
|
}
|
|
|
|
public void OnLoad(IServiceProvider services)
|
|
{
|
|
Factory.OpenFactory.RegisterFactory<Skill>((id, name, args) => id switch
|
|
{
|
|
91001 => new ExampleStrike(),
|
|
_ => null
|
|
});
|
|
}
|
|
|
|
public void OnUnload()
|
|
{
|
|
Factory.OpenFactory.UnRegisterFactory<Skill>((id, name, args) => id switch
|
|
{
|
|
91001 => new ExampleStrike(),
|
|
_ => null
|
|
});
|
|
}
|
|
}
|
|
|
|
/// <summary>示例物品模组</summary>
|
|
public class ExampleItemModule : IItemModule
|
|
{
|
|
public string Name => "example.items";
|
|
public string Version => "1.0.0";
|
|
public string Author => "FunGame.Server";
|
|
public string Description => "示例物品模组:回复药剂";
|
|
|
|
private IReadOnlyDictionary<string, Item>? _entities;
|
|
|
|
public IReadOnlyDictionary<string, Item> Entities
|
|
{
|
|
get
|
|
{
|
|
_entities ??= new Dictionary<string, Item>
|
|
{
|
|
["example_potion"] = new ExamplePotion()
|
|
};
|
|
return _entities;
|
|
}
|
|
}
|
|
|
|
public void OnLoad(IServiceProvider services)
|
|
{
|
|
Factory.OpenFactory.RegisterFactory<Item>((id, name, args) => id switch
|
|
{
|
|
92001 => new ExamplePotion(),
|
|
_ => null
|
|
});
|
|
}
|
|
|
|
public void OnUnload()
|
|
{
|
|
Factory.OpenFactory.UnRegisterFactory<Item>((id, name, args) => id switch
|
|
{
|
|
92001 => new ExamplePotion(),
|
|
_ => null
|
|
});
|
|
}
|
|
}
|