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

简化Scene的Parent跟Domain实现

tanghai 2 лет назад
Родитель
Сommit
c3fd68bd11

+ 11 - 6
Unity/Assets/Scripts/Codes/HotfixView/Client/Demo/Camera/CameraComponentSystem.cs

@@ -25,19 +25,24 @@ namespace ET.Client
 
 		private static void Awake(this CameraComponent self)
 		{
-			self.mainCamera = Camera.main;
+			self.Camera = Camera.main;
 		}
 
 		private static void LateUpdate(this CameraComponent self)
 		{
 			// 摄像机每帧更新位置
-			self.UpdatePosition();
+			UnitF unit = self.GetMyUnit();
+			if (unit != null)
+			{
+				Vector3 pos = unit.Position.ToVector();
+				Vector3 cameraPos = self.Transform.position;
+				self.Transform.position = new Vector3(pos.x, cameraPos.y, pos.z - 1);
+			}
 		}
-
-		private static void UpdatePosition(this CameraComponent self)
+		
+		private static UnitF GetMyUnit(this CameraComponent self)
 		{
-			Vector3 cameraPos = self.mainCamera.transform.position;
-			self.mainCamera.transform.position = new Vector3(self.Unit.Position.x, cameraPos.y, self.Unit.Position.z - 1);
+			return self.DomainScene().GetComponent<BattleComponent>().LSScene.Get(self.MyId) as UnitF;
 		}
 	}
 }

+ 2 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/LockStep/LockStepSceneChangeStart_AddComponent.cs

@@ -23,6 +23,8 @@ namespace ET.Client
             currentScene.AddComponent<UnitFViewComponent>();
 
             currentScene.AddComponent<OperaComponent>();
+
+            currentScene.AddComponent<CameraComponent>();
         }
     }
 }

+ 2 - 0
Unity/Assets/Scripts/Codes/Model/Share/Entry.cs

@@ -36,6 +36,8 @@
             
             Game.AddSingleton<NetServices>();
             Game.AddSingleton<Root>();
+            Game.AddSingleton<LSSington>();
+            
             await Game.AddSingleton<ConfigComponent>().LoadAsync();
 
             await EventSystem.Instance.PublishAsync(Root.Instance.Scene, new EventType.EntryEvent1());

+ 1 - 1
Unity/Assets/Scripts/Codes/Model/Share/LockStep/BattleComponent.cs

@@ -13,7 +13,7 @@ namespace ET
             }
             set
             {
-                value.Parent = this;
+                this.AddChild(value);
                 this.sceneInstanceId = value.InstanceId;
             }
         }

+ 1 - 1
Unity/Assets/Scripts/Codes/Model/Share/LockStep/UnitF/UnitF.cs

@@ -4,7 +4,7 @@ using TrueSync;
 namespace ET
 {
     [ChildOf(typeof(UnitFComponent))]
-    public class UnitF: Entity, IAwake, ISerializeToEntity
+    public class UnitF: LSEntity, IAwake, ISerializeToEntity
     {
         public TSVector Position
         {

+ 1 - 1
Unity/Assets/Scripts/Codes/Model/Share/LockStep/UnitF/UnitFComponent.cs

@@ -1,7 +1,7 @@
 namespace ET
 {
 	[ComponentOf(typeof(Scene))]
-	public class UnitFComponent: Entity, IAwake, IDestroy, ISerializeToEntity
+	public class UnitFComponent: LSEntity, IAwake, IDestroy, ISerializeToEntity
 	{
 	}
 }

+ 13 - 13
Unity/Assets/Scripts/Codes/ModelView/Client/Demo/Camera/CameraComponent.cs

@@ -2,30 +2,30 @@
 
 namespace ET.Client
 {
+	[ComponentOf(typeof(Scene))]
 	public class CameraComponent : Entity, IAwake, ILateUpdate
 	{
 		// 战斗摄像机
-		public Camera mainCamera;
+		private Camera camera;
 
-		private long unitInstanceId;
+		public Transform Transform;
 
-		public Unit Unit
+		public long MyId
 		{
-			get
-			{
-				return Root.Instance.Get(this.unitInstanceId) as Unit;
-			}
-			set
-			{
-				this.unitInstanceId = value.InstanceId;
-			}
+			get;
+			set;
 		}
 
-		public Camera MainCamera
+		public Camera Camera
 		{
 			get
 			{
-				return this.mainCamera;
+				return this.camera;
+			}
+			set
+			{
+				this.camera = value;
+				this.Transform = this.camera.transform;
 			}
 		}
 	}

+ 6 - 15
Unity/Assets/Scripts/Core/LockStep/LSScene.cs

@@ -39,13 +39,15 @@ namespace ET
         {
         }
         
-        public LSScene(long id, int zone, SceneType sceneType, string name): base(id, IdGenerater.Instance.GenerateInstanceId(), zone, sceneType, name, null)
+        public LSScene(long id, int zone, SceneType sceneType, string name): base(id, IdGenerater.Instance.GenerateInstanceId(), zone, sceneType, name)
         {
             this.Updater.Parent = this;
             
             Log.Info($"LSScene create: {this.Id} {this.InstanceId}");
         }
-        
+
+        #region AddComponent And AddChild
+
         public new K AddComponent<K>(bool isFromPool = false) where K : LSEntity, IAwake, new()
         {
             return this.AddComponentWithId<K>(this.GetId(), isFromPool);
@@ -85,20 +87,9 @@ namespace ET
         {
             return this.AddChildWithId<T, A, B, C>(this.GetId(), a, b, c, isFromPool);
         }
-        
-        [BsonIgnore]
-        public new Entity Parent
-        {
-            get
-            {
-                return this.parent;
-            }
-            set
-            {
-                base.Parent = value;
-            }
-        }
 
+        #endregion
+        
         private readonly Dictionary<long, LSEntity> allLSEntities = new();
 
         [BsonElement]

+ 27 - 4
Unity/Assets/Scripts/Core/Module/Entity/Entity.cs

@@ -13,6 +13,7 @@ namespace ET
         IsComponent = 1 << 2,
         IsCreated = 1 << 3,
         IsNew = 1 << 4,
+        IsScene = 1 << 5,
     }
 
     public partial class Entity: DisposeObject
@@ -47,6 +48,23 @@ namespace ET
                 }
             }
         }
+        
+        [BsonIgnore]
+        protected bool IsScene
+        {
+            get => (this.status & EntityStatus.IsScene) == EntityStatus.IsScene;
+            set
+            {
+                if (value)
+                {
+                    this.status |= EntityStatus.IsScene;
+                }
+                else
+                {
+                    this.status &= ~EntityStatus.IsScene;
+                }
+            }
+        }
 
         [BsonIgnore]
         protected bool IsRegister
@@ -103,7 +121,7 @@ namespace ET
         }
 
         [BsonIgnore]
-        private bool IsComponent
+        protected bool IsComponent
         {
             get => (this.status & EntityStatus.IsComponent) == EntityStatus.IsComponent;
             set
@@ -197,7 +215,8 @@ namespace ET
                 this.parent = value;
                 this.IsComponent = false;
                 this.parent.AddToChildren(this);
-                this.Domain = this.parent.domain;
+
+                this.Domain = this.IsScene? this : this.parent.domain;
 
 #if ENABLE_VIEW && UNITY_EDITOR
                 this.viewGO.GetComponent<ComponentView>().Component = this;
@@ -294,7 +313,11 @@ namespace ET
 
                 if (preDomain == null)
                 {
-                    this.InstanceId = IdGenerater.Instance.GenerateInstanceId();
+                    if (this.InstanceId == 0)
+                    {
+                        this.InstanceId = IdGenerater.Instance.GenerateInstanceId();
+                    }
+
                     this.IsRegister = true;
 
                     // 反序列化出来的需要设置父子关系
@@ -383,7 +406,7 @@ namespace ET
 
         [BsonElement("C")]
         [BsonIgnoreIfNull]
-        private List<Entity> componentsDB;
+        protected List<Entity> componentsDB;
 
         [BsonIgnore]
         private SortedDictionary<string, Entity> components;

+ 4 - 2
Unity/Assets/Scripts/Core/Module/Entity/EntitySceneFactory.cs

@@ -4,14 +4,16 @@
     {
         public static Scene CreateScene(long id, long instanceId, int zone, SceneType sceneType, string name, Entity parent = null)
         {
-            Scene scene = new Scene(id, instanceId, zone, sceneType, name, parent);
+            Scene scene = new Scene(id, instanceId, zone, sceneType, name);
+            parent?.AddChild(scene);
             return scene;
         }
 
         public static Scene CreateScene(int zone, SceneType sceneType, string name, Entity parent = null)
         {
             long instanceId = IdGenerater.Instance.GenerateInstanceId();
-            Scene scene = new Scene(zone, instanceId, zone, sceneType, name, parent);
+            Scene scene = new Scene(zone, instanceId, zone, sceneType, name);
+            parent?.AddChild(scene);
             return scene;
         }
     }

+ 9 - 38
Unity/Assets/Scripts/Core/Module/Entity/Scene.cs

@@ -1,4 +1,6 @@
-namespace ET
+using System;
+
+namespace ET
 {
     [EnableMethod]
     [ChildOf]
@@ -24,22 +26,7 @@
             
         }
 
-        public Scene(long instanceId, int zone, SceneType sceneType, string name, Entity parent)
-        {
-            this.Id = instanceId;
-            this.InstanceId = instanceId;
-            this.Zone = zone;
-            this.SceneType = sceneType;
-            this.Name = name;
-            this.IsCreated = true;
-            this.IsNew = true;
-            this.Parent = parent;
-            this.Domain = this;
-            this.IsRegister = true;
-            Log.Info($"scene create: {this.SceneType} {this.Name} {this.Id} {this.InstanceId} {this.Zone}");
-        }
-
-        public Scene(long id, long instanceId, int zone, SceneType sceneType, string name, Entity parent)
+        public Scene(long id, long instanceId, int zone, SceneType sceneType, string name)
         {
             this.Id = id;
             this.InstanceId = instanceId;
@@ -48,9 +35,9 @@
             this.Name = name;
             this.IsCreated = true;
             this.IsNew = true;
-            this.Parent = parent;
-            this.Domain = this;
+            this.IsScene = true;
             this.IsRegister = true;
+            this.domain = this;
             Log.Info($"scene create: {this.SceneType} {this.Name} {this.Id} {this.InstanceId} {this.Zone}");
         }
 
@@ -60,28 +47,12 @@
             
             Log.Info($"scene dispose: {this.SceneType} {this.Name} {this.Id} {this.InstanceId} {this.Zone}");
         }
-
-        public new Entity Domain
-        {
-            get => this.domain;
-            private set => this.domain = value;
-        }
-
-        public new Entity Parent
+        
+        protected override string ViewName
         {
             get
             {
-                return this.parent;
-            }
-            protected set
-            {
-                if (value == null)
-                {
-                    return;
-                }
-
-                this.parent = value;
-                this.parent.Children.Add(this.Id, this);
+                return $"{this.GetType().Name} ({this.SceneType})";
             }
         }
     }

+ 1 - 1
Unity/Assets/Scripts/Loader/MonoBehaviour/Init.cs

@@ -32,7 +32,7 @@ namespace ET
 			Game.AddSingleton<EventSystem>();
 			Game.AddSingleton<TimerComponent>();
 			Game.AddSingleton<CoroutineLockComponent>();
-			
+
 			ETTask.ExceptionHandler += Log.Error;
 
 			Game.AddSingleton<CodeLoader>().Start();

+ 1 - 1
Unity/ProjectSettings/ProjectSettings.asset

@@ -841,7 +841,7 @@ PlayerSettings:
   scriptingDefineSymbols:
     Android: NETSTANDARD2_0;UNITY
     Server: NETSTANDARD2_0;UNITY
-    Standalone: NETSTANDARD2_0;UNITY;ENABLE_CODES
+    Standalone: NETSTANDARD2_0;UNITY;ENABLE_CODES;ENABLE_VIEW
     WebGL: NETSTANDARD2_0;UNITY
     iPhone: NETSTANDARD2_0;UNITY
   additionalCompilerArguments: {}