This commit is contained in:
2022-12-15 23:29:02 +01:00
parent 95220bec08
commit 4f57b57a00
24 changed files with 1695 additions and 81 deletions

View File

@@ -25,8 +25,14 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
public static int RandomInclusive(int from, int to) => Random.Range(from, to + 1);
public static T RandomElement<T>(this List<T> list) => list[Random.Range(0, list.Count)];
#endregion
}
public static class ListExtensions
{
/// <summary>
/// remove a random element from a list and return it.
/// </summary>
public static T PopRandomElement<T>(this List<T> list)
{
var index = Random.Range(0, list.Count);
@@ -35,7 +41,20 @@ namespace EscapeRoomEngine.Engine.Runtime.Utilities
return element;
}
#endregion
public static T RandomElement<T>(this List<T> list) => list[Random.Range(0, list.Count)];
/// <summary>
/// Perform a Fisher-Yates shuffle on a given list, leaving it in a random order.
/// </summary>
public static void Shuffle<T>(this List<T> list)
{
for (var n = list.Count - 1; n > 0; n--)
{
var i = Utilities.RandomInclusive(0, n);
var j = Utilities.RandomInclusive(0, n);
(list[i], list[j]) = (list[j], list[i]);
}
}
}
public static class Vector2IntExtensions