mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 19:18:46 +00:00
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
This commit is contained in:
parent
f35f398d7f
commit
28dd88cf0a
@ -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]
|
||||
|
88
X10D.Unity.Tests/Assets/Tests/Numerics/Vector2IntTests.cs
Normal file
88
X10D.Unity.Tests/Assets/Tests/Numerics/Vector2IntTests.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ad987e96afa849e6b0626ba7d7720f7b
|
||||
timeCreated: 1653993201
|
@ -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();
|
||||
|
78
X10D.Unity.Tests/Assets/Tests/Numerics/Vector3IntTests.cs
Normal file
78
X10D.Unity.Tests/Assets/Tests/Numerics/Vector3IntTests.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e474d98fd3ee48159980aaa88040cfb3
|
||||
timeCreated: 1653993371
|
80
X10D.Unity/src/Numerics/Vector2IntExtensions.cs
Normal file
80
X10D.Unity/src/Numerics/Vector2IntExtensions.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace X10D.Unity.Numerics;
|
||||
|
||||
/// <summary>
|
||||
/// Numeric-extensions for <see cref="Vector2Int" />.
|
||||
/// </summary>
|
||||
public static class Vector2IntExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Deconstructs the current <see cref="Vector2" /> into its components.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to deconstruct.</param>
|
||||
/// <param name="x">The X component value.</param>
|
||||
/// <param name="y">The Y component value.</param>
|
||||
public static void Deconstruct(this Vector2Int vector, out int x, out int y)
|
||||
{
|
||||
x = vector.x;
|
||||
y = vector.y;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the current <see cref="Vector2Int" /> into a <see cref="Point" />.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to convert.</param>
|
||||
/// <returns>The resulting <see cref="Point" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Point ToSystemPoint(this Vector2Int vector)
|
||||
{
|
||||
return new Point(vector.x, vector.y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the current <see cref="Vector2Int" /> into a <see cref="Size" />.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to convert.</param>
|
||||
/// <returns>The resulting <see cref="Size" />.</returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Size ToSystemSize(this Vector2Int vector)
|
||||
{
|
||||
return new Size(vector.x, vector.y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a vector whose Y component is the same as the specified vector, and whose X component is a new value.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to copy.</param>
|
||||
/// <param name="x">The new X component value.</param>
|
||||
/// <returns>
|
||||
/// A new instance of <see cref="Vector2Int" /> whose <see cref="Vector2Int.y" /> components is the same as that of
|
||||
/// <paramref name="vector" />, and whose <see cref="Vector2Int.x" /> component is <paramref name="x" />.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2Int WithX(this Vector2Int vector, int x)
|
||||
{
|
||||
return vector with {x = x};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a vector whose X component is the same as the specified vector, and whose Y component is a new value.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to copy.</param>
|
||||
/// <param name="y">The new Y component value.</param>
|
||||
/// <returns>
|
||||
/// A new instance of <see cref="Vector2Int" /> whose <see cref="Vector2Int.x" /> components is the same as that of
|
||||
/// <paramref name="vector" />, and whose <see cref="Vector2Int.y" /> component is <paramref name="y" />.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector2Int WithY(this Vector2Int vector, int y)
|
||||
{
|
||||
return vector with {y = y};
|
||||
}
|
||||
}
|
77
X10D.Unity/src/Numerics/Vector3IntExtensions.cs
Normal file
77
X10D.Unity/src/Numerics/Vector3IntExtensions.cs
Normal file
@ -0,0 +1,77 @@
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Drawing;
|
||||
using System.Runtime.CompilerServices;
|
||||
using UnityEngine;
|
||||
|
||||
namespace X10D.Unity.Numerics;
|
||||
|
||||
/// <summary>
|
||||
/// Numeric-extensions for <see cref="Vector3Int" />.
|
||||
/// </summary>
|
||||
public static class Vector3IntExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Deconstructs the current <see cref="Vector3Int" /> into its components.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to deconstruct.</param>
|
||||
/// <param name="x">The X component value.</param>
|
||||
/// <param name="y">The Y component value.</param>
|
||||
/// <param name="z">The Z component value.</param>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a vector whose Y and Z components are the same as the specified vector, and whose X component is a new value.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to copy.</param>
|
||||
/// <param name="x">The new X component value.</param>
|
||||
/// <returns>
|
||||
/// A new instance of <see cref="Vector3Int" /> whose <see cref="Vector3Int.y" /> and <see cref="Vector3Int.z"/>
|
||||
/// components are the same as that of <paramref name="vector" />, and whose <see cref="Vector3Int.x" /> component is
|
||||
/// <paramref name="x" />.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3Int WithX(this Vector3Int vector, int x)
|
||||
{
|
||||
return vector with {x = x};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a vector whose X and Z components are the same as the specified vector, and whose Y component is a new value.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to copy.</param>
|
||||
/// <param name="y">The new Y component value.</param>
|
||||
/// <returns>
|
||||
/// A new instance of <see cref="Vector3Int" /> whose <see cref="Vector3Int.x" /> and <see cref="Vector3Int.z"/>
|
||||
/// components are the same as that of <paramref name="vector" />, and whose <see cref="Vector3Int.y" /> component is
|
||||
/// <paramref name="y" />.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3Int WithY(this Vector3Int vector, int y)
|
||||
{
|
||||
return vector with {y = y};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a vector whose X and Y components are the same as the specified vector, and whose Z component is a new value.
|
||||
/// </summary>
|
||||
/// <param name="vector">The vector to copy.</param>
|
||||
/// <param name="z">The new Z component value.</param>
|
||||
/// <returns>
|
||||
/// A new instance of <see cref="Vector3Int" /> whose <see cref="Vector3Int.x" /> and <see cref="Vector3Int.y"/>
|
||||
/// components are the same as that of <paramref name="vector" />, and whose <see cref="Vector3Int.z" /> component is
|
||||
/// <paramref name="z" />.
|
||||
/// </returns>
|
||||
[Pure]
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static Vector3Int WithZ(this Vector3Int vector, int z)
|
||||
{
|
||||
return vector with {z = z};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user