Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion GameKit/Dependencies/GameKit.Dependencies.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "GameKit.Dependencies",
"rootNamespace": "",
"references": [
"GUID:6055be8ebefd69e48b49212b09b47b2f"
"GUID:6055be8ebefd69e48b49212b09b47b2f",
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:2665a8d13d1b3f18800f46e256720795",
"GUID:e0cd26848372d4e5c891c569017e11f1"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down
44 changes: 30 additions & 14 deletions GameKit/Dependencies/Utilities/Dictionaries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ namespace GameKit.Dependencies.Utilities
{
public static class DictionaryFN
{

/// <summary>
/// Uses a hacky way to TryGetValue on a dictionary when using IL2CPP and on mobile.
/// This is to support older devices that don't properly handle IL2CPP builds.
/// </summary>
public static bool TryGetValueIL2CPP<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, out TValue value)
public static bool TryGetValueIL2CPP<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, TKey key, out TValue value)
{
#if ENABLE_IL2CPP && UNITY_IOS || UNITY_ANDROID
if (dict.ContainsKey(key))
Expand All @@ -28,36 +27,53 @@ public static bool TryGetValueIL2CPP<TKey, TValue>(this IDictionary<TKey, TValue
}

/// <summary>
/// Returns values as an allocated list.
/// Returns values as a list.
/// </summary>
/// <returns></returns>
public static List<TValue> ValuesToList<TKey, TValue>(this IDictionary<TKey, TValue> dict)
public static List<TValue> ValuesToList<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, bool useCache)
{
List<TValue> result = new(dict.Count);
dict.ValuesToList(ref result);
List<TValue> result = useCache ? CollectionCaches<TValue>.RetrieveList() : new(dict.Count);

//No need to clear the list since it's already clear.
dict.ValuesToList(ref result, clearLst: false);

return result;
}

/// <summary>
/// Clears a collection and populates it with this dictionaries values.
/// Adds values to a list.
/// </summary>
public static void ValuesToList<TKey, TValue>(this IDictionary<TKey, TValue> dict, ref List<TValue> result)
public static void ValuesToList<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, ref List<TValue> result, bool clearLst)
{
result.Clear();
if (clearLst)
result.Clear();

foreach (TValue item in dict.Values)
result.Add(item);
}

/// <summary>
/// Clears a collection and populates it with this dictionaries keys.
/// Returns keys as a list.
/// </summary>
public static void KeysToList<TKey, TValue>(this IDictionary<TKey, TValue> dict, ref List<TKey> result)
public static List<TKey> KeysToList<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, bool useCache)
{
List<TKey> result = useCache ? CollectionCaches<TKey>.RetrieveList() : new(dict.Count);

//No need to clear the list since it's already clear.
dict.KeysToList(ref result, clearLst: false);

return result;
}

/// <summary>
/// Adds keys to a list.
/// </summary>
public static void KeysToList<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, ref List<TKey> result, bool clearLst)
{
result.Clear();

foreach (TKey item in dict.Keys)
result.Add(item);
}

}

}
105 changes: 55 additions & 50 deletions GameKit/Dependencies/Utilities/Floats.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Runtime.CompilerServices;
using UnityEngine;

namespace GameKit.Dependencies.Utilities
{

public static class Floats
{
/// <summary>
Expand All @@ -14,9 +14,10 @@ public static class Floats
/// <summary>
/// Sets a source float to value if equal to or greater than tolerance.
/// </summary>
/// <param name="source">Float to check against tolerance.</param>
/// <param name="tolerance">Tolerance float must be equal to or greater than to change to value.</param>
/// <param name="value">Value source is set to when breaking tolerance.</param>
/// <param name = "source">Float to check against tolerance.</param>
/// <param name = "tolerance">Tolerance float must be equal to or greater than to change to value.</param>
/// <param name = "value">Value source is set to when breaking tolerance.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float SetIfOverTolerance(this float source, float tolerance, float value)
{
if (source >= tolerance)
Expand All @@ -28,9 +29,10 @@ public static float SetIfOverTolerance(this float source, float tolerance, float
/// <summary>
/// Sets a source float to value if equal to or less than tolerance.
/// </summary>
/// <param name="source">Float to check against tolerance.</param>
/// <param name="tolerance">Tolerance float must be equal to or less than to change to value.</param>
/// <param name="value">Value source is set to when breaking tolerance.</param>
/// <param name = "source">Float to check against tolerance.</param>
/// <param name = "tolerance">Tolerance float must be equal to or less than to change to value.</param>
/// <param name = "value">Value source is set to when breaking tolerance.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float SetIfUnderTolerance(this float source, float tolerance, float value)
{
if (source <= tolerance)
Expand All @@ -43,38 +45,39 @@ public static float SetIfUnderTolerance(this float source, float tolerance, floa
/// Returns how much time is left on an endTime. Returns -1 if no time is left.
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float TimeRemainingValue(this float endTime)
{
float remaining = endTime - Time.time;
//None remaining.
// None remaining.
if (remaining < 0f)
return -1f;

return (endTime - Time.time);
return endTime - Time.time;
}

/// <summary>
/// Returns how much time is left on an endTime. Returns -1 if no time is left.
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int TimeRemainingValue(this float endTime, bool useFloor = true)
{
float remaining = endTime - Time.time;
//None remaining.
// None remaining.
if (remaining < 0f)
return -1;

float result = (endTime - Time.time);
return (useFloor) ? Mathf.FloorToInt(result) : Mathf.CeilToInt(result);
float result = endTime - Time.time;
return useFloor ? Mathf.FloorToInt(result) : Mathf.CeilToInt(result);
}


/// <summary>
/// Returns time remaining as a string using hh:mm:ss.
/// </summary>
/// <param name="value"></param>
/// <param name="segments">Number of places to return. 1 is seconds, 2 is minutes, 3 is hours. If a placement does not exist it is replaced with 00.</param>
/// <param name="emptyOnZero">True to return an empty string when value is 0 or less.</param>
/// <param name = "value"></param>
/// <param name = "segments">Number of places to return. 1 is seconds, 2 is minutes, 3 is hours. If a placement does not exist it is replaced with 00.</param>
/// <param name = "emptyOnZero">True to return an empty string when value is 0 or less.</param>
/// <returns></returns>
public static string TimeRemainingText(this float value, byte segments, bool emptyOnZero = false)
{
Expand All @@ -91,13 +94,13 @@ public static string TimeRemainingText(this float value, byte segments, bool emp
string timeText;
if (segments == 1)
{
seconds += (minutes * 60);
seconds += (hours * 3600);
seconds += minutes * 60;
seconds += hours * 3600;
timeText = string.Format("{0:D2}", seconds);
}
else if (segments == 2)
{
minutes += (hours * 60);
minutes += hours * 60;
timeText = string.Format("{0:D2}:{1:D2}", minutes, seconds);
}
else
Expand All @@ -109,17 +112,17 @@ public static string TimeRemainingText(this float value, byte segments, bool emp
}

/// <summary>
/// Provides a random inclusive int within a given range. Preferred over Unity's Random to eliminate confusion as Unity uses inclusive for floats max, and exclusive for int max.
/// Provides a random inclusive int within a given range. Preferred over Unity's Random to eliminate confusion as Unity uses inclusive for floats max, and exclusive for int max.
/// </summary>
/// <param name="minimum">Inclusive minimum value.</param>
/// <param name="maximum">Inclusive maximum value.</param>
/// <param name = "minimum">Inclusive minimum value.</param>
/// <param name = "maximum">Inclusive maximum value.</param>
/// <returns></returns>
public static float RandomInclusiveRange(float minimum, float maximum)
{
double min = Convert.ToDouble(minimum);
double max = Convert.ToDouble(maximum);

double result = (_random.NextDouble() * (max - min)) + min;
double result = _random.NextDouble() * (max - min) + min;
return Convert.ToSingle(result);
}

Expand All @@ -135,29 +138,31 @@ public static float Random01()
/// <summary>
/// Returns if a target float is within variance of the source float.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="tolerance"></param>
/// <param name = "a"></param>
/// <param name = "b"></param>
/// <param name = "tolerance"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Near(this float a, float b, float tolerance = 0.01f)
{
return (Mathf.Abs(a - b) <= tolerance);
return Mathf.Abs(a - b) <= tolerance;
}

/// <summary>
/// Clamps a float and returns if the float required clamping.
/// </summary>
/// <param name="value"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <param name="clamped"></param>
/// <param name = "value"></param>
/// <param name = "min"></param>
/// <param name = "max"></param>
/// <param name = "clamped"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Clamp(float value, float min, float max, ref bool clamped)
{
clamped = (value < min);
clamped = value < min;
if (clamped)
return min;

clamped = (value > min);
clamped = value > min;
if (clamped)
return max;

Expand All @@ -168,65 +173,65 @@ public static float Clamp(float value, float min, float max, ref bool clamped)
/// <summary>
/// Returns a float after being adjusted by the specified variance.
/// </summary>
/// <param name="source"></param>
/// <param name="variance"></param>
/// <param name = "source"></param>
/// <param name = "variance"></param>
/// <returns></returns>
public static float Variance(this float source, float variance)
{
float pickedVariance = RandomInclusiveRange(1f - variance, 1f + variance);
return (source * pickedVariance);
return source * pickedVariance;
}

/// <summary>
/// Sets a float value to result after being adjusted by the specified variance.
/// </summary>
/// <param name="source"></param>
/// <param name="variance"></param>
/// <param name = "source"></param>
/// <param name = "variance"></param>
/// <returns></returns>
public static void Variance(this float source, float variance, ref float result)
{
float pickedVariance = RandomInclusiveRange(1f - variance, 1f + variance);
result = (source * pickedVariance);
result = source * pickedVariance;
}

/// <summary>
/// Returns negative-one, zero, or postive-one of a value instead of just negative-one or positive-one.
/// </summary>
/// <param name="value">Value to sign.</param>
/// <param name = "value">Value to sign.</param>
/// <returns>Precise sign.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float PreciseSign(float value)
{
if (value == 0f)
return 0f;
else
return (Mathf.Sign(value));
return Mathf.Sign(value);
}

/// <summary>
/// Returns if a float is within a range.
/// </summary>
/// <param name="source">Value of float.</param>
/// <param name="rangeMin">Minimum of range.</param>
/// <param name="rangeMax">Maximum of range.</param>
/// <param name = "source">Value of float.</param>
/// <param name = "rangeMin">Minimum of range.</param>
/// <param name = "rangeMax">Maximum of range.</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool InRange(this float source, float rangeMin, float rangeMax)
{
return (source >= rangeMin && source <= rangeMax);
return source >= rangeMin && source <= rangeMax;
}

/// <summary>
/// Randomly flips a float value.
/// </summary>
/// <param name="value"></param>
/// <param name = "value"></param>
/// <returns></returns>
public static float RandomlyFlip(this float value)
{
if (Ints.RandomInclusiveRange(0, 1) == 0)
return value;
else
return (value *= -1f);
return value *= -1f;
}

}


}
Loading