using System.Collections; using System.Collections.Generic; using UnityEngine; public interface IArea { public Vector2 GetRandomPoint(); public IEnumerable FindUnits(); public bool IsInside(Vector2 point); } public static class AreaUtils { public static LayerMask PlayerMask = LayerMask.GetMask("Player"); public static LayerMask MonsterMask = LayerMask.GetMask("Monster"); public static LayerMask UnitMask { get { return PlayerMask | MonsterMask; } } public static Vector2 GetRandomPoint(Vector2 center, float radius) { float length = UnityEngine.Random.Range(0.0f, radius); float deg = UnityEngine.Random.Range(0.0f, 2.0f * Mathf.PI); Vector2 result = new Vector2() { x = Mathf.Cos(deg) * length, y = Mathf.Sin(deg) * length, }; return result + center; } public static IEnumerable FindUnits(Vector2 center, float radius) { var result = Physics2D.OverlapCircleAll(center, radius, UnitMask); foreach (var item in result) { PHomeUnit unitComp = item.GetComponent(); if (unitComp == null) { if (item.attachedRigidbody != null) { unitComp = item.attachedRigidbody.GetComponent(); } } if (unitComp != null && !unitComp.IsDead) { yield return unitComp; } } } }