You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.0 KiB
38 lines
1.0 KiB
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class SingleTargetFinder
|
|
{
|
|
HashSet<string> filterTags = new HashSet<string>();
|
|
|
|
public SingleTargetFinder(params string[] filterTags)
|
|
{
|
|
for(int i = 0; i < filterTags.Length; i++)
|
|
{
|
|
this.filterTags.Add(filterTags[i]);
|
|
}
|
|
}
|
|
|
|
public bool TryFindNearest(Vector2 pivot, out CreatureBase result)
|
|
{
|
|
CreatureBase minDistCreature = null;
|
|
float minSqrtDist = float.MaxValue;
|
|
|
|
BattleMgr.LoopAllCreatures((creature) =>
|
|
{
|
|
if (filterTags.Contains(creature.tag))
|
|
{
|
|
float sqrtDist = (pivot - (Vector2)creature.transform.position).sqrMagnitude;
|
|
if (sqrtDist < minSqrtDist)
|
|
{
|
|
minSqrtDist = sqrtDist;
|
|
minDistCreature = creature;
|
|
}
|
|
}
|
|
return true;
|
|
});
|
|
|
|
result = minDistCreature;
|
|
return minDistCreature != null;
|
|
}
|
|
}
|