Browse Source

Entity和Component继承ISupportInitialize,实现EndInit,反序列化后进行初始化

tanghai 11 năm trước cách đây
mục cha
commit
53a7803ad3

+ 1 - 13
CSharp/Game/Model/Buff.cs

@@ -4,18 +4,6 @@ namespace Model
 {
     public class Buff: Object
     {
-        private BuffType type;
-
-        public BuffType Type
-        {
-            get
-            {
-                return this.type;
-            }
-            set
-            {
-                this.type = value;
-            }
-        }
+        public BuffType Type { get; set; }
     }
 }

+ 5 - 8
CSharp/Game/Model/BuffComponent.cs

@@ -1,14 +1,15 @@
 using System.Collections.Generic;
-using System.ComponentModel;
 using Common.Base;
 using MongoDB.Bson;
+using MongoDB.Bson.Serialization.Attributes;
 using Component = Common.Base.Component;
 
 namespace Model
 {
-    public class BuffComponent: Component, ISupportInitialize
+    public class BuffComponent: Component
     {
-        public HashSet<Buff> Buffs { get; set; }
+        [BsonElement]
+        private HashSet<Buff> Buffs { get; set; }
 
         private Dictionary<ObjectId, Buff> buffGuidDict { get; set; }
 
@@ -21,11 +22,7 @@ namespace Model
             this.buffTypeMMap = new MultiMap<BuffType, Buff>();
         }
 
-        void ISupportInitialize.BeginInit()
-        {
-        }
-
-        void ISupportInitialize.EndInit()
+        public override void EndInit()
         {
             foreach (var buff in this.Buffs)
             {

+ 1 - 13
CSharp/Game/Model/GameObject.cs

@@ -9,18 +9,6 @@ namespace Model
 
     public class GameObject: Entity
     {
-        private GameObjectType type;
-
-        public GameObjectType Type
-        {
-            get
-            {
-                return this.type;
-            }
-            set
-            {
-                this.type = value;
-            }
-        }
+        public GameObjectType Type { get; set; }
     }
 }

+ 10 - 4
CSharp/Platform/Common/Base/Component.cs

@@ -1,13 +1,15 @@
-using MongoDB.Bson.Serialization.Attributes;
+using System.ComponentModel;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Common.Base
 {
-    public class Component: Object
+    public abstract class Component : Object, ISupportInitialize
     {
         private Entity owner;
 
         [BsonIgnore]
-        public Entity Owner {
+        public Entity Owner
+        {
             get
             {
                 return owner;
@@ -19,7 +21,11 @@ namespace Common.Base
             }
         }
 
-        protected Component()
+        public virtual void BeginInit()
+        {
+        }
+
+        public virtual void EndInit()
         {
         }
     }

+ 32 - 8
CSharp/Platform/Common/Base/Entity.cs

@@ -1,32 +1,43 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
 using System.Linq;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Common.Base
 {
-    public class Entity: Object
+    public abstract class Entity : Object, ISupportInitialize
     {
-        public Dictionary<string, Component> Components { get; private set; }
+        [BsonElement]
+        private HashSet<Component> Components { get; set; }
+
+        private Dictionary<Type, Component> ComponentDict { get; set; }
 
         protected Entity()
         {
-            this.Components = new Dictionary<string, Component>();
+            this.Components = new HashSet<Component>();
+            this.ComponentDict = new Dictionary<Type, Component>();
         }
 
         public void AddComponent<T>() where T : Component, new()
         {
             T t = new T { Owner = this };
-            this.Components.Add(typeof(T).Name, t);
+            this.Components.Add(t);
+            this.ComponentDict.Add(typeof (T), t);
         }
 
         public void RemoveComponent<T>() where T : Component
         {
-            this.Components.Remove(typeof(T).Name);
+            Component t;
+            this.ComponentDict.TryGetValue(typeof(T), out t);
+            this.Components.Remove(t);
+            this.ComponentDict.Remove(typeof(T));
         }
 
         public T GetComponent<T>() where T : Component
         {
             Component t;
-            if (!this.Components.TryGetValue(typeof(T).Name, out t))
+            if (!this.ComponentDict.TryGetValue(typeof (T), out t))
             {
                 return null;
             }
@@ -35,7 +46,20 @@ namespace Common.Base
 
         public Component[] GetComponents()
         {
-            return this.Components.Values.ToArray();
+            return this.Components.ToArray();
+        }
+
+        public virtual void BeginInit()
+        {
+        }
+
+        public virtual void EndInit()
+        {
+            foreach (Component component in this.Components)
+            {
+                this.ComponentDict.Add(component.GetType(), component);
+                component.Owner = this;
+            }
         }
     }
 }

+ 2 - 2
CSharp/Platform/Common/Base/Object.cs

@@ -4,14 +4,14 @@ using MongoDB.Bson.Serialization.Attributes;
 
 namespace Common.Base
 {
-    public class Object
+    public abstract class Object
     {
         [BsonId]
         public ObjectId Guid { get; protected set; }
 
         [BsonElement]
         [BsonIgnoreIfNull]
-        public Dictionary<string, object> Values;
+        private Dictionary<string, object> Values;
 
         protected Object()
         {