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.
59 lines
1.5 KiB
59 lines
1.5 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlTypes;
|
|
using System.Linq;
|
|
using System.Xml.Schema;
|
|
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Util;
|
|
|
|
public static class LinqExtras
|
|
{
|
|
public static void ForEach<T>(this IEnumerable<T> self, Action<T> action)
|
|
{
|
|
foreach (var t in self)
|
|
{
|
|
action.Invoke(t);
|
|
}
|
|
}
|
|
|
|
public static void ForEach<T>(this IEnumerable<T> self, Action<T, int> action)
|
|
{
|
|
var i = 0;
|
|
foreach (var t in self)
|
|
{
|
|
action.Invoke(t, i);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
public static T? Random<T>(this IEnumerable<T> self)
|
|
{
|
|
var selfList = self.ToList();
|
|
if (selfList.Count == 0) return default;
|
|
if (selfList.Count == 1) return selfList[0];
|
|
var randomIndex = new Random().Next(0, selfList.Count);
|
|
return selfList[randomIndex];
|
|
}
|
|
|
|
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> self)
|
|
{
|
|
var selfList = self.ToList();
|
|
var random = new Random();
|
|
for (var i = 0; i < selfList.Count; i++)
|
|
{
|
|
var j = random.Next(i, selfList.Count);
|
|
(selfList[i], selfList[j]) = (selfList[j], selfList[i]);
|
|
}
|
|
return selfList;
|
|
}
|
|
|
|
public static Color RandomHue(this Color color)
|
|
{
|
|
color.ToHsv(out _, out float saturation, out var value );
|
|
var rng = new RandomNumberGenerator();
|
|
return Color.FromHsv(rng.Randf(), saturation, value);
|
|
}
|
|
}
|