diff --git a/CSharp/Game/Controller/BuffComponentExtension.cs b/CSharp/Game/Controller/BuffComponentExtension.cs new file mode 100644 index 0000000000000000000000000000000000000000..8409f5229a4653dba5194f37033bf0375eb0861c --- /dev/null +++ b/CSharp/Game/Controller/BuffComponentExtension.cs @@ -0,0 +1,96 @@ +using System; +using System.Collections.Generic; +using Common.Event; +using Model; +using MongoDB.Bson; + +namespace Controller +{ + /// + /// 控制复杂的buff逻辑,可以reload + /// + public static class BuffComponentExtension + { + public static void Add(this BuffComponent buffComponent, Buff buff) + { + if (buffComponent.buffs.Contains(buff)) + { + throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id)); + } + + if (buffComponent.idBuff.ContainsKey(buff.Id)) + { + throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id)); + } + + Env env = new Env(); + env[EnvKey.Unit] = buffComponent.Owner; + env[EnvKey.Buff] = buff; + + World.Instance.GetComponent>().Trigger(WorldEventType.BeforeAddBuff, env); + + buffComponent.buffs.Add(buff); + buffComponent.idBuff.Add(buff.Id, buff); + buffComponent.typeBuff.Add(buff.Config.Type, buff); + + World.Instance.GetComponent>().Trigger(WorldEventType.AfterAddBuff, env); + } + + public static Buff GetById(this BuffComponent buffComponent, ObjectId id) + { + if (!buffComponent.idBuff.ContainsKey(id)) + { + return null; + } + + return buffComponent.idBuff[id]; + } + + public static Buff GetOneByType(this BuffComponent buffComponent, BuffType type) + { + return buffComponent.typeBuff.GetOne(type); + } + + public static Buff[] GetByType(this BuffComponent buffComponent, BuffType type) + { + return buffComponent.typeBuff.GetByKey(type); + } + + private static bool Remove(this BuffComponent buffComponent, Buff buff) + { + if (buff == null) + { + return false; + } + + Env env = new Env(); + env[EnvKey.Unit] = buffComponent.Owner; + env[EnvKey.Buff] = buff; + + World.Instance.GetComponent>().Trigger(WorldEventType.BeforeRemoveBuff, env); + + buffComponent.buffs.Remove(buff); + buffComponent.idBuff.Remove(buff.Id); + buffComponent.typeBuff.Remove(buff.Config.Type, buff); + + World.Instance.GetComponent>().Trigger(WorldEventType.AfterRemoveBuff, env); + + return true; + } + + public static bool RemoveById(this BuffComponent buffComponent, ObjectId id) + { + Buff buff = buffComponent.GetById(id); + return buffComponent.Remove(buff); + } + + public static void RemoveByType(this BuffComponent buffComponent, BuffType type) + { + Buff[] allbuffs = buffComponent.GetByType(type); + foreach (Buff buff in allbuffs) + { + buffComponent.Remove(buff); + } + } + } +} \ No newline at end of file diff --git a/CSharp/Game/Controller/BuffController.cs b/CSharp/Game/Controller/BuffController.cs deleted file mode 100644 index a4e7a6d0926e30dc8f6ba06b81a7614651996157..0000000000000000000000000000000000000000 --- a/CSharp/Game/Controller/BuffController.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Controller -{ - /// - /// 控制复杂的buff逻辑,可以reload - /// - public class BuffController - { - } -} \ No newline at end of file diff --git a/CSharp/Game/Controller/Controller.csproj b/CSharp/Game/Controller/Controller.csproj index 65c7a9428f793b61a165b85d68dfed659eb13006..23c50ee4d828969e3650f4dd67a8f3c77df0bafd 100644 --- a/CSharp/Game/Controller/Controller.csproj +++ b/CSharp/Game/Controller/Controller.csproj @@ -48,13 +48,16 @@ - + + + + diff --git a/CSharp/Game/Controller/EnvKey.cs b/CSharp/Game/Controller/EnvKey.cs new file mode 100644 index 0000000000000000000000000000000000000000..8ee9137e1a7db242ce728bb3eb9d80ef9878f849 --- /dev/null +++ b/CSharp/Game/Controller/EnvKey.cs @@ -0,0 +1,8 @@ +namespace Controller +{ + public static class EnvKey + { + public const string Unit = "Unit"; + public const string Buff = "Buff"; + } +} diff --git a/CSharp/Game/Controller/Factory/UnitFactory.cs b/CSharp/Game/Controller/Factory/UnitFactory.cs index bb90c6c3edf77c7b7d11fd5e531dca8c978e3f9a..22ce0d0e15988f2f16caeabb6a611a83da45766d 100644 --- a/CSharp/Game/Controller/Factory/UnitFactory.cs +++ b/CSharp/Game/Controller/Factory/UnitFactory.cs @@ -9,6 +9,9 @@ namespace Controller { Unit player = new Unit(configId); player.AddComponent(); + Buff buff = new Buff(1); + player.GetComponent().Add(buff); + World.Instance.GetComponent().Add(player); return player; } } diff --git a/CSharp/Game/Controller/UnitComponentExtension.cs b/CSharp/Game/Controller/UnitComponentExtension.cs new file mode 100644 index 0000000000000000000000000000000000000000..6ef762c7565aed52e76843a8881218006c30f143 --- /dev/null +++ b/CSharp/Game/Controller/UnitComponentExtension.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Model; +using MongoDB.Bson; + +namespace Controller +{ + /// + /// 控制复杂的unit逻辑,可以reload + /// + public static class UnitComponentExtension + { + public static void Add(this UnitComponent unitComponent, Unit unit) + { + unitComponent.units.Add(unit.Id, unit); + if (!unitComponent.typeUnits.ContainsKey(unit.Config.Type)) + { + unitComponent.typeUnits.Add(unit.Config.Type, new Dictionary()); + } + unitComponent.typeUnits[unit.Config.Type].Add(unit.Id, unit); + } + + public static Unit Get(this UnitComponent unitComponent, ObjectId id) + { + Unit unit = null; + unitComponent.units.TryGetValue(id, out unit); + return unit; + } + + public static Unit[] GetOneType(this UnitComponent unitComponent, int type) + { + Dictionary oneTypeUnits = null; + if (!unitComponent.typeUnits.TryGetValue(type, out oneTypeUnits)) + { + return new Unit[0]; + } + return oneTypeUnits.Values.ToArray(); + } + + public static bool Remove(this UnitComponent unitComponent, Unit unit) + { + if (unit == null) + { + throw new ArgumentNullException("unit"); + } + if (!unitComponent.units.Remove(unit.Id)) + { + return false; + } + if (!unitComponent.typeUnits[unit.Config.Type].Remove(unit.Id)) + { + return false; + } + return true; + } + + public static bool Remove(this UnitComponent unitComponent, ObjectId id) + { + Unit unit = unitComponent.Get(id); + if (unit == null) + { + return false; + } + return unitComponent.Remove(unit); + } + } +} \ No newline at end of file diff --git a/CSharp/Game/Controller/WorldEventType.cs b/CSharp/Game/Controller/WorldEventType.cs new file mode 100644 index 0000000000000000000000000000000000000000..0c013e8e6426e532965e02dea707b9ebab2f7f07 --- /dev/null +++ b/CSharp/Game/Controller/WorldEventType.cs @@ -0,0 +1,10 @@ +namespace Controller +{ + public static class WorldEventType + { + public const int BeforeAddBuff = 0; + public const int AfterAddBuff = 1; + public const int BeforeRemoveBuff = 2; + public const int AfterRemoveBuff = 3; + } +} diff --git a/CSharp/Game/Model/Component/BehaviorTreeComponent.cs b/CSharp/Game/Model/Component/BehaviorTreeComponent.cs index 81864a86ed2b27765ce27d393f151cf83073e625..e4b025d880815e5b9dc2579bb8ac8232ec68efbe 100644 --- a/CSharp/Game/Model/Component/BehaviorTreeComponent.cs +++ b/CSharp/Game/Model/Component/BehaviorTreeComponent.cs @@ -4,7 +4,7 @@ using Common.Base; namespace Model { - public class BehaviorTreeComponent: Component + public class BehaviorTreeComponent : Component, IAssemblyLoader { private Dictionary trees = new Dictionary(); diff --git a/CSharp/Game/Model/Component/BuffComponent.cs b/CSharp/Game/Model/Component/BuffComponent.cs index 92fa650ad1c903fbb3c0e62ec1712470de99fe05..744bd8ba7d404f9f616dcde05adb135a3028bee3 100644 --- a/CSharp/Game/Model/Component/BuffComponent.cs +++ b/CSharp/Game/Model/Component/BuffComponent.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using Common.Base; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; @@ -9,11 +8,13 @@ namespace Model public class BuffComponent: Component { [BsonElement] - private HashSet buffs; + public HashSet buffs; - private Dictionary idBuff; + [BsonIgnore] + public Dictionary idBuff; - private MultiMap typeBuff; + [BsonIgnore] + public MultiMap typeBuff; public BuffComponent() { @@ -41,71 +42,5 @@ namespace Model this.typeBuff.Add(buff.Config.Type, buff); } } - - public void Add(Buff buff) - { - if (this.buffs.Contains(buff)) - { - throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id)); - } - - if (this.idBuff.ContainsKey(buff.Id)) - { - throw new ArgumentException(string.Format("already exist same buff, Id: {0} ConfigId: {1}", buff.Id, buff.Config.Id)); - } - - this.buffs.Add(buff); - this.idBuff.Add(buff.Id, buff); - this.typeBuff.Add(buff.Config.Type, buff); - } - - public Buff GetById(ObjectId id) - { - if (!this.idBuff.ContainsKey(id)) - { - return null; - } - - return this.idBuff[id]; - } - - public Buff GetOneByType(BuffType type) - { - return this.typeBuff.GetOne(type); - } - - public Buff[] GetByType(BuffType type) - { - return this.typeBuff.GetByKey(type); - } - - private bool Remove(Buff buff) - { - if (buff == null) - { - return false; - } - - this.buffs.Remove(buff); - this.idBuff.Remove(buff.Id); - this.typeBuff.Remove(buff.Config.Type, buff); - - return true; - } - - public bool RemoveById(ObjectId id) - { - Buff buff = this.GetById(id); - return this.Remove(buff); - } - - public void RemoveByType(BuffType type) - { - Buff[] allbuffs = this.GetByType(type); - foreach (Buff buff in allbuffs) - { - this.Remove(buff); - } - } } } \ No newline at end of file diff --git a/CSharp/Game/Model/Component/ConfigComponent.cs b/CSharp/Game/Model/Component/ConfigComponent.cs index c7b435953f96ced10a46905b0caf7c17bfd3e64e..421cc3ceb8efa3296bb18753a21057c22fe0b3e0 100644 --- a/CSharp/Game/Model/Component/ConfigComponent.cs +++ b/CSharp/Game/Model/Component/ConfigComponent.cs @@ -6,7 +6,7 @@ using Common.Config; namespace Model { - public class ConfigComponent: Component + public class ConfigComponent : Component, IAssemblyLoader { public Dictionary allConfig; diff --git a/CSharp/Game/Model/Component/EventComponent.cs b/CSharp/Game/Model/Component/EventComponent.cs index fd52d9d7766115c54824ca30e250a2e90f43449d..8535ea96ec00d787a0e987229807bddee2d4a5c4 100644 --- a/CSharp/Game/Model/Component/EventComponent.cs +++ b/CSharp/Game/Model/Component/EventComponent.cs @@ -6,7 +6,8 @@ using Common.Event; namespace Model { - public class EventComponent: Component where T : AEventAttribute + public class EventComponent : Component, IAssemblyLoader + where AttributeType : AEventAttribute { private Dictionary> events; @@ -17,7 +18,7 @@ namespace Model var types = assembly.GetTypes(); foreach (var t in types) { - object[] attrs = t.GetCustomAttributes(typeof (T), false); + object[] attrs = t.GetCustomAttributes(typeof (AttributeType), false); if (attrs.Length == 0) { continue; diff --git a/CSharp/Game/Model/Component/FactoryComponent.cs b/CSharp/Game/Model/Component/FactoryComponent.cs index 70238b71d36b1e150fcabcf2e3a6f015386e61eb..5b1e712091d29622de3a70bf643d6f21beed2411 100644 --- a/CSharp/Game/Model/Component/FactoryComponent.cs +++ b/CSharp/Game/Model/Component/FactoryComponent.cs @@ -5,7 +5,7 @@ using Common.Base; namespace Model { - public class FactoryComponent: Component where T : Entity + public class FactoryComponent : Component, IAssemblyLoader where T : Entity { private Dictionary> allConfig; diff --git a/CSharp/Game/Model/Component/UnitComponent.cs b/CSharp/Game/Model/Component/UnitComponent.cs index 2d5cafe7f842ec043829868612ed9329e7cbf973..cfd46e1ada8eff6319a2a7d3e19558cf2d4581a2 100644 --- a/CSharp/Game/Model/Component/UnitComponent.cs +++ b/CSharp/Game/Model/Component/UnitComponent.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Collections.Generic; using Common.Base; using MongoDB.Bson; @@ -8,64 +6,10 @@ namespace Model { public class UnitComponent: Component { - private readonly Dictionary units = + public readonly Dictionary units = new Dictionary(); - private readonly Dictionary> typeUnits = + public readonly Dictionary> typeUnits = new Dictionary>(); - - public void Add(Unit unit) - { - this.units.Add(unit.Id, unit); - if (!this.typeUnits.ContainsKey(unit.Config.Type)) - { - this.typeUnits.Add(unit.Config.Type, new Dictionary()); - } - this.typeUnits[unit.Config.Type].Add(unit.Id, unit); - } - - public Unit Get(ObjectId id) - { - Unit unit = null; - this.units.TryGetValue(id, out unit); - return unit; - } - - public Unit[] GetOneType(int type) - { - Dictionary oneTypeUnits = null; - if (!this.typeUnits.TryGetValue(type, out oneTypeUnits)) - { - return new Unit[0]; - } - return oneTypeUnits.Values.ToArray(); - } - - public bool Remove(Unit unit) - { - if (unit == null) - { - throw new ArgumentNullException("unit"); - } - if (!this.units.Remove(unit.Id)) - { - return false; - } - if (!this.typeUnits[unit.Config.Type].Remove(unit.Id)) - { - return false; - } - return true; - } - - public bool Remove(ObjectId id) - { - Unit unit = this.Get(id); - if (unit == null) - { - return false; - } - return this.Remove(unit); - } } } \ No newline at end of file diff --git a/CSharp/Game/Model/IAssemblyLoader.cs b/CSharp/Game/Model/IAssemblyLoader.cs new file mode 100644 index 0000000000000000000000000000000000000000..3e4125fdaa785b6802d6f6d9139b6bb7bb2fa177 --- /dev/null +++ b/CSharp/Game/Model/IAssemblyLoader.cs @@ -0,0 +1,8 @@ +using System.Reflection; +namespace Model +{ + public interface IAssemblyLoader + { + void Load(Assembly assembly); + } +} diff --git a/CSharp/Game/Model/Model.csproj b/CSharp/Game/Model/Model.csproj index 9e0154b28672c49cba842a400c52a51042eb41d9..bd537d56aba45fe235da16810f44d33774863fde 100644 --- a/CSharp/Game/Model/Model.csproj +++ b/CSharp/Game/Model/Model.csproj @@ -59,6 +59,7 @@ + @@ -67,6 +68,8 @@ + + diff --git a/CSharp/Game/Model/UnitEventAttribute.cs b/CSharp/Game/Model/UnitEventAttribute.cs new file mode 100644 index 0000000000000000000000000000000000000000..00f8bce972398122b09b50ff07787efc257d111c --- /dev/null +++ b/CSharp/Game/Model/UnitEventAttribute.cs @@ -0,0 +1,11 @@ +using Common.Event; + +namespace Model +{ + public class UnitEventAttribute: AEventAttribute + { + public UnitEventAttribute(int type): base(type) + { + } + } +} diff --git a/CSharp/Game/Model/World.cs b/CSharp/Game/Model/World.cs index b0c6e207544f130eda38857c828ce2fc8530fe61..2bbf7481e21a854e0dd9d5e4e2f47639b019180f 100644 --- a/CSharp/Game/Model/World.cs +++ b/CSharp/Game/Model/World.cs @@ -1,9 +1,15 @@ -namespace Model +using System.IO; +using System.Reflection; +using Common.Base; + +namespace Model { public class World : GameObject { private static readonly World instance = new World(); + public Assembly Assembly { get; set; } + public static World Instance { get @@ -14,6 +20,22 @@ private World() { + this.Assembly = Assembly.Load(File.ReadAllBytes(@"./Controller.dll")); + } + + public void ReLoad() + { + this.Assembly = Assembly.Load(File.ReadAllBytes(@"./Controller.dll")); + + foreach (Component component in this.GetComponents()) + { + IAssemblyLoader assemblyLoader = component as IAssemblyLoader; + if (assemblyLoader == null) + { + continue; + } + assemblyLoader.Load(this.Assembly); + } } } } \ No newline at end of file diff --git a/CSharp/Game/Model/WorldEventAttribute.cs b/CSharp/Game/Model/WorldEventAttribute.cs new file mode 100644 index 0000000000000000000000000000000000000000..1c7013a1c04e8d2ef5ae100d59fb0241e6391e2c --- /dev/null +++ b/CSharp/Game/Model/WorldEventAttribute.cs @@ -0,0 +1,11 @@ +using Common.Event; + +namespace Model +{ + public class WorldEventAttribute: AEventAttribute + { + public WorldEventAttribute(int type): base(type) + { + } + } +} diff --git a/CSharp/Game/MongoDBTest/MongoDBTest.cs b/CSharp/Game/MongoDBTest/MongoDBTest.cs index 636479d3622771782be2f46613c2d1e7f4073389..0848ed9bddfafa7256f55d1da3da722732562407 100644 --- a/CSharp/Game/MongoDBTest/MongoDBTest.cs +++ b/CSharp/Game/MongoDBTest/MongoDBTest.cs @@ -1,6 +1,4 @@ using System; -using System.IO; -using System.Reflection; using Common.Helper; using Microsoft.VisualStudio.TestTools.UnitTesting; using Model; @@ -23,21 +21,23 @@ namespace MongoDBTest World world = World.Instance; - Assembly assembly = Assembly.Load(File.ReadAllBytes(@"./Controller.dll")); // 加载配置 - world.AddComponent().Load(assembly); + world.AddComponent().Load(world.Assembly); + + // 事件管理器 + world.AddComponent>().Load(world.Assembly); + + world.AddComponent(); // 构造工厂 - world.AddComponent>().Load(assembly); + world.AddComponent>().Load(world.Assembly); // 构造行为树 - world.AddComponent().Load(assembly); + world.AddComponent().Load(world.Assembly); Unit player1 = world.GetComponent>().Create(1); - Buff buff = new Buff(1); - player1.GetComponent().Add(buff); player1["hp"] = 10; collection.Insert(player1); diff --git a/CSharp/Game/MongoDBTest/MongoDBTest.csproj b/CSharp/Game/MongoDBTest/MongoDBTest.csproj index 6f2ebb627a50e14942039b65f3403afdd970db97..9c030d367a9d801cb0adb9d01dfb2f246c777cf2 100644 --- a/CSharp/Game/MongoDBTest/MongoDBTest.csproj +++ b/CSharp/Game/MongoDBTest/MongoDBTest.csproj @@ -69,10 +69,6 @@ {19F8F043-1F99-4550-99DF-DEA5C7D77E55} Common - - {e69c1f22-e004-4d69-8667-787bd243454b} - Controller - {0fa529d1-d0a9-4a8e-90f5-117ce80f2ede} Model diff --git a/CSharp/Platform/Common/Base/Entity.cs b/CSharp/Platform/Common/Base/Entity.cs index 173d789e35116bcc20a4e8108d6bec77882ad7ff..e662b62b6b93694ecb2862d1c3355088418fff7a 100644 --- a/CSharp/Platform/Common/Base/Entity.cs +++ b/CSharp/Platform/Common/Base/Entity.cs @@ -57,9 +57,7 @@ namespace Common.Base Component t; if (!this.componentDict.TryGetValue(typeof (T), out t)) { - throw new Exception( - string.Format("GetComponent, component not exist, id: {0}, component: {1}", - this.Id, typeof(T).Name)); + return default (T); } return (T) t; } diff --git a/CSharp/Platform/Common/Base/Object.cs b/CSharp/Platform/Common/Base/Object.cs index d99f49e44c32aba8003b397c6f1018d460c27c91..7230608e3653c484bcbf11751b8e2d6c4b27db8a 100644 --- a/CSharp/Platform/Common/Base/Object.cs +++ b/CSharp/Platform/Common/Base/Object.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System.Collections; +using System.Collections.Generic; using System.ComponentModel; using MongoDB.Bson; using MongoDB.Bson.Serialization.Attributes; @@ -72,7 +73,7 @@ namespace Common.Base this.values[typeof (T).Name] = obj; } - public bool Contain(string key) + public bool ContainKey(string key) { return this.values.ContainsKey(key); } @@ -87,6 +88,20 @@ namespace Common.Base return ret; } + public void Add(string key, object value) + { + if (this.values == null) + { + this.values = new Dictionary(); + } + this.values.Add(key, value); + } + + public IEnumerator GetEnumerator() + { + return this.values.GetEnumerator(); + } + public virtual void BeginInit() { if (this.values == null)