using System.Linq; using System.Collections; using System.Collections.Generic; using UnityEngine; public class PHomeFirer_Common : PHomeFirer { [SerializeField] private float m_radius; [SerializeField] protected ShootType m_shootType; public override bool CanFire() { if (NeedTargetUnit(m_shootType)) { var targets = GetTargetUnits(); if (targets.Any()) { //units = new List(targets); return true; } return false; } return true; } public override IEnumerator Fire(int count) { List targets = null; float interval = 1 / m_shootRate; for (int j = 0; j < count; j++) { switch (m_shootType) { case ShootType.NearestTarget: { targets = new List(GetTargetUnits()); if (targets.Count == 0) { yield break; } PHomeUnit target; if (targets.Count == 1) { target = targets[0]; } else { target = targets.Aggregate((a, b) => { float distA = Vector2.Distance(a.transform.position, transform.position); float distB = Vector2.Distance(b.transform.position, transform.position); return distA < distB ? a : b; }); } if (target == null) { break; } ShootData shootData = GetShootData(); shootData.SetTargetUnitWithPos(target); ShootProjectiles(shootData); } break; case ShootType.NearbyRandomPoint: { Vector2 dest = AreaUtils.GetRandomPoint(transform.position, m_radius); ShootData shootData = GetShootData(); shootData.EndPoint = dest; ShootProjectiles(shootData); } break; case ShootType.NearbyRandomTarget: { targets = new List(GetTargetUnits()); if (targets.Count == 0) { yield break; } var target = targets[UnityEngine.Random.Range(0, targets.Count)]; ShootData shootData = GetShootData(); shootData.SetTargetUnitWithPos(target); ShootProjectiles(shootData); } break; case ShootType.NearbyAllTarget: break; case ShootType.ScreenRandomPoint: { Vector2 dest = PHomeGameMode.main.PlayerUnit.PlayerScreen.GetRandomPoint(); ShootData shootData = GetShootData(); shootData.EndPoint = dest; ShootProjectiles(shootData); } break; case ShootType.UnitPos: break; case ShootType.None: break; default: break; } yield return new WaitForSeconds(interval); } } private bool NeedTargetUnit(ShootType type) { switch (type) { case ShootType.NearestTarget: case ShootType.NearbyRandomTarget: case ShootType.NearbyAllTarget: return true; case ShootType.NearbyRandomPoint: case ShootType.ScreenRandomPoint: case ShootType.UnitPos: case ShootType.None: return false; default: return default; } } private IEnumerable GetTargetUnits() { var units = AreaUtils.FindUnits(transform.position, m_radius); return units.Where(unit => PHomeGameMode.IsEnemy(Unit, unit)); } #region Debug private void OnDrawGizmosSelected() { switch (m_shootType) { case ShootType.NearestTarget: Gizmos.color = Color.red; GizmosUtils.DrawCircle(transform.position, m_radius); break; case ShootType.NearbyRandomPoint: break; case ShootType.NearbyRandomTarget: break; case ShootType.NearbyAllTarget: break; case ShootType.ScreenRandomPoint: break; case ShootType.UnitPos: break; case ShootType.None: break; default: break; } } #endregion }