using System; using System.Collections; using System.Collections.Generic; using System.Net; using UnityEngine; using UnityEngine.TextCore.Text; using uTools; public class SearchTrigger : MonoBehaviour { public Character owner; public List trigDemonicList; public List trigTowerList; public List trigPlayerList; public List trigEnemyList; public List trigEnemyTowerList; public List trigBossList; public Dictionary> trigCharacterDic; Dictionary targetTypeDic; public bool needToChange; public float maxLen, minLen; //col大小 private BoxCollider col; private Vector3 origSize; private void Awake() { owner = GetComponentInParent(); trigCharacterDic = new Dictionary> { { TargetType.Demonic, trigDemonicList }, { TargetType.Tower, trigTowerList }, { TargetType.Player, trigPlayerList }, { TargetType.Enemy, trigEnemyList }, { TargetType.EnemyTower, trigEnemyTowerList }, { TargetType.Boss, trigBossList }, }; targetTypeDic = new Dictionary(); for (int i = 0; i < owner.attackController.targetTypes.Count; i++) { if (!targetTypeDic.ContainsKey(owner.attackController.targetTypes[i])) { targetTypeDic.Add(owner.attackController.targetTypes[i], true); } } } private void Start() { if (needToChange) { col = GetComponent(); origSize = col.size; col.size = new Vector3(UnityEngine.Random.Range(minLen, maxLen), origSize.y, origSize.z); } } private void FixedUpdate() { for (int i = 0; i < owner.attackController.targetTypes.Count; i++) { if (trigCharacterDic.ContainsKey(owner.attackController.targetTypes[i])) { List trigList = trigCharacterDic[owner.attackController.targetTypes[i]]; for (int j = 0; j < trigList.Count; j++) { if (trigList[j] == null || !trigList[j].enabled || !trigList[j].gameObject.activeInHierarchy || trigList[j].owner.isDie) { if (trigList[j].owner == owner.targetCharacter) { owner.targetCharacter = null; } trigList.RemoveAt(j); j--; } } } } } private void OnTriggerEnter(Collider other) { BeSearchTrigger trigger = other.GetComponent(); if (trigger) { TargetType otherTargetType = (TargetType)Enum.Parse(typeof(TargetType), trigger.owner.tag); if (HasTargetType(otherTargetType) && trigCharacterDic.ContainsKey(otherTargetType)) { trigCharacterDic[otherTargetType].Add(trigger); } } } private void OnTriggerExit(Collider other) { BeSearchTrigger trigger = other.GetComponent(); if (trigger) { TargetType otherTargetType = (TargetType)Enum.Parse(typeof(TargetType), trigger.owner.tag); if (HasTargetType(otherTargetType) && trigCharacterDic.ContainsKey(otherTargetType)) { if (trigger.owner == owner.targetCharacter) { owner.targetCharacter = null; } trigCharacterDic[otherTargetType].Remove(trigger); } } } public bool HasTargetType(TargetType targetType) { return targetTypeDic.ContainsKey(targetType); } public List GetAllTargets(List targetTypes, bool canHitFly) { List list = new List(); for (int i = 0; i < targetTypes.Count; i++) { if (trigCharacterDic.ContainsKey(targetTypes[i])) { List trigList = trigCharacterDic[targetTypes[i]]; switch (targetTypes[i]) { 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: for (int j = 0; j < trigList.Count; j++) { Character character = trigList[j].owner; if (!character.gameObject.activeInHierarchy || character.isDie) { continue; } if (owner == character) { continue; } Enemy enemy = trigList[j].owner as Enemy; if (enemy.canFly && !canHitFly) { continue; } list.Add(trigList[j].owner); } break; case TargetType.Tower: case TargetType.Player: case TargetType.EnemyTower: case TargetType.Boss: for (int j = 0; j < trigList.Count; j++) { Character character = trigList[j].owner; if (!character.gameObject.activeInHierarchy || character.isDie) { continue; } if (owner == character) { continue; } list.Add(trigList[j].owner); } break; default: break; } } } return list; } public Character GetMinDisTarget(List targetTypes, bool canHitFly) { if (!enabled || !gameObject.activeInHierarchy) { return null; } List list = GetAllTargets(targetTypes, canHitFly); 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 minDisChar; } public Character GetMinHPCharacter(List targetTypes, bool canHitFly) { List list = GetAllTargets(targetTypes, canHitFly); Character minHPChar = null; for (int i = 0; i < list.Count; i++) { Character character = list[i]; if (minHPChar == null || minHPChar.hp > character.hp) { minHPChar = character; } } return minHPChar; } }