Explorar el Código

增加血条显示支持

大爷 hace 1 año
padre
commit
833d25e278

+ 2 - 2
Unity/Assets/Scripts/Codes/Hotfix/Client/battle/BattleMgr_Cmd.cs

@@ -17,7 +17,7 @@ namespace ET
             var session = PlayerComponent.Instance.ClientScene().GetComponent<SessionComponent>().Session;
             switch (index)
             {
-                case BattleFunc.Index.Func1:
+                case BattleFunc.Index.Func2:
                     var ret = await session.Call(new C2G_AddUnitsToMap()
                     {
                         UnitId = 102,
@@ -26,7 +26,7 @@ namespace ET
                         Y = 30
                     });
 
-                    if(ret.Error != 0)
+                    if (ret.Error != 0)
                     {
                         Log.Error(ret.Message);
                     }

+ 28 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Scene/GameObjectPool.cs

@@ -1,6 +1,7 @@
 using CommonAI.Zone;
 using CommonAI.Zone.ZoneEditor;
 using CommonLang;
+using FairyGUI;
 using Sirenix.Utilities;
 using System.Collections.Generic;
 using System.Text.RegularExpressions;
@@ -39,6 +40,7 @@ namespace ET.Client
 
         private readonly HashMap<string, List<GameObject>> goPool = new();
         private readonly HashMap<string, AudioClip> audioPool = new();
+        private readonly List<GComponent>headBarPool = new();
 
         //TODO: 选取当前场景的单位、技能(事件触发等等)
         //TODO: BUFF
@@ -302,5 +304,31 @@ namespace ET.Client
             audioPool.Add(key, aac);
             return aac;
         }
+
+        public async ETTask<GComponent> AcquireHeadBar()
+        {
+            if(headBarPool.Count > 0)
+            {
+                var ret = headBarPool[0];
+                headBarPool.RemoveAt(0);
+                return ret;
+            }
+
+            var root = GlobalViewComponent.Instance.HeadbarView;
+            if (root == null)
+            {
+                //加入所有headbar的parent view
+                root = await UIHelper.Create("HeadBar", "HeadBarRoot", 0);
+                GlobalViewComponent.Instance.HeadbarView = root;
+            }
+
+            var view = await UIHelper.Create("HeadBar", "normalbar", -1, root);
+            return view;
+        }
+
+        public void RecycleHeadBar(GComponent view)
+        {
+            headBarPool.Add(view);
+        }
     }
 }

+ 5 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/UI/HUD/SceneChangeFinishEvent_CreateHUD.cs

@@ -65,6 +65,11 @@ namespace ET.Client
                 var index = i;
                 btn.onClick.Set(() =>
                 {
+                    if(index == 1)
+                    {
+                        GlobalViewComponent.Instance.HeadbarView.visible = !GlobalViewComponent.Instance.HeadbarView.visible;
+                        return;
+                    }
                     EventSystem.Instance.Publish<BattleFunc>(BattleFunc.Static.Clone(index));
                 });
             }

+ 14 - 2
Unity/Assets/Scripts/Codes/HotfixView/Client/UI/UIHelper.cs

@@ -5,13 +5,25 @@ namespace ET.Client
 {
     public static class UIHelper
     {
-        public static async ETTask<GComponent> Create(string packName, string uiName = null)
+        public static async ETTask<GComponent> Create(string packName, string uiName = null, int index = -1, GComponent parent = null)
         {
             var ass = await YooAssetProxy.LoadAssetAsync<TextAsset>($"FGUI_{packName}_fui");
             UIPackage.AddPackage(ass.GetAssetObject<TextAsset>().bytes, packName, LoadPackageInternalAsync);
             var view = UIPackage.CreateObject(packName, uiName ?? packName).asCom;
             view.name = uiName ?? packName;
-            GRoot.inst.AddChild(view);
+
+            if(parent == null)
+            {
+                parent = GRoot.inst;
+            }
+            if (index >= 0)
+            {
+                parent.AddChildAt(view, index);
+            }
+            else
+            {
+                parent.AddChild(view);
+            }
             return view;
         }
 

+ 26 - 8
Unity/Assets/Scripts/Codes/HotfixView/Client/Unit/OnNewZoneObject.cs

@@ -1,20 +1,22 @@
 using CommonAI.ZoneClient;
+using FairyGUI;
 using Sirenix.Utilities;
 using UnityEngine;
 
 namespace ET.Client
 {
     [Event(SceneType.Current)]
+    [FriendOfAttribute(typeof(ET.Client.UnitRenderComponet))]
     public class OnNewZoneObjectHandler : AEvent<EventType.OnNewZoneObject>
     {
         protected override async ETTask Run(Scene scene, EventType.OnNewZoneObject args)
         {
             var obj = UnitMgr.Instance.GetUnit(args.ObjectId);
-            if(obj is BattleUnit)
+            if (obj is BattleUnit)
             {
                 await CreatUnitModel(obj as BattleUnit);
             }
-            else if(obj is BattleSpell)
+            else if (obj is BattleSpell)
             {
                 //TODO: 性能有问题时,可以减少法术展示
                 await CreateSpellModel(obj as BattleSpell);
@@ -29,27 +31,27 @@ namespace ET.Client
         private async ETTask CreatUnitModel(BattleUnit unit)
         {
             var zu = unit.ZUnit;
-            var go = await GameObjectPool.Instance.Acquire($"Unit_{zu.Info.FileName}");
-            go.SetActive(true);
             if (!UnitMgr.Instance.HasUnit(zu.ObjectID))
             {
                 //还没显示就已挂掉的单位,走好
                 Log.Debug($"ignore dead unit: {zu}@{zu.ObjectID}");
                 return;
             }
-            ModelViewComponent.Instance.AddChildWithId<UnitRenderComponet, GameObject>(unit.Id, go, true);
 
+            var go = await GameObjectPool.Instance.Acquire($"Unit_{zu.Info.FileName}");
+            go.SetActive(true);
             go.transform.parent = GlobalViewComponent.Instance.Unit;
             vecTemp.Set(zu.X, zu.Y, zu.Z);
             go.transform.position = RenderUtils.UnityPosFromBattle(vecTemp);
             go.transform.rotation = RenderUtils.UnityRotationFromBattle(zu.Direction);
 
+            var render = ModelViewComponent.Instance.AddChildWithId<UnitRenderComponet, GameObject>(unit.Id, go, true);
+            CreateHeadbar(render, zu).Coroutine();
             if (unit is BattleActor)
             {
                 CameraMgr.FollowMe(go.transform.position);
             }
-            Log.Debug($"unitRender({zu.ObjectID}),pos({zu.X},{zu.Y},{zu.Z}) ok.");
-
+            //Log.Debug($"unitRender({zu.ObjectID}),pos({zu.X},{zu.Y},{zu.Z}) ok.");
             //TODO: 同步ZoneUnit status
         }
 
@@ -57,7 +59,7 @@ namespace ET.Client
         {
             var zs = spell.ZoneObject as ZoneSpell;
             var res = zs.Info.FileName;
-            if(res.IsNullOrWhitespace())
+            if (res.IsNullOrWhitespace())
             {
                 Log.Debug($"spell({zs.Info.ID}) not config fileName");
                 return;
@@ -87,5 +89,21 @@ namespace ET.Client
                 //TODO:特效不支持播放动画
             }
         }
+
+        private async ETTask CreateHeadbar(UnitRenderComponet render, ZoneUnit zu)
+        {
+            var view = await GameObjectPool.Instance.AcquireHeadBar();
+            view.visible = true;
+
+            render.HeadBar = view;
+            var name = view.GetChild("text_name");
+            var progresshp = view.GetChild("bar_hp") as GProgressBar;
+            progresshp.max = 100;
+            progresshp.min = 0;
+
+            name.text = render.GameObject.name;
+            progresshp.value = zu.HP * 100 / zu.MaxHP;
+            render.SyncHeadBarPos();
+        }
     }
 }

+ 31 - 0
Unity/Assets/Scripts/Codes/HotfixView/Client/Unit/UnitRenderSystem.cs

@@ -5,6 +5,7 @@ using System.Text.RegularExpressions;
 using ET.EventType;
 using Sirenix.Utilities;
 using CommonAI.Zone;
+using FairyGUI;
 
 namespace ET.Client
 {
@@ -83,6 +84,7 @@ namespace ET.Client
                 {
                     CameraMgr.FollowMe(transform.position);
                 }
+                unitRender.SyncHeadBarPos();
             }
             else
             {
@@ -109,6 +111,8 @@ namespace ET.Client
                 {
                     self.ExeCommand(UnitRenderComponet.CMDIdle);
                 }
+
+                self.HeadPartTrans = go.transform.Find("BindPart/headInfo");
             }
         }
 
@@ -140,6 +144,12 @@ namespace ET.Client
                     self.AniData.Animancer.Stop();
                 }
                 GameObjectPool.Instance?.RecycleObject(self.GameObject);
+                if(self.HeadBar != null)
+                {
+                    self.HeadBar.visible = false;
+                    GameObjectPool.Instance?.RecycleHeadBar(self.HeadBar);
+                }
+
                 self.Reset();
             }
         }
@@ -149,6 +159,27 @@ namespace ET.Client
         {
             self.Commands.Add(cmd);
         }
+        public static void SyncHeadBarPos(this UnitRenderComponet self)
+        {
+            if (self.HeadBar != null)
+            {
+                var part = self.HeadPartTrans ?? self.GameObject.transform;
+                var pos = Camera.main.WorldToScreenPoint(part.position);
+                if(pos.z < 0 || pos.x < 0 || pos.x > Screen.width || pos.y < 0 || pos.y > Screen.height)
+                {
+                    //不在视野中
+                    self.HeadBar.visible = false;
+                }
+                else
+                {
+                    var screenpos = pos;
+                    pos.y = Screen.height - pos.y;
+                    pos = GRoot.inst.GlobalToLocal(pos);
+                    self.HeadBar.SetXY(pos.x, pos.y);
+                    self.HeadBar.visible = true;
+                }
+            }
+        }
         //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 

+ 3 - 1
Unity/Assets/Scripts/Codes/ModelView/Client/Global/GlobalViewComponent.cs

@@ -1,4 +1,5 @@
-using UnityEngine;
+using FairyGUI;
+using UnityEngine;
 
 namespace ET.Client
 {
@@ -13,5 +14,6 @@ namespace ET.Client
         public Transform Global;
         public Transform Unit { get; set; }
         public Transform RecycleNode { get; set; }
+        public GComponent HeadbarView { get; set; }
     }
 }

+ 6 - 0
Unity/Assets/Scripts/Codes/ModelView/Client/Unit/UnitRenderComponet.cs

@@ -2,6 +2,8 @@
 using Mono;
 using System.Collections.Generic;
 using AniType = Mono.AnimationData.AnimationType;
+using FairyGUI;
+
 namespace ET.Client
 {
     [ChildOf(typeof(ModelViewComponent))]
@@ -17,6 +19,8 @@ namespace ET.Client
         public GameObject GameObject { get; set; }
         public AnimationData AniData;
         public AnimatorCommand DoingCmd;
+        public GComponent HeadBar;
+        public Transform HeadPartTrans;
 
         //TODO: 获得模型绑定部件,如不存在返回Root
         public Transform GetBindPart(string partName)
@@ -29,6 +33,8 @@ namespace ET.Client
             GameObject = null;
             AniData = null;
             DoingCmd = null;
+            HeadBar = null;
+            HeadPartTrans = null;
         }
     }