// *********************************************************************** // Assembly : Game // Author : Kimch // Created : 2020-09-02 // Description : // Last Modified By : // Last Modified On : // *********************************************************************** // // // *********************************************************************** using System; using UnityEngine; namespace G { /// /// /// public class Cha_Auto : MonoBehaviour { [Flags] public enum AutoType { None, SelectTarget = 0x1, AttackTarget = 0x2, CastSkill = 0x4, All = -1, } #region Field /// /// 自己选择目标 /// public float selectTargetRange = 0.5f; public Transform selectCenter; public Transform selectCircle; /// /// 最近目标 /// private Transform _nearestTarget; /// /// 性能考虑 /// private float _lastUpdateTime; /// /// /// private EntityMainPlayer _scriptCha; /// /// /// private AutoType _autoType; #endregion #region Property public Transform nearestTarget { get => _nearestTarget; } /// /// 自动攻击目标 /// public bool autoAttackTarget { get => _scriptCha.autoAttack; } /// /// 自动施放技能 /// public bool autoCastSkill => _scriptCha.autoCastSkill; #endregion #region Method /// /// /// /// public void StartAuto(AutoType autoType = AutoType.All) { _autoType = autoType; } /// /// /// public void StopAuto() { _autoType = AutoType.None; } private void AutoSelectTarget() { if (selectTargetRange < float.Epsilon) { _nearestTarget = null; return; } var minSM = selectTargetRange * selectTargetRange; var chaP = (selectCenter ?? this.transform).position; //优先选择boss var bosses = EntityManager.Instance.FindAllBossInScene(); if (bosses != null && bosses.Count > 0) { foreach (var boss in bosses) { var tarT = boss.transform; var curSM = Vector3.SqrMagnitude(chaP - tarT.position); if (curSM < minSM) { minSM = curSM; _nearestTarget = tarT; } } } //小怪 var minions = EntityManager.Instance.FindAllMinionInScene(); if (minions != null && minions.Count > 0) { foreach (var minion in minions) { if (minion.gameObject.layer == GameLayer.UnitLayer) { var tarT = minion.transform; var curSM = Vector3.SqrMagnitude(chaP - tarT.position); if (curSM < minSM) { minSM = curSM; _nearestTarget = tarT; } } } } //选择攻击目标 if (_nearestTarget) { _scriptCha.AutoSelectTarget(_nearestTarget); return; } //道具 var props = EntityManager.Instance.FindAllPropInScene(); if (props != null && props.Count > 0) { foreach (var prop in props) { var tarT = prop.transform; var curSM = Vector3.SqrMagnitude(chaP - tarT.position); if (curSM < minSM) { minSM = curSM; _nearestTarget = tarT; } } } //选择道具 if (_nearestTarget) { _scriptCha.AutoSelectProp(_nearestTarget); _nearestTarget = null; return; } } /// /// /// private void AutoAttackTarget() { if (autoAttackTarget) { if (_nearestTarget) _scriptCha.AutoAttackTarget(_nearestTarget); else _scriptCha.AutoFindPortal(); } } /// /// /// private void AutoCastSkill() { if (autoCastSkill && _nearestTarget) { _scriptCha.AutoCastSkill(); } } private void AutoStudySkill() { } private void AutoOpenBox() { } private void AutoFindPortal() { } private void UpdateAuto() { if (GameLevel.Instance.levelFinish) return; if (_autoType.HasFlag(AutoType.SelectTarget)) AutoSelectTarget(); if (_autoType.HasFlag(AutoType.AttackTarget)) { AutoAttackTarget(); AutoFindPortal(); } if (_autoType.HasFlag(AutoType.CastSkill)) { AutoCastSkill(); AutoStudySkill(); AutoOpenBox(); AutoFindPortal(); } } #endregion private void Awake() { _scriptCha = GetComponent(); } private void Update() { if (_autoType == AutoType.None) return; if (Time.time - _lastUpdateTime > 0.05f) { _lastUpdateTime = Time.time; UpdateAuto(); } if (_nearestTarget) { var tarP = _nearestTarget.position; tarP.y = 0.01f; selectCircle.transform.position = tarP; var tarR = _nearestTarget.rotation.eulerAngles; tarR.x = 90f; tarR.z = 0f; selectCircle.transform.eulerAngles = tarR; } else { selectCircle.transform.position = new Vector3(0, -100, 0); } } } }