Browse Source

支持角色位置同步、相机跟随

大爷 2 years ago
parent
commit
10e4ef8b80

+ 5 - 0
Unity/Assets/Scripts/Codes/Hotfix/Client/EntryEvent2_InitClient.cs

@@ -14,6 +14,11 @@
 
             Game.AddSingleton<BattleUnitFactory>();
             await BattleUnitFactory.Instance.InitAsync();
+
+            //Game.AddSingleton<SoundManager>();
+            //await SoundManager.Instance.InitAsync();
+
+            Game.AddSingleton<SkillMgr>();
         }
     }
 }

+ 42 - 0
Unity/Assets/Scripts/Codes/Hotfix/Client/battle/BattleMgr_Update.cs

@@ -0,0 +1,42 @@
+using CommonLang.Geometry;
+using ET.Client;
+using ET.EventType;
+using System;
+
+namespace ET
+{
+    //战斗中,每一帧要干的活
+    public partial class BattleMgr
+    {
+        public void doUpdate()
+        {
+            SyncUnitPos();
+        }
+
+        private Vector3 VecTmp = new Vector3();
+        private void SyncUnitPos()
+        {
+            if(UnitMgr.Instance != null)
+            {
+                foreach (var obj in UnitMgr.Instance.AllUnits)
+                {
+                    if (obj is BattleUnit unit)
+                    {
+                        var zu = unit.ZUnit;
+                        VecTmp.Set(zu.X, zu.Y, zu.Z);
+                        if (!VecTmp.Equal(unit.LastPos, 0.01f) || MathF.Abs(unit.LastRotation - zu.Direction) > 0.01f)
+                        {
+                            EventSystem.Instance.Publish<SyncUnitPosEvent>(SyncUnitPosEvent.Static.Clone(unit.Id, VecTmp, zu.Direction));
+                            unit.LastPos.Set(VecTmp);
+                            unit.LastRotation = zu.Direction;
+                        }
+                    }
+                    else if (obj is BattleSpell spell)
+                    {
+                        //TODO: spell
+                    }
+                }
+            }
+        }
+    }
+}

+ 1 - 1
Unity/Assets/Scripts/Codes/ModelView/Client/Camera/CameraComponent.cs.meta → Unity/Assets/Scripts/Codes/Hotfix/Client/battle/BattleMgr_Update.cs.meta

@@ -1,5 +1,5 @@
 fileFormatVersion: 2
-guid: a256bd12fa27f344fb931f76d8303e9d
+guid: ab17a1206b4aaff42bced33bcc51ed49
 MonoImporter:
   externalObjects: {}
   serializedVersion: 2

+ 1 - 1
Unity/Assets/Scripts/Codes/Hotfix/Client/battle/BattleResourceMgr.cs

@@ -204,7 +204,7 @@ public class BattleResourceMgr : Singleton<BattleResourceMgr>
             terrain_data = new ZoneManhattanMap(data, Templates.TerrainDefinition);
             path_finder = new HZUnityAstarManhattan(msg.sceneID, terrain_data, true, 0);
             area_gen = new ManhattanMapAreaGenerator(terrain_data.Data);
-            this.Data.Terrain = null;
+            //this.Data.Terrain = null;
             this.IsShareTerrain = false;
             this.IsIgnoreTerrainTouch = false;
         }

+ 11 - 0
Unity/Assets/Scripts/Codes/Hotfix/Client/battle/UnitMgr.cs

@@ -9,7 +9,18 @@ namespace ET.Client
         public static UnitMgr Instance;
 
         public BattleActor Actor { get; set; }
+        public uint ActorId
+        {
+            get
+            {
+                return Actor.Id;
+            }
+        }
         private HashMap<uint, BattleObject> UnitList = new();
+        public HashMap<uint, BattleObject>.ValueCollection AllUnits
+        {
+            get { return UnitList.Values; }
+        }
 
         public BattleObject GetUnit(uint id)
         {

+ 3 - 0
Unity/Assets/Scripts/Codes/Hotfix/Client/battle/unit/BattleUnit.cs

@@ -1,6 +1,7 @@
 using CommonAI.Zone;
 using CommonAI.Zone.Helper;
 using CommonAI.ZoneClient;
+using CommonLang.Geometry;
 using ET;
 using ET.EventType;
 using Sirenix.Utilities;
@@ -11,6 +12,8 @@ public class BattleUnit : BattleObject
 {
     public BattleUnit() { }
     public ZoneUnit ZUnit { get { return ZoneObject as ZoneUnit; } }
+    public Vector3 LastPos = Vector3.Zero;
+    public float LastRotation = 0;
 
     private EventDispatcher<UnitActionStatus, object> actionChangeHandler;
 

+ 0 - 46
Unity/Assets/Scripts/Codes/HotfixView/Client/Camera/CameraComponentSystem.cs

@@ -1,46 +0,0 @@
-using UnityEngine;
-
-namespace ET.Client
-{
-	[FriendOf(typeof(CameraComponent))]
-	public static class CameraComponentSystem
-	{
-		[ObjectSystem]
-		public class CameraComponentAwakeSystem : AwakeSystem<CameraComponent>
-		{
-			protected override void Awake(CameraComponent self)
-			{
-				self.Awake();
-			}
-		}
-
-		[ObjectSystem]
-		public class CameraComponentLateUpdateSystem : LateUpdateSystem<CameraComponent>
-		{
-			protected override void LateUpdate(CameraComponent self)
-			{
-				self.LateUpdate();
-			}
-		}
-
-		private static void Awake(this CameraComponent self)
-		{
-			self.mainCamera = Camera.main;
-		}
-
-		private static void LateUpdate(this CameraComponent self)
-		{
-			if (self.Unit != null)
-			{
-				self.UpdatePosition();
-			}
-		}
-
-        //相机跟随主角
-        private static void UpdatePosition(this CameraComponent self)
-		{
-			Vector3 cameraPos = self.mainCamera.transform.position;
-			self.mainCamera.transform.position = new Vector3(self.Unit.Position.x - 10, cameraPos.y, self.Unit.Position.z - 25);
-		}
-	}
-}

+ 16 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Camera/CameraMgr.cs

@@ -0,0 +1,16 @@
+using UnityEngine;
+
+namespace ET.Client
+{
+	public static class CameraMgr
+	{
+        //相机跟随主角
+        public static void FollowMe(Vector3 pos)
+		{
+            var camera = Camera.main;
+            pos.y = 7;
+            pos.z -= 15;
+            camera.transform.position = pos;
+		}
+	}
+}

+ 0 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Camera/CameraComponentSystem.cs.meta → Unity/Assets/Scripts/Codes/HotfixView/Client/Camera/CameraMgr.cs.meta


+ 27 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/RenderUtils.cs

@@ -0,0 +1,27 @@
+using ET;
+using UnityEngine;
+
+public static class RenderUtils
+{
+    public static int MapHeight
+    {
+        get
+        {
+            var layer = BattleMgr.Instance.Layer;
+            return layer == null ? 0 : layer.Data.Terrain.ZoneData.TotalHeight;
+        }
+    }
+
+    private static UnityEngine.Vector3 vecTemp;
+    public static UnityEngine.Vector3 UnityPosFromBattle(CommonLang.Geometry.Vector3 pos)
+    {
+        vecTemp.Set(pos.X, pos.Z, /*MapHeight -*/ pos.Y);
+        return vecTemp;
+    }
+
+    public static UnityEngine.Quaternion UnityRotationFromBattle(float rotation)
+    {
+        vecTemp.Set(0, rotation * Mathf.Rad2Deg + 90, 0);
+        return Quaternion.Euler(vecTemp);
+    }
+}

+ 11 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/RenderUtils.cs.meta

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

+ 9 - 10
Unity/Assets/Scripts/Codes/HotfixView/Client/Unit/OnNewZoneObject.cs

@@ -18,6 +18,7 @@ namespace ET.Client
             }
         }
 
+        private static CommonLang.Geometry.Vector3 vecTemp = new();
         private async ETTask CreatUnitModel(BattleUnit unit)
         {
             var zu = unit.ZUnit;
@@ -25,21 +26,19 @@ namespace ET.Client
             var handle = await YooAssetProxy.LoadAssetAsync<GameObject>($"Unit_{zu.Info.FileName}");
             var prefab = handle.GetAssetObject<GameObject>();
             GameObject go = UnityEngine.Object.Instantiate(prefab, GlobalViewComponent.Instance.Unit, true);
-            go.transform.position = new Vector3(0, 0, 0);
-            go.transform.rotation = Quaternion.identity;
+            vecTemp.Set(unit.ZUnit.X, unit.ZUnit.Y, unit.ZUnit.Z);
+            go.transform.position = RenderUtils.UnityPosFromBattle(vecTemp);
+            go.transform.rotation = RenderUtils.UnityRotationFromBattle(zu.Direction);
 
-            ModelViewComponent.Instance.AddChildWithId<AnimatorComponent, GameObject>(unit.Id, go, true);
+            ModelViewComponent.Instance.AddChildWithId<UnitRenderComponet, GameObject>(unit.Id, go, true);
 
             if (unit is BattleActor)
             {
-                //相机跟随主角
-                //ModelViewComponent.Instance.AddComponent<CameraComponent>().Unit = unit;
-
-                //固定视角相机
-                var camera = Camera.main;
-                var pos = go.transform.position;
-                camera.transform.position = new Vector3(pos.x, 7, pos.z - 15);
+                CameraMgr.FollowMe(go.transform.position);
             }
+            Log.Debug($"unitRender({zu.ObjectID}) ok.");
+
+            //TODO: 同步ZoneUnit status
         }
     }
 }

+ 0 - 8
Unity/Assets/Scripts/Codes/ModelView/Client/Camera.meta

@@ -1,8 +0,0 @@
-fileFormatVersion: 2
-guid: 505b02cfa371af54f8a31f388b9a589d
-folderAsset: yes
-DefaultImporter:
-  externalObjects: {}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 

+ 0 - 21
Unity/Assets/Scripts/Codes/ModelView/Client/Camera/CameraComponent.cs

@@ -1,21 +0,0 @@
-using UnityEngine;
-
-namespace ET.Client
-{
-    [ComponentOf(typeof(ModelViewComponent))]
-    public class CameraComponent : Entity, IAwake, ILateUpdate
-	{
-		// 战斗摄像机
-		public Camera mainCamera;
-
-		public Unit Unit{ get; set; }
-
-		public Camera MainCamera
-		{
-			get
-			{
-				return this.mainCamera;
-			}
-		}
-	}
-}