From 7b4a655ff49d3f369f21e765d821938d7ad68e04 Mon Sep 17 00:00:00 2001 From: milimoe <110188673+milimoe@users.noreply.github.com> Date: Thu, 30 Nov 2023 00:27:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0CharacterMode=EF=BC=8CSkillMo?= =?UTF-8?q?de=EF=BC=8CItemMode=E7=B1=BB=E5=92=8C=E5=85=B6=E5=AE=83?= =?UTF-8?q?=E6=9D=82=E9=A1=B9=20(#64)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 添加CharacterMode,SkillMode,ItemMode * 添加默认地图属性 * 添加CharacterMode, SkillMode, ItemMode的Loader方法,优化构造函数 * 添加和优化Equals方法 * 删除Symbol --- Api/Factory/CharacterFactory.cs | 5 +- Api/Factory/ItemFactory.cs | 4 +- Api/Factory/SkillFactory.cs | 4 +- Api/Utility/GameModeLoader.cs | 8 +- Docs/api.xml | 158 +++++++++++++++++++++++++- Entity/Character/Character.cs | 21 +++- Entity/Item/ActiveItem.cs | 18 ++- Entity/Item/PassiveItem.cs | 18 ++- Entity/Skill/ActiveSkill.cs | 16 ++- Entity/Skill/PassiveSkill.cs | 16 ++- Entity/Skill/Skill.cs | 4 +- Entity/System/Room.cs | 7 +- Entity/User/User.cs | 4 +- Interface/Base/IGameMode.cs | 2 +- Library/Common/Addon/CharacterMode.cs | 75 ++++++++++++ Library/Common/Addon/GameMode.cs | 18 ++- Library/Common/Addon/ItemMode.cs | 75 ++++++++++++ Library/Common/Addon/SkillMode.cs | 75 ++++++++++++ Service/AddonManager.cs | 33 +++++- 19 files changed, 514 insertions(+), 47 deletions(-) create mode 100644 Library/Common/Addon/CharacterMode.cs create mode 100644 Library/Common/Addon/ItemMode.cs create mode 100644 Library/Common/Addon/SkillMode.cs diff --git a/Api/Factory/CharacterFactory.cs b/Api/Factory/CharacterFactory.cs index eb3907e..a4682d9 100644 --- a/Api/Factory/CharacterFactory.cs +++ b/Api/Factory/CharacterFactory.cs @@ -7,9 +7,6 @@ namespace Milimoe.FunGame.Core.Api.Factory { public Type EntityType => typeof(Character); - public Character Create() - { - return new Character(); - } + public Character Create() => Character.GetInstance(); } } diff --git a/Api/Factory/ItemFactory.cs b/Api/Factory/ItemFactory.cs index aad04ca..891cfd9 100644 --- a/Api/Factory/ItemFactory.cs +++ b/Api/Factory/ItemFactory.cs @@ -16,11 +16,11 @@ namespace Milimoe.FunGame.Core.Api.Factory { case ItemType.Passive: _EntityType = typeof(PassiveItem); - return new PassiveItem(); + return PassiveItem.GetInstance(); case ItemType.Active: default: _EntityType = typeof(ActiveItem); - return new ActiveItem(); + return PassiveItem.GetInstance(); } } diff --git a/Api/Factory/SkillFactory.cs b/Api/Factory/SkillFactory.cs index d530d06..3bc7209 100644 --- a/Api/Factory/SkillFactory.cs +++ b/Api/Factory/SkillFactory.cs @@ -16,11 +16,11 @@ namespace Milimoe.FunGame.Core.Api.Factory { case SkillType.Passive: _EntityType = typeof(PassiveSkill); - return new PassiveSkill(); + return PassiveSkill.GetInstance(); case SkillType.Active: default: _EntityType = typeof(ActiveSkill); - return new ActiveSkill(); + return ActiveSkill.GetInstance(); } } diff --git a/Api/Utility/GameModeLoader.cs b/Api/Utility/GameModeLoader.cs index 663fc87..6b4ca71 100644 --- a/Api/Utility/GameModeLoader.cs +++ b/Api/Utility/GameModeLoader.cs @@ -1,4 +1,5 @@ -using Milimoe.FunGame.Core.Library.Common.Addon; +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Library.Common.Addon; using Milimoe.FunGame.Core.Library.Common.Event; using Milimoe.FunGame.Core.Service; @@ -8,6 +9,9 @@ namespace Milimoe.FunGame.Core.Api.Utility { public Dictionary Modes { get; } = []; public Dictionary Maps { get; } = []; + public List Characters { get; } = []; + public List Skills { get; } = []; + public List Items { get; } = []; private GameModeLoader() { @@ -17,7 +21,7 @@ namespace Milimoe.FunGame.Core.Api.Utility public static GameModeLoader LoadGameModes(params object[] objs) { GameModeLoader loader = new(); - AddonManager.LoadGameModes(loader.Modes, objs); + AddonManager.LoadGameModes(loader.Modes, loader.Characters, loader.Skills, loader.Items, objs); AddonManager.LoadGameMaps(loader.Maps, objs); return loader; } diff --git a/Docs/api.xml b/Docs/api.xml index 012d8f8..6c03f7e 100644 --- a/Docs/api.xml +++ b/Docs/api.xml @@ -1157,6 +1157,52 @@ 服务端和客户端都应该实现这个接口,用于初始化支持的Mod列表 + + + 模组名称 + + + + + 模组描述 + + + + + 模组版本 + + + + + 模组作者 + + + + + 此模组中包含的角色 + + + + + 加载标记 + + + + + 加载模组 + + + + + 模组加载后需要做的事 + + + + + 允许返回false来阻止加载此模组 + + + 必须继承基类: @@ -1241,11 +1287,24 @@ 模组作者 - + + + 默认地图 + + + 模组所使用的地图 + + + 如模组有界面,请重写此方法 + 此方法会在StartGame时调用 + + + + 加载标记 @@ -1302,6 +1361,52 @@ 绑定事件。在后触发 + + + 模组名称 + + + + + 模组描述 + + + + + 模组版本 + + + + + 模组作者 + + + + + 此模组中包含的物品 + + + + + 加载标记 + + + + + 加载模组 + + + + + 模组加载后需要做的事 + + + + + 允许返回false来阻止加载此模组 + + + 插件名称 @@ -1378,6 +1483,52 @@ 绑定事件。在后触发 + + + 模组名称 + + + + + 模组描述 + + + + + 模组版本 + + + + + 模组作者 + + + + + 此模组中包含的技能 + + + + + 加载标记 + + + + + 加载模组 + + + + + 模组加载后需要做的事 + + + + + 允许返回false来阻止加载此模组 + + + 继承这个类可以获得异步等待的功能 @@ -1870,11 +2021,14 @@ - + 从gamemodes目录加载所有模组 + + + diff --git a/Entity/Character/Character.cs b/Entity/Character/Character.cs index ee96119..81d7a51 100644 --- a/Entity/Character/Character.cs +++ b/Entity/Character/Character.cs @@ -1,12 +1,11 @@ using System.Collections; +using Milimoe.FunGame.Core.Interface.Entity; using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Entity { - public class Character + public class Character : BaseEntity { - public int Id { get; set; } - public string Name { get; set; } = ""; public string FirstName { get; set; } = ""; public string NickName { get; set; } = ""; public User? User { get; set; } = null; @@ -50,12 +49,22 @@ namespace Milimoe.FunGame.Core.Entity public decimal CritRate { get; set; } = 0.05M; // 暴击率 public decimal CritDMG { get; set; } = 1.25M; // 暴击伤害 public decimal EvadeRate { get; set; } = 0.05M; // 闪避率 - public Hashtable Skills { get; set; } = new(); - public Hashtable Items { get; set; } = new(); + public Hashtable Skills { get; set; } = []; + public Hashtable Items { get; set; } = []; - internal Character() + protected Character() { } + + internal static Character GetInstance() + { + return new(); + } + + public override bool Equals(IBaseEntity? other) + { + return other is Character c && c.Name == Name; + } } } diff --git a/Entity/Item/ActiveItem.cs b/Entity/Item/ActiveItem.cs index c172ec3..6349ada 100644 --- a/Entity/Item/ActiveItem.cs +++ b/Entity/Item/ActiveItem.cs @@ -6,23 +6,31 @@ namespace Milimoe.FunGame.Core.Entity { public ActiveSkill? Skill { get; set; } = null; - internal ActiveItem() + protected ActiveItem() { Active = true; } - internal ActiveItem(int id, string name) + protected ActiveItem(int id, string name) { Active = true; Id = id; Name = name; } + internal static ActiveItem GetInstance() + { + return new(); + } + + internal static ActiveItem GetInstance(int id, string name) + { + return new(id, name); + } + public override bool Equals(IBaseEntity? other) { - if (other != null && other.Guid == this.Guid) - return true; - else return false; + return other is ActiveItem i && i.Name == Name; } } } diff --git a/Entity/Item/PassiveItem.cs b/Entity/Item/PassiveItem.cs index 0aa99b5..5af2c25 100644 --- a/Entity/Item/PassiveItem.cs +++ b/Entity/Item/PassiveItem.cs @@ -6,23 +6,31 @@ namespace Milimoe.FunGame.Core.Entity { public PassiveSkill? Skill { get; set; } = null; - internal PassiveItem() + protected PassiveItem() { Active = false; } - internal PassiveItem(int id, string name) + protected PassiveItem(int id, string name) { Active = false; Id = id; Name = name; } + internal static PassiveItem GetInstance() + { + return new(); + } + + internal static PassiveItem GetInstance(int id, string name) + { + return new(id, name); + } + public override bool Equals(IBaseEntity? other) { - if (other != null && other.Guid == this.Guid) - return true; - else return false; + return other is PassiveItem i && i.Name == Name; } } } diff --git a/Entity/Skill/ActiveSkill.cs b/Entity/Skill/ActiveSkill.cs index ddc1ca6..9981dbb 100644 --- a/Entity/Skill/ActiveSkill.cs +++ b/Entity/Skill/ActiveSkill.cs @@ -1,4 +1,6 @@ -namespace Milimoe.FunGame.Core.Entity +using Milimoe.FunGame.Core.Interface.Entity; + +namespace Milimoe.FunGame.Core.Entity { public class ActiveSkill : Skill { @@ -15,9 +17,19 @@ public decimal Reference9 { get; set; } = 0; public decimal Reference10 { get; set; } = 0; - internal ActiveSkill() + protected ActiveSkill() { Active = true; } + + internal static ActiveSkill GetInstance() + { + return new(); + } + + public override bool Equals(IBaseEntity? other) + { + return other is ActiveSkill s && s.Name == Name; + } } } diff --git a/Entity/Skill/PassiveSkill.cs b/Entity/Skill/PassiveSkill.cs index cfabe0f..2dec9f0 100644 --- a/Entity/Skill/PassiveSkill.cs +++ b/Entity/Skill/PassiveSkill.cs @@ -1,4 +1,6 @@ -namespace Milimoe.FunGame.Core.Entity +using Milimoe.FunGame.Core.Interface.Entity; + +namespace Milimoe.FunGame.Core.Entity { public class PassiveSkill : Skill { @@ -13,9 +15,19 @@ public decimal Reference9 { get; set; } = 0; public decimal Reference10 { get; set; } = 0; - internal PassiveSkill() + protected PassiveSkill() { Active = false; } + + internal static PassiveSkill GetInstance() + { + return new(); + } + + public override bool Equals(IBaseEntity? other) + { + return other is PassiveSkill s && s.Name == Name; + } } } diff --git a/Entity/Skill/Skill.cs b/Entity/Skill/Skill.cs index 43e40be..db23e80 100644 --- a/Entity/Skill/Skill.cs +++ b/Entity/Skill/Skill.cs @@ -2,10 +2,8 @@ namespace Milimoe.FunGame.Core.Entity { - public abstract class Skill + public abstract class Skill : BaseEntity { - public int Id { get; set; } - public string Name { get; set; } = ""; public string Describe { get; set; } = ""; public char Key { get; set; } public bool Active { get; set; } diff --git a/Entity/System/Room.cs b/Entity/System/Room.cs index 4917bfe..bf7472d 100644 --- a/Entity/System/Room.cs +++ b/Entity/System/Room.cs @@ -36,14 +36,9 @@ namespace Milimoe.FunGame.Core.Entity Statistics = new(this); } - public bool Equals(Room other) - { - return Equals(other); - } - public override bool Equals(IBaseEntity? other) { - return other?.Id == Id; + return other is Room r && r.Roomid == Roomid; } } } diff --git a/Entity/User/User.cs b/Entity/User/User.cs index 6f3eb25..3e78533 100644 --- a/Entity/User/User.cs +++ b/Entity/User/User.cs @@ -48,9 +48,7 @@ namespace Milimoe.FunGame.Core.Entity public override bool Equals(IBaseEntity? other) { - if (other == null) return false; - if (((User)other).Id == Id) return true; - return false; + return other is User u && u.Id == Id; } public override string ToString() diff --git a/Interface/Base/IGameMode.cs b/Interface/Base/IGameMode.cs index 69309d3..07ebd2b 100644 --- a/Interface/Base/IGameMode.cs +++ b/Interface/Base/IGameMode.cs @@ -4,6 +4,6 @@ IGamingRandomEventHandler, IGamingMoveEventHandler, IGamingAttackEventHandler, IGamingSkillEventHandler, IGamingItemEventHandler, IGamingMagicEventHandler, IGamingBuyEventHandler, IGamingSuperSkillEventHandler, IGamingPauseEventHandler, IGamingUnpauseEventHandler, IGamingSurrenderEventHandler, IGamingUpdateInfoEventHandler { - + public abstract bool StartUI(params object[] args); } } diff --git a/Library/Common/Addon/CharacterMode.cs b/Library/Common/Addon/CharacterMode.cs new file mode 100644 index 0000000..35a5170 --- /dev/null +++ b/Library/Common/Addon/CharacterMode.cs @@ -0,0 +1,75 @@ +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Interface; + +namespace Milimoe.FunGame.Core.Library.Common.Addon +{ + public abstract class CharacterMode : IAddon + { + /// + /// 模组名称 + /// + public abstract string Name { get; } + + /// + /// 模组描述 + /// + public abstract string Description { get; } + + /// + /// 模组版本 + /// + public abstract string Version { get; } + + /// + /// 模组作者 + /// + public abstract string Author { get; } + + /// + /// 此模组中包含的角色 + /// + public abstract List Characters { get; } + + /// + /// 加载标记 + /// + private bool IsLoaded = false; + + /// + /// 加载模组 + /// + public bool Load(params object[] objs) + { + if (IsLoaded) + { + return false; + } + // BeforeLoad可以阻止加载此模组 + if (BeforeLoad()) + { + // 模组加载后,不允许再次加载此模组 + IsLoaded = true; + // 如果加载后需要执行代码,请重写AfterLoad方法 + AfterLoad(); + } + return IsLoaded; + } + + /// + /// 模组加载后需要做的事 + /// + protected virtual void AfterLoad() + { + // override + } + + /// + /// 允许返回false来阻止加载此模组 + /// + /// + protected virtual bool BeforeLoad() + { + return true; + } + } +} diff --git a/Library/Common/Addon/GameMode.cs b/Library/Common/Addon/GameMode.cs index 0ef5269..6a6ef47 100644 --- a/Library/Common/Addon/GameMode.cs +++ b/Library/Common/Addon/GameMode.cs @@ -28,10 +28,26 @@ namespace Milimoe.FunGame.Core.Library.Common.Addon /// public abstract string Author { get; } + /// + /// 默认地图 + /// + public abstract string DefaultMap { get; } + /// /// 模组所使用的地图 /// - public abstract string Map { get; } + public abstract string[] Maps { get; } + + /// + /// 如模组有界面,请重写此方法 + /// 此方法会在StartGame时调用 + /// + /// + /// + public virtual bool StartUI(params object[] args) + { + return true; + } /// /// 加载标记 diff --git a/Library/Common/Addon/ItemMode.cs b/Library/Common/Addon/ItemMode.cs new file mode 100644 index 0000000..df89b01 --- /dev/null +++ b/Library/Common/Addon/ItemMode.cs @@ -0,0 +1,75 @@ +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Interface; + +namespace Milimoe.FunGame.Core.Library.Common.Addon +{ + public abstract class ItemMode : IAddon + { + /// + /// 模组名称 + /// + public abstract string Name { get; } + + /// + /// 模组描述 + /// + public abstract string Description { get; } + + /// + /// 模组版本 + /// + public abstract string Version { get; } + + /// + /// 模组作者 + /// + public abstract string Author { get; } + + /// + /// 此模组中包含的物品 + /// + public abstract List Items { get; } + + /// + /// 加载标记 + /// + private bool IsLoaded = false; + + /// + /// 加载模组 + /// + public bool Load(params object[] objs) + { + if (IsLoaded) + { + return false; + } + // BeforeLoad可以阻止加载此模组 + if (BeforeLoad()) + { + // 模组加载后,不允许再次加载此模组 + IsLoaded = true; + // 如果加载后需要执行代码,请重写AfterLoad方法 + AfterLoad(); + } + return IsLoaded; + } + + /// + /// 模组加载后需要做的事 + /// + protected virtual void AfterLoad() + { + // override + } + + /// + /// 允许返回false来阻止加载此模组 + /// + /// + protected virtual bool BeforeLoad() + { + return true; + } + } +} diff --git a/Library/Common/Addon/SkillMode.cs b/Library/Common/Addon/SkillMode.cs new file mode 100644 index 0000000..18409cf --- /dev/null +++ b/Library/Common/Addon/SkillMode.cs @@ -0,0 +1,75 @@ +using Milimoe.FunGame.Core.Entity; +using Milimoe.FunGame.Core.Interface; + +namespace Milimoe.FunGame.Core.Library.Common.Addon +{ + public abstract class SkillMode : IAddon + { + /// + /// 模组名称 + /// + public abstract string Name { get; } + + /// + /// 模组描述 + /// + public abstract string Description { get; } + + /// + /// 模组版本 + /// + public abstract string Version { get; } + + /// + /// 模组作者 + /// + public abstract string Author { get; } + + /// + /// 此模组中包含的技能 + /// + public abstract List Skills { get; } + + /// + /// 加载标记 + /// + private bool IsLoaded = false; + + /// + /// 加载模组 + /// + public bool Load(params object[] objs) + { + if (IsLoaded) + { + return false; + } + // BeforeLoad可以阻止加载此模组 + if (BeforeLoad()) + { + // 模组加载后,不允许再次加载此模组 + IsLoaded = true; + // 如果加载后需要执行代码,请重写AfterLoad方法 + AfterLoad(); + } + return IsLoaded; + } + + /// + /// 模组加载后需要做的事 + /// + protected virtual void AfterLoad() + { + // override + } + + /// + /// 允许返回false来阻止加载此模组 + /// + /// + protected virtual bool BeforeLoad() + { + return true; + } + } +} diff --git a/Service/AddonManager.cs b/Service/AddonManager.cs index fe17d44..eba5e42 100644 --- a/Service/AddonManager.cs +++ b/Service/AddonManager.cs @@ -1,4 +1,5 @@ using System.Reflection; +using Milimoe.FunGame.Core.Entity; using Milimoe.FunGame.Core.Library.Common.Addon; using Milimoe.FunGame.Core.Library.Constant; @@ -41,9 +42,12 @@ namespace Milimoe.FunGame.Core.Service /// 从gamemodes目录加载所有模组 /// /// + /// + /// + /// /// /// - internal static Dictionary LoadGameModes(Dictionary gamemodes, params object[] objs) + internal static Dictionary LoadGameModes(Dictionary gamemodes, List Characters, List Skills, List Items, params object[] objs) { if (!Directory.Exists(ReflectionSet.GameModeFolderPath)) return gamemodes; @@ -61,6 +65,33 @@ namespace Milimoe.FunGame.Core.Service gamemodes.TryAdd(instance.Name, instance); } } + + foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(Character)))) + { + Character? instance = (Character?)Activator.CreateInstance(type); + if (instance != null && !Characters.Contains(instance)) + { + Characters.Add(instance); + } + } + + foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(Skill)))) + { + Skill? instance = (Skill?)Activator.CreateInstance(type); + if (instance != null && !Skills.Contains(instance)) + { + Skills.Add(instance); + } + } + + foreach (Type type in assembly.GetTypes().AsEnumerable().Where(type => type.IsSubclassOf(typeof(Item)))) + { + Item? instance = (Item?)Activator.CreateInstance(type); + if (instance != null && !Items.Contains(instance)) + { + Items.Add(instance); + } + } } return gamemodes;