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

Commit

Permalink
Update Range and add ReactiveProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
uurha committed Jan 29, 2024
1 parent 78ddb7f commit 0e493fe
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Runtime/Properties.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Runtime/Properties/ReactiveProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using UnityEngine;

namespace Better.DataStructures.Properties
{
[Serializable]
public class ReactiveProperty<T>
{
public event Action<T> ValueChangedEvent;

[SerializeField]
protected T _value;

public T Value
{
get => _value;
set
{
_value = value;
SetDirty();
}
}

public ReactiveProperty(T defaultValue = default)
{
_value = defaultValue;
}

public virtual void SetDirty()
{
ValueChangedEvent?.Invoke(Value);
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Properties/ReactiveProperty.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 37 additions & 8 deletions Runtime/Ranges/Range.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,62 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Better.DataStructures.Ranges
{
[Serializable]
public struct Range<T> where T : new()
public class Range<T> : IEquatable<Range<T>>
{
[SerializeField] private T min;
[SerializeField] private T max;

public Range()
{
min = default;
max = default;
}

public Range(T min, T max)
{
this.min = min;
this.max = max;
}

public T Min => min;

public T Max => max;

public Range(T minValue, T maxValue)
public bool Equals(Range<T> other)
{
min = minValue;
max = maxValue;
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return EqualityComparer<T>.Default.Equals(min, other.min) && EqualityComparer<T>.Default.Equals(max, other.max);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Range<T>)obj);
}

public Range<T> UpdateMax(T maxValue)
{
max = maxValue;
return this;
return new Range<T>(min, maxValue);
}

public Range<T> UpdateMin(T minValue)
{
min = minValue;
return this;
return new Range<T>(minValue, max);
}

public override int GetHashCode()
{
unchecked
{
return (EqualityComparer<T>.Default.GetHashCode(min) * 397) ^ EqualityComparer<T>.Default.GetHashCode(max);
}
}
}
}
30 changes: 30 additions & 0 deletions Runtime/Ranges/RangeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@ public static float Clamp(this Range<float> range, float value)
{
return Mathf.Clamp(value, range.Min, range.Max);
}

public static float Random(this Range<float> range)
{
return UnityEngine.Random.Range(range.Min, range.Max);
}

public static int Random(this Range<int> range)
{
return UnityEngine.Random.Range(range.Min, range.Max + 1); // +1 to have inclusive random
}

public static float Lerp(this Range<float> range, float t)
{
return Mathf.Lerp(range.Min, range.Max, t);
}

public static int Lerp(this Range<int> range, float t)
{
return Mathf.RoundToInt(Mathf.Lerp(range.Min, range.Max, t));
}

public static bool Contains(this Range<float> range, float value)
{
return value >= range.Min && value <= range.Max;
}

public static bool Contains(this Range<int> range, int value)
{
return value >= range.Min && value <= range.Max;
}

public static double Clamp(this Range<double> range, double value)
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.uurha.betterdatastructures",
"displayName": "Better Data Structures",
"description": "Collection of useful data structures for Unity",
"version": "0.1.1",
"version": "0.1.2",
"unity": "2018.3",
"author": {
"name": "Better Plugins",
Expand Down

0 comments on commit 0e493fe

Please sign in to comment.