Эх сурвалжийг харах

优化了searchTrigger的性能

WGL 1 сар өмнө
parent
commit
dbe8447778

+ 0 - 12
ActionTowerDefense/Assets/Scripts/Characters/Enemy.cs

@@ -15,18 +15,6 @@ public enum EnemyTag
     Tank
     Tank
 }
 }
 
 
-public enum TargetType
-{
-    None = 0,
-    Demonic = 1,
-    Tower = 2,
-    Player = 3,
-    Enemy = 4,
-    EnemyTower = 5,
-    Boss = 6,
-    Portal = 7,
-}
-
 public enum SearchState
 public enum SearchState
 {
 {
     NoTarget = 0,       //ËÑË÷·¶Î§ÄÚûÓÐÄ¿±ê
     NoTarget = 0,       //ËÑË÷·¶Î§ÄÚûÓÐÄ¿±ê

+ 78 - 103
ActionTowerDefense/Assets/Scripts/Characters/SearchTrigger.cs

@@ -69,21 +69,30 @@ public class SearchTrigger : MonoBehaviour
 
 
     private void FixedUpdate()
     private void FixedUpdate()
     {
     {
-        for (int i = 0; i < owner.attackController.targetTypes.Count; i++)
+        var targetTypes = owner.attackController.targetTypes;
+        for (int i = 0; i < targetTypes.Count; i++)
         {
         {
-            if (trigCharacterDic.ContainsKey(owner.attackController.targetTypes[i]))
+            TargetType targetType = targetTypes[i];
+            
+            if (!trigCharacterDic.TryGetValue(targetType, out var trigList) ||trigList == null || trigList.Count == 0)
+                continue;
+
+            for (int j = trigList.Count - 1; j >= 0; j--)
             {
             {
-                List<BeSearchTrigger> trigList = trigCharacterDic[owner.attackController.targetTypes[i]];
-                for (int j = trigList.Count - 1; j >= 0; j--)
+                BeSearchTrigger trigger = trigList[j];
+                if (trigger == null)
+                {
+                    trigList.RemoveAt(j);
+                    continue;
+                }
+                Character triggerOwner = trigger.owner;
+                if (!trigger.enabled || !trigger.gameObject.activeInHierarchy || triggerOwner.isDie)
                 {
                 {
-                    if (trigList[j] == null || !trigList[j].enabled || !trigList[j].gameObject.activeInHierarchy || trigList[j].owner.isDie)
+                    if (triggerOwner == owner.targetCharacter)
                     {
                     {
-                        if (trigList[j].owner == owner.targetCharacter)
-                        {
-                            owner.targetCharacter = null;
-                        }
-                        trigList.RemoveAt(j);
+                        owner.targetCharacter = null;
                     }
                     }
+                    trigList.RemoveAt(j);
                 }
                 }
             }
             }
         }
         }
@@ -91,35 +100,45 @@ public class SearchTrigger : MonoBehaviour
 
 
     private void OnTriggerEnter(Collider other)
     private void OnTriggerEnter(Collider other)
     {
     {
-        BeSearchTrigger trigger = other.GetComponent<BeSearchTrigger>();
-        if (trigger)
-        {
-            TargetType otherTargetType = (TargetType)Enum.Parse(typeof(TargetType), trigger.owner.tag);
+        if (!other.TryGetComponent(out BeSearchTrigger trigger)) return;
 
 
-            if (HasTargetType(otherTargetType) && trigCharacterDic.ContainsKey(otherTargetType))
-            {
-                trigCharacterDic[otherTargetType].Add(trigger);
-            }
+        if (!TryGetTargetTypeFromTag(trigger.owner.tag, out TargetType otherTargetType)) return;
+
+        if (HasTargetType(otherTargetType) && trigCharacterDic.TryGetValue(otherTargetType, out var triggerList))
+        {
+            triggerList.Add(trigger);
         }
         }
     }
     }
 
 
     private void OnTriggerExit(Collider other)
     private void OnTriggerExit(Collider other)
     {
     {
-        BeSearchTrigger trigger = other.GetComponent<BeSearchTrigger>();
-        if (trigger)
+        if (!other.TryGetComponent(out BeSearchTrigger trigger)) return;
+
+        Character triggerOwner = trigger.owner;
+        if (triggerOwner == null) return;
+
+        bool isCurrentTarget = triggerOwner == owner.targetCharacter;
+
+        if (!TryGetTargetTypeFromTag(triggerOwner.tag, out TargetType otherTargetType)) return;
+
+        if (!HasTargetType(otherTargetType)) return;
+
+        if (trigCharacterDic.TryGetValue(otherTargetType, out var triggerList))
         {
         {
-            TargetType otherTargetType = (TargetType)Enum.Parse(typeof(TargetType), trigger.owner.tag);
-            if (HasTargetType(otherTargetType) && trigCharacterDic.ContainsKey(otherTargetType))
+            triggerList.Remove(trigger);
+
+            if (isCurrentTarget)
             {
             {
-                if (trigger.owner == owner.targetCharacter)
-                {
-                    owner.targetCharacter = null;
-                }
-                trigCharacterDic[otherTargetType].Remove(trigger);
+                owner.targetCharacter = null;
             }
             }
         }
         }
     }
     }
 
 
+    private bool TryGetTargetTypeFromTag(string tag, out TargetType targetType)
+    {
+        return GameManager.TagToTargetTypeMap.TryGetValue(tag, out targetType);
+    }
+
     public bool HasTargetType(TargetType targetType)
     public bool HasTargetType(TargetType targetType)
     {
     {
         return targetTypeDic.ContainsKey(targetType);
         return targetTypeDic.ContainsKey(targetType);
@@ -130,31 +149,12 @@ public class SearchTrigger : MonoBehaviour
         List<Character> list = new List<Character>();
         List<Character> list = new List<Character>();
         for (int i = 0; i < targetTypes.Count; i++)
         for (int i = 0; i < targetTypes.Count; i++)
         {
         {
-            if (trigCharacterDic.ContainsKey(targetTypes[i]))
+            TargetType targetType = targetTypes[i];
+            if (trigCharacterDic.TryGetValue(targetType, out List<BeSearchTrigger> trigList))
             {
             {
-                List<BeSearchTrigger> trigList = trigCharacterDic[targetTypes[i]];
-                switch (targetTypes[i])
+                switch (targetType)
                 {
                 {
                     case TargetType.Demonic:
                     case TargetType.Demonic:
-                        for (int j = 0; j < trigList.Count; j++)
-                        {
-                            Character character = trigList[j].owner;
-                            if (!character.gameObject.activeInHierarchy || character.isDie)
-                            {
-                                continue;
-                            }
-                            if (owner == character)
-                            {
-                                continue;
-                            }
-                            Demonic demonic = trigList[j].owner as Demonic;
-                            if (demonic.canFly && !canHitFly)
-                            {
-                                continue;
-                            }
-                            list.Add(trigList[j].owner);
-                        }
-                        break;
                     case TargetType.Enemy:
                     case TargetType.Enemy:
                         for (int j = 0; j < trigList.Count; j++)
                         for (int j = 0; j < trigList.Count; j++)
                         {
                         {
@@ -167,8 +167,7 @@ public class SearchTrigger : MonoBehaviour
                             {
                             {
                                 continue;
                                 continue;
                             }
                             }
-                            Enemy enemy = trigList[j].owner as Enemy;
-                            if (enemy.canFly && !canHitFly)
+                            if (character.canFly && !canHitFly)
                             {
                             {
                                 continue;
                                 continue;
                             }
                             }
@@ -208,6 +207,20 @@ public class SearchTrigger : MonoBehaviour
             return null;
             return null;
         }
         }
         List<Character> list = GetAllTargets(targetTypes, canHitFly);
         List<Character> list = GetAllTargets(targetTypes, canHitFly);
+
+        Vector3 ownerPosition = owner.transform.position;
+        Vector3 ownerBodyScale = owner.bodyTrans.localScale;        
+        AttackController.AttackMethod am = owner.attackController.curAttackMethod;
+        bool isShootAttack = am.attackType == AttackController.AttackType.Shoot;
+        float upAngle = 0f;
+        float downAngle = 0f;
+
+        if (isShootAttack)
+        {
+            upAngle = am.maxUpAngle;
+            downAngle = am.maxDownAngle;
+        }
+
         switch (searchMode)
         switch (searchMode)
         {
         {
             case SearchMode.MinDistance:
             case SearchMode.MinDistance:
@@ -217,20 +230,17 @@ public class SearchTrigger : MonoBehaviour
                 {
                 {
                     Character character = list[i];
                     Character character = list[i];
                     //判断对象是否在远程单位的射击夹角范围内
                     //判断对象是否在远程单位的射击夹角范围内
-                    AttackController.AttackMethod am = owner.attackController.curAttackMethod;
-                    if (am.attackType == AttackController.AttackType.Shoot)
+                    if (isShootAttack)
                     {
                     {
-                        float up = am.maxUpAngle;
-                        float down = am.maxDownAngle;
                         Vector3 dir = character.beSearchTrigger.transform.position - transform.position;
                         Vector3 dir = character.beSearchTrigger.transform.position - transform.position;
-                        float angle = Vector3.Angle(dir, Vector3.left * owner.bodyTrans.localScale.x);
-                        if ((dir.y > 0 && angle > up) || (dir.y < 0 && angle > down))
+                        float angle = Vector3.Angle(dir, Vector3.left * ownerBodyScale.x);
+                        if ((dir.y > 0 && angle > upAngle) || (dir.y < 0 && angle > downAngle))
                         {
                         {
                             continue;
                             continue;
                         }
                         }
                     }
                     }
                     //判断是否在攻击范围内
                     //判断是否在攻击范围内
-                    float distance = Mathf.Abs(character.transform.position.x - owner.transform.position.x);
+                    float distance = Mathf.Abs(character.transform.position.x - ownerPosition.x);
                     if (minDisChar == null || minDistance == -1 || minDistance > distance)
                     if (minDisChar == null || minDistance == -1 || minDistance > distance)
                     {
                     {
                         minDisChar = character;
                         minDisChar = character;
@@ -245,20 +255,17 @@ public class SearchTrigger : MonoBehaviour
                 {
                 {
                     Character character = list[i];
                     Character character = list[i];
                     //判断对象是否在远程单位的射击夹角范围内
                     //判断对象是否在远程单位的射击夹角范围内
-                    AttackController.AttackMethod am = owner.attackController.curAttackMethod;
-                    if (am.attackType == AttackController.AttackType.Shoot)
+                    if (isShootAttack)
                     {
                     {
-                        float up = am.maxUpAngle;
-                        float down = am.maxDownAngle;
                         Vector3 dir = character.beSearchTrigger.transform.position - transform.position;
                         Vector3 dir = character.beSearchTrigger.transform.position - transform.position;
-                        float angle = Vector3.Angle(dir, Vector3.left * owner.bodyTrans.localScale.x);
-                        if ((dir.y > 0 && angle > up) || (dir.y < 0 && angle > down))
+                        float angle = Vector3.Angle(dir, Vector3.left * ownerBodyScale.x);
+                        if ((dir.y > 0 && angle > upAngle) || (dir.y < 0 && angle > downAngle))
                         {
                         {
                             continue;
                             continue;
                         }
                         }
                     }
                     }
                     //判断是否在攻击范围内
                     //判断是否在攻击范围内
-                    float distance = Mathf.Abs(character.transform.position.x - owner.transform.position.x);
+                    float distance = Mathf.Abs(character.transform.position.x - ownerPosition.x);
                     if (maxDisChar == null || maxDistance == -1 || maxDistance < distance)
                     if (maxDisChar == null || maxDistance == -1 || maxDistance < distance)
                     {
                     {
                         maxDisChar = character;
                         maxDisChar = character;
@@ -273,14 +280,11 @@ public class SearchTrigger : MonoBehaviour
                 {
                 {
                     Character character = list[i];
                     Character character = list[i];
                     //判断对象是否在远程单位的射击夹角范围内
                     //判断对象是否在远程单位的射击夹角范围内
-                    AttackController.AttackMethod am = owner.attackController.curAttackMethod;
-                    if (am.attackType == AttackController.AttackType.Shoot)
+                    if (isShootAttack)
                     {
                     {
-                        float up = am.maxUpAngle;
-                        float down = am.maxDownAngle;
                         Vector3 dir = character.beSearchTrigger.transform.position - transform.position;
                         Vector3 dir = character.beSearchTrigger.transform.position - transform.position;
-                        float angle = Vector3.Angle(dir, Vector3.left * owner.bodyTrans.localScale.x);
-                        if ((dir.y > 0 && angle > up) || (dir.y < 0 && angle > down))
+                        float angle = Vector3.Angle(dir, Vector3.left * ownerBodyScale.x);
+                        if ((dir.y > 0 && angle > upAngle) || (dir.y < 0 && angle > downAngle))
                         {
                         {
                             continue;
                             continue;
                         }
                         }
@@ -297,14 +301,11 @@ public class SearchTrigger : MonoBehaviour
                 {
                 {
                     Character character = list[i];
                     Character character = list[i];
                     //判断对象是否在远程单位的射击夹角范围内
                     //判断对象是否在远程单位的射击夹角范围内
-                    AttackController.AttackMethod am = owner.attackController.curAttackMethod;
-                    if (am.attackType == AttackController.AttackType.Shoot)
+                    if (isShootAttack)
                     {
                     {
-                        float up = am.maxUpAngle;
-                        float down = am.maxDownAngle;
                         Vector3 dir = character.beSearchTrigger.transform.position - transform.position;
                         Vector3 dir = character.beSearchTrigger.transform.position - transform.position;
-                        float angle = Vector3.Angle(dir, Vector3.left * owner.bodyTrans.localScale.x);
-                        if ((dir.y > 0 && angle > up) || (dir.y < 0 && angle > down))
+                        float angle = Vector3.Angle(dir, Vector3.left * ownerBodyScale.x);
+                        if ((dir.y > 0 && angle > upAngle) || (dir.y < 0 && angle > downAngle))
                         {
                         {
                             continue;
                             continue;
                         }
                         }
@@ -318,32 +319,6 @@ public class SearchTrigger : MonoBehaviour
             case SearchMode.ByType:
             case SearchMode.ByType:
                 break;
                 break;
         }
         }
-        //Character minDisChar = null;
-        //float minDistance = -1;
-        //for (int i = 0; i < list.Count; i++)
-        //{
-        //    Character character = list[i];
-        //    //判断对象是否在远程单位的射击夹角范围内
-        //    AttackController.AttackMethod am = owner.attackController.curAttackMethod;
-        //    if (am.attackType == AttackController.AttackType.Shoot)
-        //    {
-        //        float up = am.maxUpAngle;
-        //        float down = am.maxDownAngle;
-        //        Vector3 dir = character.beSearchTrigger.transform.position - transform.position;
-        //        float angle = Vector3.Angle(dir, Vector3.left * owner.bodyTrans.localScale.x);
-        //        if ((dir.y > 0 && angle > up) || (dir.y < 0 && angle > down))
-        //        {
-        //            continue;
-        //        }
-        //    }
-        //    //判断是否在攻击范围内
-        //    float distance = Mathf.Abs(character.transform.position.x - owner.transform.position.x);
-        //    if (minDisChar == null || minDistance == -1 || minDistance > distance)
-        //    {
-        //        minDisChar = character;
-        //        minDistance = distance;
-        //    }
-        //}
         return null;
         return null;
     }
     }
 
 

+ 23 - 0
ActionTowerDefense/Assets/Scripts/GameManager.cs

@@ -1,6 +1,7 @@
 using cfg;
 using cfg;
 using SimpleJSON;
 using SimpleJSON;
 using Sirenix.OdinInspector;
 using Sirenix.OdinInspector;
+using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.IO;
 using System.IO;
 using TMPro;
 using TMPro;
@@ -33,6 +34,18 @@ public enum GameType
     GameEnd,
     GameEnd,
     Shop,
     Shop,
 }
 }
+public enum TargetType
+{
+    None = 0,
+    Demonic = 1,
+    Tower = 2,
+    Player = 3,
+    Enemy = 4,
+    EnemyTower = 5,
+    Boss = 6,
+    Portal = 7,
+}
+
 public class GameManager : MonoBehaviour
 public class GameManager : MonoBehaviour
 {
 {
     public static GameManager instance;
     public static GameManager instance;
@@ -60,6 +73,8 @@ public class GameManager : MonoBehaviour
     [Header("商店刷新")]
     [Header("商店刷新")]
     [ValidateInput("CheckTagWeight","标签池子概率总和不是100%")]
     [ValidateInput("CheckTagWeight","标签池子概率总和不是100%")]
     [FoldoutGroup("Rogue")] [LabelText("标签池子比重(%)")] [Tooltip("最高标签池;标签池;公共池")] public List<float> tagWeight;
     [FoldoutGroup("Rogue")] [LabelText("标签池子比重(%)")] [Tooltip("最高标签池;标签池;公共池")] public List<float> tagWeight;
+
+    public static readonly Dictionary<string, TargetType> TagToTargetTypeMap = new Dictionary<string, TargetType>(StringComparer.Ordinal);
     public bool CheckTagWeight(List<float> weights)
     public bool CheckTagWeight(List<float> weights)
     {
     {
         float num = 0;
         float num = 0;
@@ -183,6 +198,14 @@ public class GameManager : MonoBehaviour
         }
         }
         maxTreasuresTag = -1;
         maxTreasuresTag = -1;
         nowLevel = 1;
         nowLevel = 1;
+        foreach (TargetType type in Enum.GetValues(typeof(TargetType)))
+        {
+            string tagName = type.ToString();
+            if (!string.IsNullOrEmpty(tagName))
+            {
+                TagToTargetTypeMap[tagName] = type;
+            }
+        }
     }
     }
 
 
     private void Start()
     private void Start()