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

MongoDB反序列化顺序是反序列化子成员,再反序列化本身,所以,EndInit也是这个调用顺序,需要注意!例如Unit反序列化,Buff->BuffComponent->Unit

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

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

@@ -9,7 +9,7 @@ namespace Controller
         {
             Unit player = new Unit(configId);
             player.AddComponent<BuffComponent>();
-            Buff buff = new Buff(1);
+            Buff buff = new Buff(1, player.Id);
             player.GetComponent<BuffComponent>().Add(buff);
             World.Instance.GetComponent<UnitComponent>().Add(player);
             return player;

+ 66 - 5
CSharp/Game/Model/Buff.cs

@@ -1,15 +1,20 @@
 using Common.Base;
+using Common.Event;
 using Common.Helper;
 using MongoDB.Bson;
 using MongoDB.Bson.Serialization.Attributes;
+using System;
 
 namespace Model
 {
-    public class Buff: Entity<Buff>
-    {
+    public class Buff: Entity<Buff>, IDisposable
+	{
         [BsonElement]
         private int configId { get; set; }
 
+		[BsonElement]
+		private ObjectId ownerId;
+
         [BsonElement]
         private long expiration;
 
@@ -42,16 +47,47 @@ namespace Model
             }
         }
 
-        public Buff(int configId)
+        public Buff(int configId, ObjectId ownerId)
         {
             this.configId = configId;
+			this.ownerId = ownerId;
             if (this.Config.Duration != 0)
             {
                 this.Expiration = TimeHelper.Now() + this.Config.Duration;
             }
+
+			if (this.Expiration != 0)
+			{
+				// 注册Timer回调
+				Env env = new Env();
+				env[EnvKey.OwnerId] = this.OwnerId;
+				env[EnvKey.BuffId] = this.Id;
+				this.TimerId = World.Instance.GetComponent<TimerComponent>()
+						.Add(this.Expiration, CallbackType.BuffTimeoutCallback, env);
+			}
         }
 
-        [BsonIgnore]
+		public override void BeginInit()
+		{
+			base.BeginInit();
+		}
+
+		public override void EndInit()
+		{
+			base.EndInit();
+
+			if (this.Expiration != 0)
+			{
+				// 注册Timer回调
+				Env env = new Env();
+				env[EnvKey.OwnerId] = this.OwnerId;
+				env[EnvKey.BuffId] = this.Id;
+				this.TimerId = World.Instance.GetComponent<TimerComponent>()
+						.Add(this.Expiration, CallbackType.BuffTimeoutCallback, env);
+			}
+		}
+
+		[BsonIgnore]
         public BuffConfig Config
         {
             get
@@ -59,5 +95,30 @@ namespace Model
                 return World.Instance.GetComponent<ConfigComponent>().Get<BuffConfig>(this.configId);
             }
         }
-    }
+
+		[BsonIgnore]
+		public ObjectId OwnerId
+		{
+			get
+			{
+				return ownerId;
+			}
+
+			set
+			{
+				this.ownerId = value;
+			}
+		}
+
+		public void Dispose()
+		{
+			if (this.Expiration == 0)
+			{
+				return;
+			}
+			
+			World.Instance.GetComponent<TimerComponent>().Remove(this.TimerId);
+			this.expiration = 0;
+		}
+	}
 }

+ 5 - 28
CSharp/Game/Model/Component/BuffComponent.cs

@@ -42,32 +42,9 @@ namespace Model
             {
                 this.idBuff.Add(buff.Id, buff);
                 this.typeBuff.Add(buff.Config.Type, buff);
-                AddToTimer(this.Owner, buff);
             }
         }
 
-        private static void AddToTimer(Unit owner, Buff buff)
-        {
-            if (buff.Expiration == 0)
-            {
-                return;
-            }
-            Env env = new Env();
-            env[EnvKey.OwnerId] = owner.Id;
-            env[EnvKey.BuffId] = buff.Id;
-            buff.TimerId = World.Instance.GetComponent<TimerComponent>()
-                    .Add(buff.Expiration, CallbackType.BuffTimeoutCallback, env);
-        }
-
-        private static void RemoveFromTimer(Buff buff)
-        {
-            if (buff.Expiration == 0)
-            {
-                return;
-            }
-            World.Instance.GetComponent<TimerComponent>().Remove(buff.TimerId);
-        }
-
         public void Add(Buff buff)
         {
             if (this.buffs.Contains(buff))
@@ -89,7 +66,6 @@ namespace Model
             this.buffs.Add(buff);
             this.idBuff.Add(buff.Id, buff);
             this.typeBuff.Add(buff.Config.Type, buff);
-            AddToTimer(this.Owner, buff);
 
             World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.AfterAddBuff, env);
         }
@@ -130,16 +106,17 @@ namespace Model
             this.buffs.Remove(buff);
             this.idBuff.Remove(buff.Id);
             this.typeBuff.Remove(buff.Config.Type, buff);
-            RemoveFromTimer(buff);
 
             World.Instance.GetComponent<EventComponent<EventAttribute>>().Run(EventType.AfterRemoveBuff, env);
-        }
+
+			buff.Dispose();
+		}
 
         public void RemoveById(ObjectId id)
         {
             Buff buff = this.GetById(id);
             this.Remove(buff);
-        }
+		}
 
         public void RemoveByType(BuffType type)
         {
@@ -147,7 +124,7 @@ namespace Model
             foreach (Buff buff in allbuffs)
             {
                 this.Remove(buff);
-            }
+			}
         }
     }
 }