WXMC/proj/unity/Assets/Scripts/Utils/AreaUtils.cs
2024-12-04 16:18:46 +08:00

57 lines
1.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface IArea
{
public Vector2 GetRandomPoint();
public IEnumerable<PHomeUnit> 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<PHomeUnit> FindUnits(Vector2 center, float radius)
{
var result = Physics2D.OverlapCircleAll(center, radius, UnitMask);
foreach (var item in result)
{
PHomeUnit unitComp = item.GetComponent<PHomeUnit>();
if (unitComp == null)
{
if (item.attachedRigidbody != null)
{
unitComp = item.attachedRigidbody.GetComponent<PHomeUnit>();
}
}
if (unitComp != null && !unitComp.IsDead)
{
yield return unitComp;
}
}
}
}