Importing common methods

This commit is contained in:
2024-05-04 18:10:45 +01:00
commit 254ceaf815
15 changed files with 507 additions and 0 deletions
@@ -0,0 +1,19 @@
using UnityFlow.DocumentationHelper.Library.Attributes;
namespace GameDevLibrary.Extensions
{
[Documentation("Array Extensions", "Set of extension methods to help with Arrays.")]
public static class ArrayExtensions
{
// Idea obtained from TaroDev's video on things to do in Unity - https://youtu.be/Ic5ux-tpkCE?t=302
[Documentation("Next", @"Method to obtain the next object in the array by passing the current index.
Note: Idea obtained from TaroDev's video on things to do in Unity - https://youtu.be/Ic5ux-tpkCE?t=302", new []
{
"CurrentIndex - The index we're currently on inside the array passed as reference so we can automatically assign to it the next index"
})]
public static T Next<T>(this T[] array, ref int currentIndex)
{
return array[currentIndex++ % array.Length];
}
}
}
@@ -0,0 +1,17 @@
using System;
namespace GameDevLibrary.Extensions
{
public static class FloatExtensions
{
public static float Round(this float value, int digits = 2)
{
return MathF.Round(value, digits);
}
public static int RoundToInt(this float value)
{
return (int)value.Round(0);
}
}
}
@@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace GameDevLibrary.Extensions
{
public static class IListExtensions
{
/// <summary>
/// Swaps the position of 2 elements inside of a List
/// </summary>
/// <param name="list"></param>
/// <param name="indexA"></param>
/// <param name="indexB"></param>
/// <typeparam name="T"></typeparam>
/// <returns>List with swapped elements</returns>
public static IList<T> Swap<T>(this IList<T> list, int indexA, int indexB)
{
(list[indexA], list[indexB]) = (list[indexB], list[indexA]);
return list;
}
/// <summary>
/// Swaps the position of 2 elements inside of a List
/// </summary>
/// <param name="list"></param>
/// <param name="itemA"></param>
/// <param name="itemB"></param>
/// <typeparam name="T"></typeparam>
/// <returns>List with swapped elements</returns>
public static IList<T> Swap<T>(this IList<T> list, T itemA, T itemB)
{
return list.Swap(list.IndexOf(itemA), list.IndexOf(itemB));
}
/// <summary>
/// Replace an element inside a list with a different element
/// </summary>
/// <param name="list"></param>
/// <param name="originalValue"></param>
/// <param name="valueToReplace"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static IList<T> Replace<T>(this IList<T> list, T originalValue, T valueToReplace)
{
var index = list.IndexOf(originalValue);
if (index != -1)
{
list[index] = valueToReplace;
}
return list;
}
/// <summary>
/// Method to obtain a random element from inside a list
/// </summary>
/// <param name="list"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T Random<T>(this IList<T> list)
{
if (list.Count == 0) return default;
// Updated based on Robin King's tip about random items https://twitter.com/quoxel/status/1729137730607841755/photo/1
int seed = (int)DateTime.Now.Ticks;
var r = new Random(seed);
var randomIndex = r.Next(0, list.Count);
var returnValue = list[randomIndex];
return returnValue ?? list[0];
}
/// <summary>
/// Method to obtain <paramref name="count"/> elements from inside a list
/// </summary>
/// <param name="list"></param>
/// <param name="count"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public static List<T> Random<T>(this IList<T> list, int count = 1)
{
if (count <= 0) throw new ArgumentOutOfRangeException(nameof(count));
List<T> values = new List<T>();
for (int i = 0; i < count; i++)
{
values.Add(list.Where(x => !values.Contains(x)).ToList().Random());
}
return values;
}
public static IEnumerable<TSource> DistinctBy<TSource, TKey>
(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
HashSet<TKey> seenKeys = new HashSet<TKey>();
foreach (TSource element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
// Idea obtained from TaroDev's video on things to do in Unity - https://youtu.be/Ic5ux-tpkCE?t=302
public static T Next<T>(this List<T> array, ref int currentIndex)
{
return array[currentIndex++ % array.Count];
}
}
}
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.Linq;
namespace GameDevLibrary.Extensions
{
public static class MathExtensions
{
/// <summary>
/// Calculate the average value based on a list of values
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static int AverageWithNullValidation(this IEnumerable<int> list)
{
return list.Any() ? (int)list.Average() : 0;
}
/// <summary>
/// Calculate the average value based on a list of values
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static double AverageWithNullValidation(this IEnumerable<double> list)
{
return list.Any() ? list.Average() : 0.0;
}
/// <summary>
/// Calculate the average value based on a list of values
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static float AverageWithNullValidation(this IEnumerable<float> list)
{
return list.Any() ? list.Average() : 0.0f;
}
}
}
@@ -0,0 +1,69 @@
using System;
namespace GameDevLibrary.Extensions
{
public static class StringExtensions
{
/// <summary>
/// Cut off a string once it reaches a <paramref name="maxChars"/> amount of characters and add '...' to the end of the string
/// </summary>
/// <param name="value"></param>
/// <param name="maxChars"></param>
/// <returns></returns>
public static string Truncate(this string value, int maxChars)
{
return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "...";
}
/// <summary>
/// Identify if a string is null or empty
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsNullOrEmpty(this string str)
{
return String.IsNullOrEmpty(str);
}
/// <summary>
/// Identify if a string is <b>not</b> null or empty
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsNotNullOrEmpty(this string str)
{
return !str.IsNullOrEmpty();
}
/// <summary>
/// Check if the string matches any of the search parameters
/// </summary>
/// <param name="str"></param>
/// <param name="search"></param>
/// <returns></returns>
public static bool AnyMatch(this string str, params string[] search)
{
foreach (string s in search)
{
if (s.Equals(str, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
return false;
}
/// <summary>
/// Reverses a given string
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string Reverse(this string str)
{
char[] charArray = str.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
}