WinterJamSnowman/Assets/Scripts/LemonGenericLib/LemonCodeUtilities.cs

79 lines
2.6 KiB
C#
Raw Permalink Normal View History

2023-01-24 13:51:46 +00:00
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Security.Cryptography;
//for Iced_Lemon only, do not modify
//TODO: seperate the functionalities in the future
namespace Lemon.GenericLib.Code.Utilities {
public static class LemonCodeUtilities {
//strings
#region StringUtilities
public static bool CompareTextNonCase(string textA, string textB)
{
return textA.ToUpper() == textB.ToUpper();
}
public static string TrimLastChar(string text)
{
return text.Substring(0, text.Length - 1);
}
#endregion
#region RandomUtilities
//randoms
public static float RandomVectorValue(Vector2 vector) { return UnityEngine.Random.Range(vector.x, vector.y); }
public static int RandomVectorValueInt(Vector2Int vector) { return UnityEngine.Random.Range(vector.x, vector.y + 1); }
public static int RandomArrayIndex(object[] array) { return UnityEngine.Random.Range(0, array.Length); }
public static object RandomArrayItem(object[] array) { return array[UnityEngine.Random.Range(0, array.Length)]; }
public static int RandomListIndex(List<object> list) { return UnityEngine.Random.Range(0, list.Count); }
public static object RandomListItem(List<object> list) { return list[UnityEngine.Random.Range(0, list.Count)]; }
public static bool RandomBool() { return UnityEngine.Random.Range(0, 2) > 0; }
public static void Shuffle<T>(this IList<T> list)
{
//RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
//int n = list.Count;
//while (n > 1)
//{
// byte[] box = new byte[1];
// do provider.GetBytes(box);
// while (!(box[0] < n * (Byte.MaxValue / n)));
// int k = (box[0] % n);
// n--;
// T value = list[k];
// list[k] = list[n];
// list[n] = value;
//}
System.Random rng = new System.Random();
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
#endregion
public static string PrintList<T>(List<T> list) {
string str = "[ ";
foreach (var item in list)
{
str += item.ToString() + ", ";
}
return str + " ]";
}
}
}