Jelajahi Sumber

增加EntityRef,方便Entity引用另外的Entity

tanghai 2 tahun lalu
induk
melakukan
ffc27254d5

+ 0 - 10
Unity/Assets/Scripts/Codes/Hotfix/Client/Demo/Session/SessionComponentSystem.cs

@@ -1,10 +0,0 @@
-namespace ET.Client
-{
-	public class SessionComponentDestroySystem: DestroySystem<SessionComponent>
-	{
-		protected override void Destroy(SessionComponent self)
-		{
-			self.Session?.Dispose();
-		}
-	}
-}

+ 0 - 11
Unity/Assets/Scripts/Codes/Hotfix/Client/Demo/Session/SessionComponentSystem.cs.meta

@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: 2a5119adf4d729744980440346d1414e
-MonoImporter:
-  externalObjects: {}
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 3 - 3
Unity/Assets/Scripts/Codes/Model/Client/Demo/Session/SessionComponent.cs

@@ -3,17 +3,17 @@
 	[ComponentOf(typeof(Scene))]
 	public class SessionComponent: Entity, IAwake, IDestroy
 	{
-		private long sessionInstanceId;
+		private EntityRef<Session> session;
 
 		public Session Session
 		{
 			get
 			{
-				return Root.Instance.Get(this.sessionInstanceId) as Session;
+				return this.session;
 			}
 			set
 			{
-				this.sessionInstanceId = value.InstanceId;
+				this.session = value;
 			}
 		}
 	}

+ 3 - 3
Unity/Assets/Scripts/Codes/Model/Server/Demo/Gate/PlayerSessionComponent.cs

@@ -3,17 +3,17 @@
 	[ComponentOf(typeof(Player))]
 	public class PlayerSessionComponent : Entity, IAwake
 	{
-		private long sessionInstanceId;
+		private EntityRef<Session> session;
 
 		public Session Session
 		{
 			get
 			{
-				return Root.Instance.Get(this.sessionInstanceId) as Session;
+				return this.session;
 			}
 			set
 			{
-				this.sessionInstanceId = value.InstanceId;
+				this.session = value;
 			}
 		}
 	}

+ 3 - 3
Unity/Assets/Scripts/Codes/Model/Server/Demo/Gate/SessionPlayerComponent.cs

@@ -3,17 +3,17 @@
 	[ComponentOf(typeof(Session))]
 	public class SessionPlayerComponent : Entity, IAwake, IDestroy
 	{
-		private long playerInstanceId;
+		private EntityRef<Player> player;
 
 		public Player Player
 		{
 			get
 			{
-				return Root.Instance.Get(this.playerInstanceId) as Player;
+				return this.player;
 			}
 			set
 			{
-				this.playerInstanceId = value.InstanceId;
+				this.player = value;
 			}
 		}
 	}

+ 3 - 3
Unity/Assets/Scripts/Codes/Model/Server/Module/AOI/AOIEntity.cs

@@ -10,17 +10,17 @@ namespace ET.Server
 
         public int ViewDistance;
 
-        private long cellInstanceId;
+        private EntityRef<Cell> cell;
 
         public Cell Cell
         {
             get
             {
-                return Root.Instance.Get(this.cellInstanceId) as Cell;
+                return this.cell;
             }
             set
             {
-                this.cellInstanceId = value.InstanceId;
+                this.cell = value;
             }
         }
 

+ 3 - 3
Unity/Assets/Scripts/Codes/Model/Share/LockStep/Room.cs

@@ -10,18 +10,18 @@ namespace ET
         public SceneType SceneType { get; set; } = SceneType.Room;
         public string Name { get; set; }
         
-        private long lsWorldInstanceId;
+        private EntityRef<LSWorld> lsWorld;
         
         public LSWorld LSWorld
         {
             get
             {
-                return Root.Instance.Get(this.lsWorldInstanceId) as LSWorld;
+                return this.lsWorld;
             }
             set
             {
                 this.AddChild(value);
-                this.lsWorldInstanceId = value.InstanceId;
+                this.lsWorld = value;
             }
         }
 

+ 3 - 3
Unity/Assets/Scripts/Codes/Model/Share/Module/Scene/CurrentScenesComponent.cs

@@ -4,17 +4,17 @@
     [ComponentOf(typeof(Scene))]
     public class CurrentScenesComponent: Entity, IAwake
     {
-        private long sceneInstanceId;
+        private EntityRef<Scene> scene;
 
         public Scene Scene
         {
             get
             {
-                return Root.Instance.Get(this.sceneInstanceId) as Scene;
+                return this.scene;
             }
             set
             {
-                this.sceneInstanceId = value.InstanceId;
+                this.scene = value;
             }
         }
     }

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

@@ -23,21 +23,17 @@ namespace ET.Client
 			}
 		}
 
-		private long unitViewInstanceId;
+		private EntityRef<LSUnitView> unitView;
 
 		public LSUnitView MyUnitView
 		{
 			get
 			{
-				return Root.Instance.Get(this.unitViewInstanceId) as LSUnitView;
+				return this.unitView;
 			}
 			set
 			{
-				if (value == null)
-				{
-					return;
-				}
-				this.unitViewInstanceId = value.InstanceId;
+				this.unitView = value;
 			}
 		}
 	}

+ 42 - 0
Unity/Assets/Scripts/Core/Module/EntityRef.cs

@@ -0,0 +1,42 @@
+using System;
+
+namespace ET
+{
+    public readonly struct EntityRef<T> where T: Entity
+    {
+        private readonly long instanceId;
+        private readonly T entity;
+
+        private EntityRef(T t)
+        {
+            this.instanceId = t.InstanceId;
+            this.entity = t;
+        }
+        
+        private T UnWrap
+        {
+            get
+            {
+                if (this.entity == null)
+                {
+                    return null;
+                }
+                if (this.entity.InstanceId != this.instanceId)
+                {
+                    return null;
+                }
+                return this.entity;
+            }
+        }
+        
+        public static implicit operator EntityRef<T>(T t)
+        {
+            return new EntityRef<T>(t);
+        }
+
+        public static implicit operator T(EntityRef<T> v)
+        {
+            return v.UnWrap;
+        }
+    }
+}

+ 3 - 0
Unity/Assets/Scripts/Core/Module/EntityRef.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 2119824a0cfc4b7ab9717faf0884210d
+timeCreated: 1683204652