From 28dd88cf0a45f4d6311b20d372f51987873e5b0f Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 31 May 2022 11:41:29 +0100 Subject: [PATCH] Add Vector2/3Int method parity * Added Vector2/3Int.Deconstruct * Added Vector2/3Int.WithX * Added Vector2/3Int.WithY * Added Vector3Int.WithZ * Added Vector2Int.ToSystemPoint * Added Vector2Int.ToSystemSize --- CHANGELOG.md | 9 ++ .../Assets/Tests/Numerics/Vector2IntTests.cs | 88 +++++++++++++++++++ .../Tests/Numerics/Vector2IntTests.cs.meta | 3 + .../Assets/Tests/Numerics/Vector2Tests.cs | 2 +- .../Assets/Tests/Numerics/Vector3IntTests.cs | 78 ++++++++++++++++ .../Tests/Numerics/Vector3IntTests.cs.meta | 3 + .../src/Numerics/Vector2IntExtensions.cs | 80 +++++++++++++++++ .../src/Numerics/Vector3IntExtensions.cs | 77 ++++++++++++++++ 8 files changed, 339 insertions(+), 1 deletion(-) create mode 100644 X10D.Unity.Tests/Assets/Tests/Numerics/Vector2IntTests.cs create mode 100644 X10D.Unity.Tests/Assets/Tests/Numerics/Vector2IntTests.cs.meta create mode 100644 X10D.Unity.Tests/Assets/Tests/Numerics/Vector3IntTests.cs create mode 100644 X10D.Unity.Tests/Assets/Tests/Numerics/Vector3IntTests.cs.meta create mode 100644 X10D.Unity/src/Numerics/Vector2IntExtensions.cs create mode 100644 X10D.Unity/src/Numerics/Vector3IntExtensions.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 938238d..4ec0c8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,7 +32,16 @@ - X10D.Unity: Added `Vector2.Deconstruct()` - X10D.Unity: Added `Vector2.ToSystemPointF()` - X10D.Unity: Added `Vector2.ToSystemSizeF()` +- X10D.Unity: Added `Vector2Int.Deconstruct()` +- X10D.Unity: Added `Vector2Int.ToSystemPoint()` +- X10D.Unity: Added `Vector2Int.ToSystemSize()` +- X10D.Unity: Added `Vector2Int.WithX()` +- X10D.Unity: Added `Vector2Int.WithY()` - X10D.Unity: Added `Vector3.Deconstruct()` +- X10D.Unity: Added `Vector3Int.Deconstruct()` +- X10D.Unity: Added `Vector3Int.WithX()` +- X10D.Unity: Added `Vector3Int.WithY()` +- X10D.Unity: Added `Vector3Int.WithZ()` - X10D.Unity: Added `Vector4.Deconstruct()` ## [3.1.0] diff --git a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2IntTests.cs b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2IntTests.cs new file mode 100644 index 0000000..eb1b217 --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2IntTests.cs @@ -0,0 +1,88 @@ +using System.Collections; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; +using X10D.Unity.Numerics; +using Random = System.Random; + +namespace X10D.Unity.Tests.Numerics +{ + public class Vector2IntTests + { + [UnityTest] + public IEnumerator Deconstruct_ShouldReturnCorrectValues() + { + var vector = new Vector2Int(1, 2); + (int x, int y) = vector; + + Assert.AreEqual(1, x); + Assert.AreEqual(2, y); + + yield break; + } + + [UnityTest] + public IEnumerator ToSystemPoint_ShouldReturnPoint_WithEquivalentMembers() + { + var random = new Random(); + int x = random.Next(); + int y = random.Next(); + + var vector = new Vector2Int(x, y); + var point = vector.ToSystemPoint(); + + Assert.AreEqual(vector.x, point.X); + Assert.AreEqual(vector.y, point.Y); + + yield break; + } + + [UnityTest] + public IEnumerator ToSystemSize_ShouldReturnSize_WithEquivalentMembers() + { + var random = new Random(); + int x = random.Next(); + int y = random.Next(); + + var vector = new Vector2Int(x, y); + var point = vector.ToSystemSize(); + + Assert.AreEqual(vector.x, point.Width); + Assert.AreEqual(vector.y, point.Height); + + yield break; + } + + [UnityTest] + public IEnumerator WithX_ShouldReturnVectorWithNewX_GivenVector() + { + Assert.AreEqual(Vector2Int.up, Vector2Int.one.WithX(0)); + Assert.AreEqual(Vector2Int.zero, Vector2Int.zero.WithX(0)); + Assert.AreEqual(Vector2Int.zero, Vector2Int.right.WithX(0)); + Assert.AreEqual(Vector2Int.up, Vector2Int.up.WithX(0)); + + Assert.AreEqual(Vector2Int.one, Vector2Int.one.WithX(1)); + Assert.AreEqual(Vector2Int.right, Vector2Int.zero.WithX(1)); + Assert.AreEqual(Vector2Int.right, Vector2Int.right.WithX(1)); + Assert.AreEqual(Vector2Int.one, Vector2Int.up.WithX(1)); + + yield break; + } + + [UnityTest] + public IEnumerator WithY_ShouldReturnVectorWithNewY_GivenVector() + { + Assert.AreEqual(Vector2Int.right, Vector2Int.one.WithY(0)); + Assert.AreEqual(Vector2Int.zero, Vector2Int.zero.WithY(0)); + Assert.AreEqual(Vector2Int.right, Vector2Int.right.WithY(0)); + Assert.AreEqual(Vector2Int.zero, Vector2Int.up.WithY(0)); + + Assert.AreEqual(Vector2Int.one, Vector2Int.one.WithY(1)); + Assert.AreEqual(Vector2Int.up, Vector2Int.zero.WithY(1)); + Assert.AreEqual(Vector2Int.one, Vector2Int.right.WithY(1)); + Assert.AreEqual(Vector2Int.up, Vector2Int.up.WithY(1)); + + yield break; + } + } +} diff --git a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2IntTests.cs.meta b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2IntTests.cs.meta new file mode 100644 index 0000000..7a62c97 --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2IntTests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ad987e96afa849e6b0626ba7d7720f7b +timeCreated: 1653993201 \ No newline at end of file diff --git a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2Tests.cs b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2Tests.cs index 860b593..232a54b 100644 --- a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2Tests.cs +++ b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector2Tests.cs @@ -39,7 +39,7 @@ namespace X10D.Unity.Tests.Numerics } [UnityTest] - public IEnumerator ToSystemSizeF_ShouldReturnPoint_WithEquivalentMembers() + public IEnumerator ToSystemSizeF_ShouldReturnSize_WithEquivalentMembers() { var random = new Random(); float x = random.NextSingle(); diff --git a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector3IntTests.cs b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector3IntTests.cs new file mode 100644 index 0000000..21bf438 --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector3IntTests.cs @@ -0,0 +1,78 @@ +using System.Collections; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; +using X10D.Unity.Numerics; + +namespace X10D.Unity.Tests.Numerics +{ + public class Vector3IntTests + { + [UnityTest] + public IEnumerator Deconstruct_ShouldReturnCorrectValues() + { + var vector = new Vector3Int(1, 2, 3); + (float x, float y, float z) = vector; + + Assert.AreEqual(1, x); + Assert.AreEqual(2, y); + Assert.AreEqual(3, z); + + yield break; + } + + [UnityTest] + public IEnumerator WithX_ShouldReturnVectorWithNewX_GivenVector() + { + Assert.AreEqual(new Vector3Int(0, 1, 1), Vector3Int.one.WithX(0)); + Assert.AreEqual(Vector3Int.zero, Vector3Int.zero.WithX(0)); + Assert.AreEqual(Vector3Int.zero, Vector3Int.right.WithX(0)); + Assert.AreEqual(Vector3Int.up, Vector3Int.up.WithX(0)); + Assert.AreEqual(Vector3Int.forward, Vector3Int.forward.WithX(0)); + + Assert.AreEqual(Vector3Int.one, Vector3Int.one.WithX(1)); + Assert.AreEqual(Vector3Int.right, Vector3Int.zero.WithX(1)); + Assert.AreEqual(Vector3Int.right, Vector3Int.right.WithX(1)); + Assert.AreEqual(new Vector3Int(1, 1, 0), Vector3Int.up.WithX(1)); + Assert.AreEqual(new Vector3Int(1, 0, 1), Vector3Int.forward.WithX(1)); + + yield break; + } + + [UnityTest] + public IEnumerator WithY_ShouldReturnVectorWithNewY_GivenVector() + { + Assert.AreEqual(new Vector3Int(1, 0, 1), Vector3Int.one.WithY(0)); + Assert.AreEqual(Vector3Int.zero, Vector3Int.zero.WithY(0)); + Assert.AreEqual(Vector3Int.right, Vector3Int.right.WithY(0)); + Assert.AreEqual(Vector3Int.zero, Vector3Int.up.WithY(0)); + Assert.AreEqual(Vector3Int.forward, Vector3Int.forward.WithY(0)); + + Assert.AreEqual(Vector3Int.one, Vector3Int.one.WithY(1)); + Assert.AreEqual(Vector3Int.up, Vector3Int.zero.WithY(1)); + Assert.AreEqual(new Vector3Int(1, 1, 0), Vector3Int.right.WithY(1)); + Assert.AreEqual(Vector3Int.up, Vector3Int.up.WithY(1)); + Assert.AreEqual(new Vector3Int(0, 1, 1), Vector3Int.forward.WithY(1)); + + yield break; + } + + [UnityTest] + public IEnumerator WithZ_ShouldReturnVectorWithNewZ_GivenVector() + { + Assert.AreEqual(new Vector3Int(1, 1, 0), Vector3Int.one.WithZ(0)); + Assert.AreEqual(Vector3Int.zero, Vector3Int.zero.WithZ(0)); + Assert.AreEqual(Vector3Int.right, Vector3Int.right.WithZ(0)); + Assert.AreEqual(Vector3Int.up, Vector3Int.up.WithZ(0)); + Assert.AreEqual(Vector3Int.zero, Vector3Int.forward.WithZ(0)); + + Assert.AreEqual(Vector3Int.one, Vector3Int.one.WithZ(1)); + Assert.AreEqual(Vector3Int.forward, Vector3Int.zero.WithZ(1)); + Assert.AreEqual(new Vector3Int(1, 0, 1), Vector3Int.right.WithZ(1)); + Assert.AreEqual(new Vector3Int(0, 1, 1), Vector3Int.up.WithZ(1)); + Assert.AreEqual(Vector3Int.forward, Vector3Int.forward.WithZ(1)); + + yield break; + } + } +} diff --git a/X10D.Unity.Tests/Assets/Tests/Numerics/Vector3IntTests.cs.meta b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector3IntTests.cs.meta new file mode 100644 index 0000000..1959799 --- /dev/null +++ b/X10D.Unity.Tests/Assets/Tests/Numerics/Vector3IntTests.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e474d98fd3ee48159980aaa88040cfb3 +timeCreated: 1653993371 \ No newline at end of file diff --git a/X10D.Unity/src/Numerics/Vector2IntExtensions.cs b/X10D.Unity/src/Numerics/Vector2IntExtensions.cs new file mode 100644 index 0000000..42b4f34 --- /dev/null +++ b/X10D.Unity/src/Numerics/Vector2IntExtensions.cs @@ -0,0 +1,80 @@ +using System.Diagnostics.Contracts; +using System.Drawing; +using System.Runtime.CompilerServices; +using UnityEngine; + +namespace X10D.Unity.Numerics; + +/// +/// Numeric-extensions for . +/// +public static class Vector2IntExtensions +{ + /// + /// Deconstructs the current into its components. + /// + /// The vector to deconstruct. + /// The X component value. + /// The Y component value. + public static void Deconstruct(this Vector2Int vector, out int x, out int y) + { + x = vector.x; + y = vector.y; + } + + /// + /// Converts the current into a . + /// + /// The vector to convert. + /// The resulting . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Point ToSystemPoint(this Vector2Int vector) + { + return new Point(vector.x, vector.y); + } + + /// + /// Converts the current into a . + /// + /// The vector to convert. + /// The resulting . + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Size ToSystemSize(this Vector2Int vector) + { + return new Size(vector.x, vector.y); + } + + /// + /// Returns a vector whose Y component is the same as the specified vector, and whose X component is a new value. + /// + /// The vector to copy. + /// The new X component value. + /// + /// A new instance of whose components is the same as that of + /// , and whose component is . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2Int WithX(this Vector2Int vector, int x) + { + return vector with {x = x}; + } + + /// + /// Returns a vector whose X component is the same as the specified vector, and whose Y component is a new value. + /// + /// The vector to copy. + /// The new Y component value. + /// + /// A new instance of whose components is the same as that of + /// , and whose component is . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2Int WithY(this Vector2Int vector, int y) + { + return vector with {y = y}; + } +} diff --git a/X10D.Unity/src/Numerics/Vector3IntExtensions.cs b/X10D.Unity/src/Numerics/Vector3IntExtensions.cs new file mode 100644 index 0000000..48697aa --- /dev/null +++ b/X10D.Unity/src/Numerics/Vector3IntExtensions.cs @@ -0,0 +1,77 @@ +using System.Diagnostics.Contracts; +using System.Drawing; +using System.Runtime.CompilerServices; +using UnityEngine; + +namespace X10D.Unity.Numerics; + +/// +/// Numeric-extensions for . +/// +public static class Vector3IntExtensions +{ + /// + /// Deconstructs the current into its components. + /// + /// The vector to deconstruct. + /// The X component value. + /// The Y component value. + /// The Z component value. + public static void Deconstruct(this Vector3Int vector, out int x, out int y, out int z) + { + x = vector.x; + y = vector.y; + z = vector.z; + } + + /// + /// Returns a vector whose Y and Z components are the same as the specified vector, and whose X component is a new value. + /// + /// The vector to copy. + /// The new X component value. + /// + /// A new instance of whose and + /// components are the same as that of , and whose component is + /// . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector3Int WithX(this Vector3Int vector, int x) + { + return vector with {x = x}; + } + + /// + /// Returns a vector whose X and Z components are the same as the specified vector, and whose Y component is a new value. + /// + /// The vector to copy. + /// The new Y component value. + /// + /// A new instance of whose and + /// components are the same as that of , and whose component is + /// . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector3Int WithY(this Vector3Int vector, int y) + { + return vector with {y = y}; + } + + /// + /// Returns a vector whose X and Y components are the same as the specified vector, and whose Z component is a new value. + /// + /// The vector to copy. + /// The new Z component value. + /// + /// A new instance of whose and + /// components are the same as that of , and whose component is + /// . + /// + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector3Int WithZ(this Vector3Int vector, int z) + { + return vector with {z = z}; + } +}