This commit is contained in:
2026-01-31 23:24:13 +02:00
parent be0c819c8b
commit 12c1360ae7
58 changed files with 1902 additions and 238 deletions
+26
View File
@@ -0,0 +1,26 @@
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;
}
}
+1
View File
@@ -0,0 +1 @@
uid://ef7d25f50nut
+40
View File
@@ -0,0 +1,40 @@
using System;
public static class RandomHelper
{
// Single Random instance for the whole app
private static readonly Random _rand = new Random();
// Random integer [min, max] inclusive
public static int Int(int min, int max)
{
return _rand.Next(min, max + 1); // max is inclusive
}
// Random float [min, max)
public static float Float(float min, float max)
{
return (float)(_rand.NextDouble() * (max - min) + min);
}
// Random double [min, max)
public static double Double(double min, double max)
{
return _rand.NextDouble() * (max - min) + min;
}
// Random element from an array
public static T Choice<T>(T[] array)
{
if (array == null || array.Length == 0)
throw new ArgumentException("Array cannot be null or empty.");
return array[_rand.Next(array.Length)];
}
// Random enum value
public static T ChoiceEnum<T>() where T : Enum
{
Array values = Enum.GetValues(typeof(T)); // returns System.Array
return (T)values.GetValue(_rand.Next(values.Length)); // use GetValue
}
}
+1
View File
@@ -0,0 +1 @@
uid://dy2yccrqc1ho3