26 lines
693 B
C#
26 lines
693 B
C#
using System;
|
|
using Godot;
|
|
|
|
namespace fgj26.Scripts.Helpers;
|
|
|
|
public static class Helpers
|
|
{
|
|
public static Node2D GetClosest(Node2D from, Node2D[] targets)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(from);
|
|
ArgumentNullException.ThrowIfNull(targets);
|
|
if (targets.Length == 0) throw new ArgumentNullException(nameof(targets));
|
|
float dist = float.PositiveInfinity;
|
|
Node2D closest = null;
|
|
foreach (var t in targets)
|
|
{
|
|
var td = t.GlobalPosition.DistanceTo(from.GlobalPosition);
|
|
if (td < dist)
|
|
{
|
|
dist = td;
|
|
closest = t;
|
|
}
|
|
}
|
|
return closest;
|
|
}
|
|
} |