diff --git a/.circleci/config.yml b/.circleci/config.yml index e190f28..d2f1ea3 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,7 +3,7 @@ version: 2.1 defaults: &defaults working_directory: ~/repo docker: - - image: mcr.microsoft.com/dotnet/core/sdk:2.2 + - image: mcr.microsoft.com/dotnet/core/sdk:3.0 environment: DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 DOTNET_CLI_TELEMETRY_OPTOUT: 1 diff --git a/Structs/Position2D.cs b/Structs/Position2D.cs index 48e5554..24db499 100644 --- a/Structs/Position2D.cs +++ b/Structs/Position2D.cs @@ -3,7 +3,7 @@ using Microsoft.Xna.Framework; namespace MoonTools.Core.Structs { - public struct Position2D + public struct Position2D : System.IEquatable { private Vector2 remainder; @@ -146,5 +146,37 @@ namespace MoonTools.Core.Structs { return (int)Math.Floor(value); } + + public override bool Equals(object other) + { + if (other is Position2D otherPosition) + { + return Equals(otherPosition); + } + + return false; + } + + public bool Equals(Position2D other) + { + return + X == other.X && + Y == other.Y; + } + + public override int GetHashCode() + { + return X.GetHashCode() ^ Y.GetHashCode(); + } + + public static bool operator ==(Position2D a, Position2D b) + { + return a.Equals(b); + } + + public static bool operator !=(Position2D a, Position2D b) + { + return !(a == b); + } } } \ No newline at end of file diff --git a/Structs/Transform2D.cs b/Structs/Transform2D.cs index 002760a..52fe7a0 100644 --- a/Structs/Transform2D.cs +++ b/Structs/Transform2D.cs @@ -58,13 +58,16 @@ namespace MoonTools.Core.Structs } } - public static Transform2D DefaultTransform { - get { + public static Transform2D DefaultTransform + { + get + { return _defaultTransform; } } - public Transform2D(Position2D position) { + public Transform2D(Position2D position) + { _position = position; _rotation = 0f; _scale = new Vector2(1, 1); @@ -79,7 +82,8 @@ namespace MoonTools.Core.Structs TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale); } - public Transform2D(Position2D position, float rotation) { + public Transform2D(Position2D position, float rotation) + { _position = position; _rotation = rotation; _scale = new Vector2(1, 1); @@ -94,27 +98,25 @@ namespace MoonTools.Core.Structs TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale); } - public Transform2D(Position2D position, float rotation, Vector2 scale) { + public Transform2D(Position2D position, float rotation, Vector2 scale) + { _position = position; _rotation = rotation; _scale = scale; TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale); } - public Transform2D(Vector2 position, float rotation, Vector2 scale) { + public Transform2D(Vector2 position, float rotation, Vector2 scale) + { _position = new Position2D(position.X, position.Y); _rotation = rotation; _scale = scale; TransformMatrix = CreateTransformMatrix(_position, _rotation, _scale); } - public Transform2D Compose(Transform2D other) { - return new Transform2D(Position + other.Position, Rotation + other.Rotation, Scale * other.Scale); - } - - public bool Equals(Transform2D other) + public Transform2D Compose(Transform2D other) { - return TransformMatrix == other.TransformMatrix; + return new Transform2D(Position + other.Position, Rotation + other.Rotation, Scale * other.Scale); } private static Matrix CreateTransformMatrix(Position2D translation, float rotationDegrees, Vector2 scale) @@ -123,5 +125,35 @@ namespace MoonTools.Core.Structs Matrix.CreateRotationZ(Microsoft.Xna.Framework.MathHelper.ToRadians(rotationDegrees)) * Matrix.CreateTranslation(translation.X, translation.Y, 0); } + + public override bool Equals(Object other) + { + if (other is Transform2D otherTransform) + { + return Equals(otherTransform); + } + + return false; + } + + public bool Equals(Transform2D other) + { + return TransformMatrix == other.TransformMatrix; + } + + public override int GetHashCode() + { + return TransformMatrix.GetHashCode(); + } + + public static bool operator ==(Transform2D a, Transform2D b) + { + return a.Equals(b); + } + + public static bool operator !=(Transform2D a, Transform2D b) + { + return !(a == b); + } } } diff --git a/Tests/Position2DTest.cs b/Tests/Position2DTest.cs index 276236d..675c1b2 100644 --- a/Tests/Position2DTest.cs +++ b/Tests/Position2DTest.cs @@ -61,5 +61,41 @@ namespace Tests result.X.Should().BeApproximately(1.2f, 0.01f); result.Y.Should().BeApproximately(6.4f, 0.01f); } + + [Test] + public void Equals() + { + var one = new Position2D(5.2f, 4.7f); + var two = new Position2D(5.1f, 4.4f); + + one.Equals(two).Should().BeTrue(); + } + + [Test] + public void EqualsOperator() + { + var one = new Position2D(5.2f, 4.7f); + var two = new Position2D(5.1f, 4.4f); + + (one == two).Should().BeTrue(); + } + + [Test] + public void NotEquals() + { + var one = new Position2D(5.2f, 4.7f); + var two = new Position2D(3.1f, 2.4f); + + one.Equals(two).Should().BeFalse(); + } + + [Test] + public void NotEqualsOperator() + { + var one = new Position2D(5.2f, 4.7f); + var two = new Position2D(3.1f, 2.4f); + + (one != two).Should().BeTrue(); + } } } \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 5ee2f07..39e2bd3 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -1,6 +1,6 @@ - netcoreapp2.2 + netcoreapp3.0 false diff --git a/Tests/Transform2DTest.cs b/Tests/Transform2DTest.cs index 75ed648..018e941 100644 --- a/Tests/Transform2DTest.cs +++ b/Tests/Transform2DTest.cs @@ -25,6 +25,24 @@ namespace Tests transformA.Should().NotBeEquivalentTo(transformB); } + [Test] + public void EqualsOperator() + { + var transformA = new Transform2D(new Position2D(0, 1), 4f, new Vector2(2, 1)); + var transformB = new Transform2D(new Position2D(0, 1), 4f, new Vector2(2, 1)); + + (transformA == transformB).Should().BeTrue(); + } + + [Test] + public void NotEqualsOperator() + { + var transformA = new Transform2D(new Position2D(2, 3)); + var transformB = new Transform2D(new Position2D(5, 1)); + + (transformA != transformB).Should().BeTrue(); + } + [Test] public void Compose() {