Просмотр исходного кода

1.简单的逻辑代码可以放到Component
2.增加Component继承模式,比如怪物的死亡和玩家的死亡不一样,这样可以设计PlayerDeadComponent和MonsterDeadComponent继承于DeadComponent,实现不同的死亡.但是带来一个问题,这部分逻辑将无法reload

tanghai 11 лет назад
Родитель
Сommit
ede8c8db9c

+ 0 - 96
CSharp/Game/Controller/BuffComponentExtension.cs

@@ -1,96 +0,0 @@
-using System;
-using System.Collections.Generic;
-using Common.Event;
-using Model;
-using MongoDB.Bson;
-
-namespace Controller
-{
-    /// <summary>
-    /// 控制复杂的buff逻辑,可以reload
-    /// </summary>
-    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<EventComponent<EventAttribute>>().Trigger(EventType.BeforeAddBuff, env);
-
-            buffComponent.buffs.Add(buff);
-            buffComponent.idBuff.Add(buff.Id, buff);
-            buffComponent.typeBuff.Add(buff.Config.Type, buff);
-
-            World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.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<EventComponent<EventAttribute>>().Trigger(EventType.BeforeRemoveBuff, env);
-
-            buffComponent.buffs.Remove(buff);
-            buffComponent.idBuff.Remove(buff.Id);
-            buffComponent.typeBuff.Remove(buff.Config.Type, buff);
-
-            World.Instance.GetComponent<EventComponent<EventAttribute>>().Trigger(EventType.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);
-            }
-        }
-    }
-}

+ 0 - 2
CSharp/Game/Controller/Controller.csproj

@@ -49,8 +49,6 @@
     <Compile Include="BehaviorTreeNode\Selector.cs" />
     <Compile Include="BehaviorTreeNode\Sequence.cs" />
     <Compile Include="EnvKey.cs" />
-    <Compile Include="UnitComponentExtension.cs" />
-    <Compile Include="BuffComponentExtension.cs" />
     <Compile Include="ConfigCategory\BuffCategory.cs" />
     <Compile Include="ConfigCategory\GlobalCategory.cs" />
     <Compile Include="ConfigCategory\NodeCategory.cs" />

+ 1 - 0
CSharp/Game/Controller/Factory/UnitFactory.cs

@@ -8,6 +8,7 @@ namespace Controller
         public Unit Create(int configId)
         {
             Unit player = new Unit(configId);
+            player.AddComponent<PlayerDeadComponent>();
             player.AddComponent<BuffComponent>();
             Buff buff = new Buff(1);
             player.GetComponent<BuffComponent>().Add(buff);

+ 0 - 68
CSharp/Game/Controller/UnitComponentExtension.cs

@@ -1,68 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using Model;
-using MongoDB.Bson;
-
-namespace Controller
-{
-    /// <summary>
-    /// 控制复杂的unit逻辑,可以reload
-    /// </summary>
-    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<ObjectId, Unit>());
-            }
-            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<ObjectId, Unit> 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);
-        }
-    }
-}

+ 72 - 7
CSharp/Game/Model/Component/BuffComponent.cs

@@ -1,20 +1,19 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using Common.Base;
 using MongoDB.Bson;
 using MongoDB.Bson.Serialization.Attributes;
 
 namespace Model
 {
-    public class BuffComponent: Component<Unit>
+    public class BuffComponent : Component<Unit>
     {
         [BsonElement]
-        public HashSet<Buff> buffs;
+        private HashSet<Buff> buffs;
 
-        [BsonIgnore]
-        public Dictionary<ObjectId, Buff> idBuff;
+        private Dictionary<ObjectId, Buff> idBuff;
 
-        [BsonIgnore]
-        public MultiMap<BuffType, Buff> typeBuff;
+        private MultiMap<BuffType, Buff> typeBuff;
 
         public BuffComponent()
         {
@@ -42,5 +41,71 @@ 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);
+            }
+        }
     }
 }

+ 15 - 0
CSharp/Game/Model/Component/DeadComponent.cs

@@ -0,0 +1,15 @@
+using System;
+using Common.Base;
+
+namespace Model
+{
+    public abstract class DeadComponent: Component<Unit>
+    {
+        public override Type GetComponentType()
+        {
+            return typeof (DeadComponent);
+        }
+
+        public abstract void Dead();
+    }
+}

+ 9 - 0
CSharp/Game/Model/Component/MonsterDeadComponent.cs

@@ -0,0 +1,9 @@
+namespace Model
+{
+    public class MonsterDeadComponent: DeadComponent
+    {
+        public override void Dead()
+        {
+        }
+    }
+}

+ 12 - 0
CSharp/Game/Model/Component/PlayerDeadComponent.cs

@@ -0,0 +1,12 @@
+using System;
+
+namespace Model
+{
+    public class PlayerDeadComponent: DeadComponent
+    {
+        public override void Dead()
+        {
+            Console.WriteLine("Player {0} Dead!", this.Owner.Id);
+        }
+    }
+}

+ 60 - 4
CSharp/Game/Model/Component/UnitComponent.cs

@@ -1,15 +1,71 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
+using System.Linq;
 using Common.Base;
 using MongoDB.Bson;
 
 namespace Model
 {
-    public class UnitComponent: Component<World>
+    public class UnitComponent : Component<World>
     {
-        public readonly Dictionary<ObjectId, Unit> units =
+        private readonly Dictionary<ObjectId, Unit> units =
                 new Dictionary<ObjectId, Unit>();
 
-        public readonly Dictionary<int, Dictionary<ObjectId, Unit>> typeUnits =
+        private readonly Dictionary<int, Dictionary<ObjectId, Unit>> typeUnits =
                 new Dictionary<int, Dictionary<ObjectId, Unit>>();
+
+        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<ObjectId, Unit>());
+            }
+            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<ObjectId, Unit> 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);
+        }
     }
 }

+ 3 - 0
CSharp/Game/Model/Model.csproj

@@ -52,10 +52,13 @@
     <Compile Include="BehaviorTree\Node.cs" />
     <Compile Include="BehaviorTree\NodeAttribute.cs" />
     <Compile Include="Buff.cs" />
+    <Compile Include="Component\DeadComponent.cs" />
     <Compile Include="Component\MessageComponent.cs" />
     <Compile Include="Component\BuffComponent.cs" />
     <Compile Include="Component\ConfigComponent.cs" />
     <Compile Include="Component\EventComponent.cs" />
+    <Compile Include="Component\MonsterDeadComponent.cs" />
+    <Compile Include="Component\PlayerDeadComponent.cs" />
     <Compile Include="Config\UnitConfig.cs" />
     <Compile Include="Config\BuffConfig.cs" />
     <Compile Include="MessageAttribute.cs" />

+ 4 - 0
CSharp/Game/MongoDBTest/MongoDBTest.cs

@@ -34,10 +34,14 @@ namespace MongoDBTest
             Unit player1 = world.GetComponent<FactoryComponent<Unit>>().Create(1);
             player1["hp"] = 10;
 
+            player1.GetComponent<DeadComponent>().Dead();
+
             collection.Insert(player1);
 
             var query = Query<Unit>.EQ(e => e.Id, player1.Id);
             Unit player2 = collection.FindOne(query);
+
+            player2.GetComponent<DeadComponent>().Dead();
             
             Console.WriteLine(MongoHelper.ToJson(player2));
             Assert.AreEqual(MongoHelper.ToJson(player1), MongoHelper.ToJson(player2));

+ 11 - 1
CSharp/Platform/Common/Base/Component.cs

@@ -1,4 +1,5 @@
-using MongoDB.Bson.Serialization.Attributes;
+using System;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Common.Base
 {
@@ -22,5 +23,14 @@ namespace Common.Base
                 this.Id = this.owner.Id;
             }
         }
+
+        /// <summary>
+        /// 用于父类的Component需要重写此方法
+        /// </summary>
+        /// <returns></returns>
+        public virtual Type GetComponentType()
+        {
+            return this.GetType();
+        }
     }
 }

+ 23 - 5
CSharp/Platform/Common/Base/Entity.cs

@@ -15,10 +15,12 @@ namespace Common.Base
 
         public T AddComponent<T>() where T : Component<K>, new()
         {
-            if (this.componentDict.ContainsKey(typeof (T)))
+            T t = new T { Owner = (K) this };
+
+            if (this.componentDict.ContainsKey(t.GetComponentType()))
             {
                 throw new Exception(
-                    string.Format("AddComponent, component already exist, id: {0}, component: {1}", 
+                    string.Format("AddComponent, component already exist, id: {0}, component: {1}",
                     this.Id, typeof(T).Name));
             }
 
@@ -27,12 +29,28 @@ namespace Common.Base
                 this.components = new HashSet<Component<K>>();
             }
 
-            T t = new T { Owner = (K)this };
             this.components.Add(t);
-            this.componentDict.Add(typeof (T), t);
+            this.componentDict.Add(t.GetComponentType(), t);
             return t;
         }
 
+        public void AddComponent(Component<K> component)
+        {
+            if (this.componentDict.ContainsKey(component.GetComponentType()))
+            {
+                throw new Exception(
+                    string.Format("AddComponent, component already exist, id: {0}, component: {1}",
+                    this.Id, component.GetComponentType().Name));
+            }
+
+            if (this.components == null)
+            {
+                this.components = new HashSet<Component<K>>();
+            }
+            this.components.Add(component);
+            this.componentDict.Add(component.GetComponentType(), component);
+        }
+
         public void RemoveComponent<T>() where T : Component<K>
         {
             Component<K> t;
@@ -85,7 +103,7 @@ namespace Common.Base
             foreach (var component in this.components)
             {
                 component.Owner = (K)this;
-                this.componentDict.Add(component.GetType(), component);
+                this.componentDict.Add(component.GetComponentType(), component);
             }
         }
     }