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

Entity增加AddChild(Entity)方法,跟AddComponent(Entity)方法一样,方便把反序列化的Entity加入数据树,Parent的Set方法改成私有
Component跟Child变得很对称了

tanghai 4 лет назад
Родитель
Сommit
7b95cb5b15
1 измененных файлов с 57 добавлено и 45 удалено
  1. 57 45
      Unity/Assets/Model/Core/Object/Entity.cs

+ 57 - 45
Unity/Assets/Model/Core/Object/Entity.cs

@@ -135,7 +135,7 @@ namespace ET
         public Entity Parent
         {
             get => this.parent;
-            set
+            private set
             {
                 if (value == null)
                 {
@@ -161,12 +161,12 @@ namespace ET
                         Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
                         return;
                     }
-                    this.parent.RemoveChild(this);
+                    this.parent.RemoveFromChildren(this);
                 }
                 
                 this.parent = value;
-                this.parent.AddChild(this);
                 this.IsComponent = false;
+                this.parent.AddToChildren(this);
                 this.Domain = this.parent.domain;
             }
         }
@@ -182,9 +182,32 @@ namespace ET
                 {
                     throw new Exception($"Component parent is not null: {this.GetType().Name}");
                 }
+                
+                if (value == this)
+                {
+                    throw new Exception($"cant set parent self: {this.GetType().Name}");
+                }
+                
+                // 严格限制parent必须要有domain,也就是说parent必须在数据树上面
+                if (value.Domain == null)
+                {
+                    throw new Exception($"cant set parent because parent domain is null: {this.GetType().Name} {value.GetType().Name}");
+                }
+                
+                if (this.parent != null) // 之前有parent
+                {
+                    // parent相同,不设置
+                    if (this.parent == value)
+                    {
+                        Log.Error($"重复设置了Parent: {this.GetType().Name} parent: {this.parent.GetType().Name}");
+                        return;
+                    }
+                    this.parent.RemoveFromComponents(this);
+                }
 
                 this.parent = value;
                 this.IsComponent = true;
+                this.parent.AddToComponents(this);
                 this.Domain = this.parent.domain;
             }
         }
@@ -306,13 +329,13 @@ namespace ET
             }
         }
 
-        private void AddChild(Entity entity)
+        private void AddToChildren(Entity entity)
         {
             this.Children.Add(entity.Id, entity);
-            this.AddChildDB(entity);
+            this.AddToChildrenDB(entity);
         }
 
-        private void RemoveChild(Entity entity)
+        private void RemoveFromChildren(Entity entity)
         {
             if (this.children == null)
             {
@@ -327,10 +350,10 @@ namespace ET
                 this.children = null;
             }
 
-            this.RemoveChildDB(entity);
+            this.RemoveFromChildrenDB(entity);
         }
 
-        private void AddChildDB(Entity entity)
+        private void AddToChildrenDB(Entity entity)
         {
             if (!(entity is ISerializeToEntity))
             {
@@ -342,7 +365,7 @@ namespace ET
             this.childrenDB.Add(entity);
         }
 
-        private void RemoveChildDB(Entity entity)
+        private void RemoveFromChildrenDB(Entity entity)
         {
             if (!(entity is ISerializeToEntity))
             {
@@ -461,7 +484,7 @@ namespace ET
                 }
                 else
                 {
-                    this.parent.RemoveChild(this);
+                    this.parent.RemoveFromChildren(this);
                 }
             }
 
@@ -481,6 +504,11 @@ namespace ET
 
         private void AddToComponentsDB(Entity component)
         {
+            if (!(component is ISerializeToEntity))
+            {
+                return;
+            }
+            
             if (this.componentsDB == null)
             {
                 this.componentsDB = hashSetPool.Fetch();
@@ -491,6 +519,11 @@ namespace ET
 
         private void RemoveFromComponentsDB(Entity component)
         {
+            if (!(component is ISerializeToEntity))
+            {
+                return;
+            }
+            
             if (this.componentsDB == null)
             {
                 return;
@@ -504,29 +537,20 @@ namespace ET
             }
         }
 
-        private void AddToComponent(Type type, Entity component)
+        private void AddToComponents(Entity component)
         {
-            if (this.components == null)
-            {
-                this.components = dictPool.Fetch();
-            }
-
-            this.components.Add(type, component);
-
-            if (component is ISerializeToEntity)
-            {
-                this.AddToComponentsDB(component);
-            }
+            this.Components.Add(component.GetType(), component);
+            this.AddToComponentsDB(component);
         }
 
-        private void RemoveFromComponent(Type type, Entity component)
+        private void RemoveFromComponents(Entity component)
         {
             if (this.components == null)
             {
                 return;
             }
 
-            this.components.Remove(type);
+            this.components.Remove(component.GetType());
 
             if (this.components.Count == 0 && this.IsFromPool)
             {
@@ -566,7 +590,7 @@ namespace ET
                 return;
             }
 
-            this.RemoveFromComponent(type, c);
+            this.RemoveFromComponents(c);
             c.Dispose();
         }
 
@@ -594,7 +618,7 @@ namespace ET
                 return;
             }
 
-            this.RemoveFromComponent(type, c);
+            this.RemoveFromComponents(c);
             c.Dispose();
         }
 
@@ -611,7 +635,7 @@ namespace ET
                 return;
             }
 
-            RemoveFromComponent(type, c);
+            RemoveFromComponents(c);
             c.Dispose();
         }
 
@@ -673,9 +697,6 @@ namespace ET
             }
 
             component.ComponentParent = this;
-
-            this.AddToComponent(type, component);
-
             return component;
         }
 
@@ -690,9 +711,6 @@ namespace ET
             component.Id = this.Id;
             component.ComponentParent = this;
             EventSystem.Instance.Awake(component);
-
-            this.AddToComponent(type, component);
-
             return component;
         }
 
@@ -708,9 +726,6 @@ namespace ET
             component.Id = this.Id;
             component.ComponentParent = this;
             EventSystem.Instance.Awake(component);
-
-            this.AddToComponent(type, component);
-            
             return component as K;
         }
 
@@ -726,9 +741,6 @@ namespace ET
             component.Id = this.Id;
             component.ComponentParent = this;
             EventSystem.Instance.Awake(component, p1);
-
-            this.AddToComponent(type, component);
-
             return component as K;
         }
 
@@ -744,9 +756,6 @@ namespace ET
             component.Id = this.Id;
             component.ComponentParent = this;
             EventSystem.Instance.Awake(component, p1, p2);
-            
-            this.AddToComponent(type, component);
-
             return component as K;
         }
 
@@ -762,11 +771,14 @@ namespace ET
             component.Id = this.Id;
             component.ComponentParent = this;
             EventSystem.Instance.Awake(component, p1, p2, p3);
-
-            this.AddToComponent(type, component);
-
             return component as K;
         }
+        
+        public Entity AddChild(Entity entity)
+        {
+            entity.Parent = this;
+            return entity;
+        }
 
         public T AddChild<T>(bool isFromPool = false) where T : Entity
         {