Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Clean Up
Browse files Browse the repository at this point in the history
  • Loading branch information
uurha committed Feb 28, 2024
1 parent c7710b5 commit 2aba9c1
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public sealed class ReadOnlyReactiveProperty<T>
/// <param name="source">The ReactiveProperty to encapsulate in a read-only manner.</param>
public ReadOnlyReactiveProperty(ReactiveProperty<T> source)
{
if (source == null)
{
throw new ArgumentException(nameof(source));
}

_source = source;
}

Expand All @@ -43,4 +48,4 @@ public ReadOnlyReactiveProperty(ReactiveProperty<T> source)
/// <param name="action">The callback action to unsubscribe.</param>
public void Unsubscribe(Action<T> action) => _source.Unsubscribe(action);
}
}
}
120 changes: 90 additions & 30 deletions Assets/BetterDataStructures/Runtime/Ranges/RangeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,99 +11,153 @@ public static class RangeExtensions
/// <summary>
/// Clamps an integer value to the range defined by the Range object.
/// </summary>
/// <param name="range">The Range object.</param>
/// <param name="self">The Range object.</param>
/// <param name="value">The value to clamp.</param>
/// <returns>The clamped value.</returns>
public static int Clamp(this Range<int> range, int value)
public static int Clamp(this Range<int> self, int value)
{
return Mathf.Clamp(value, range.Min, range.Max);
if (self == null)
{
Debug.LogException(new ArgumentException(nameof(self)));
return value;
}

return Mathf.Clamp(value, self.Min, self.Max);
}

/// <summary>
/// Clamps a float value to the range defined by the Range object.
/// </summary>
/// <param name="range">The Range object.</param>
/// <param name="self">The Range object.</param>
/// <param name="value">The value to clamp.</param>
/// <returns>The clamped value.</returns>
public static float Clamp(this Range<float> range, float value)
public static float Clamp(this Range<float> self, float value)
{
return Mathf.Clamp(value, range.Min, range.Max);
if (self == null)
{
Debug.LogException(new ArgumentException(nameof(self)));
return value;
}

return Mathf.Clamp(value, self.Min, self.Max);
}

/// <summary>
/// Returns a random float value within the range defined by the Range object.
/// </summary>
/// <param name="range">The Range object.</param>
/// <param name="self">The Range object.</param>
/// <returns>A random float value within the range.</returns>
public static float Random(this Range<float> range)
public static float Random(this Range<float> self)
{
return UnityEngine.Random.Range(range.Min, range.Max);
if (self == null)
{
Debug.LogException(new ArgumentException(nameof(self)));
return default;
}

return UnityEngine.Random.Range(self.Min, self.Max);
}

/// <summary>
/// Returns a random integer value within the range defined by the Range object.
/// </summary>
/// <param name="range">The Range object.</param>
/// <param name="self">The Range object.</param>
/// <returns>A random integer value within the range.</returns>
public static int Random(this Range<int> range)
public static int Random(this Range<int> self)
{
return UnityEngine.Random.Range(range.Min, range.Max + 1); // +1 to include the maximum value in the range.
if (self == null)
{
Debug.LogException(new ArgumentException(nameof(self)));
return default;
}

return UnityEngine.Random.Range(self.Min, self.Max + 1); // +1 to include the maximum value in the range.
}

/// <summary>
/// Linearly interpolates between the minimum and maximum values of the range based on the given interpolation parameter.
/// </summary>
/// <param name="range">The Range object.</param>
/// <param name="self">The Range object.</param>
/// <param name="t">The interpolation parameter.</param>
/// <returns>The interpolated float value.</returns>
public static float Lerp(this Range<float> range, float t)
public static float Lerp(this Range<float> self, float t)
{
return Mathf.Lerp(range.Min, range.Max, t);
if (self == null)
{
Debug.LogException(new ArgumentException(nameof(self)));
return default;
}

return Mathf.Lerp(self.Min, self.Max, t);
}

/// <summary>
/// Linearly interpolates between the minimum and maximum values of the range based on the given interpolation parameter and rounds the result to the nearest integer.
/// </summary>
/// <param name="range">The Range object.</param>
/// <param name="self">The Range object.</param>
/// <param name="t">The interpolation parameter.</param>
/// <returns>The interpolated and rounded integer value.</returns>
public static int Lerp(this Range<int> range, float t)
public static int Lerp(this Range<int> self, float t)
{
return Mathf.RoundToInt(Mathf.Lerp(range.Min, range.Max, t));
if (self == null)
{
Debug.LogException(new ArgumentException(nameof(self)));
return default;
}

return Mathf.RoundToInt(Mathf.Lerp(self.Min, self.Max, t));
}

/// <summary>
/// Checks if a float value is contained within the range defined by the Range object.
/// </summary>
/// <param name="range">The Range object.</param>
/// <param name="self">The Range object.</param>
/// <param name="value">The value to check.</param>
/// <returns>True if the value is within the range; otherwise, false.</returns>
public static bool Contains(this Range<float> range, float value)
public static bool Contains(this Range<float> self, float value)
{
return value >= range.Min && value <= range.Max;
if (self == null)
{
Debug.LogException(new ArgumentException(nameof(self)));
return default;
}

return value >= self.Min && value <= self.Max;
}

/// <summary>
/// Checks if an integer value is contained within the range defined by the Range object.
/// </summary>
/// <param name="range">The Range object.</param>
/// <param name="self">The Range object.</param>
/// <param name="value">The value to check.</param>
/// <returns>True if the value is within the range; otherwise, false.</returns>
public static bool Contains(this Range<int> range, int value)
public static bool Contains(this Range<int> self, int value)
{
return value >= range.Min && value <= range.Max;
if (self == null)
{
Debug.LogException(new ArgumentException(nameof(self)));
return default;
}

return value >= self.Min && value <= self.Max;
}

/// <summary>
/// Clamps a double value to the range defined by the Range object. Uses Math.Clamp if available.
/// </summary>
/// <param name="range">The Range object.</param>
/// <param name="self">The Range object.</param>
/// <param name="value">The value to clamp.</param>
/// <returns>The clamped value.</returns>
public static double Clamp(this Range<double> range, double value)
public static double Clamp(this Range<double> self, double value)
{
if (self == null)
{
Debug.LogException(new ArgumentException(nameof(self)));
return default;
}

#if UNITY_2021_3_OR_NEWER
return Math.Clamp(value, range.Min, range.Max);
return Math.Clamp(value, self.Min, self.Max);
#else
if (value < range.Min)
value = range.Min;
Expand All @@ -116,13 +170,19 @@ public static double Clamp(this Range<double> range, double value)
/// <summary>
/// Clamps a byte value to the range defined by the Range object. Uses Math.Clamp if available.
/// </summary>
/// <param name="range">The Range object.</param>
/// <param name="self">The Range object.</param>
/// <param name="value">The value to clamp.</param>
/// <returns>The clamped value.</returns>
public static byte Clamp(this Range<byte> range, byte value)
public static byte Clamp(this Range<byte> self, byte value)
{
if (self == null)
{
Debug.LogException(new ArgumentException(nameof(self)));
return default;
}

#if UNITY_2021_3_OR_NEWER
return Math.Clamp(value, range.Min, range.Max);
return Math.Clamp(value, self.Min, self.Max);
#else
if (value < range.Min)
value = range.Min;
Expand Down
11 changes: 9 additions & 2 deletions Assets/BetterDataStructures/Runtime/Tree/TreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using UnityEngine;

namespace Better.DataStructures.Tree
{
Expand All @@ -13,7 +14,7 @@ public class TreeNode<T>
{
// Holds the value of the node
private protected readonly T _value;

// List to hold child nodes
private readonly List<TreeNode<T>> _children = new List<TreeNode<T>>();

Expand Down Expand Up @@ -83,6 +84,12 @@ public TreeNode<T>[] AddChildren(params T[] values)
/// <returns>true if the node was successfully removed; otherwise, false.</returns>
public bool RemoveChild(TreeNode<T> node)
{
if (node == null)
{
Debug.LogException(new ArgumentException(nameof(node)));
return default;
}

return _children.Remove(node);
}

Expand All @@ -106,4 +113,4 @@ public IEnumerable<T> Flatten()
return new[] { Value }.Concat(_children.SelectMany(x => x.Flatten()));
}
}
}
}
5 changes: 4 additions & 1 deletion Assets/BetterDataStructures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
"name": "com.uurha.betterdatastructures",
"displayName": "Better Data Structures",
"description": "Collection of useful data structures for Unity",
"version": "0.1.5",
"version": "0.1.6",
"unity": "2021.3",
"dependencies": {
"com.tdw.better.internal.core": "0.0.2"
},
"author": {
"name": "Better Plugins",
"url": "https://github.com/techno-dwarf-works/"
Expand Down

0 comments on commit 2aba9c1

Please sign in to comment.