refactor: Shape2D converted into a class as it has a reference type
This commit is contained in:
		@@ -11,19 +11,33 @@ namespace Syntriax.Engine.Core;
 | 
			
		||||
/// Initializes a new instance of a <see cref="Shape2D"/> struct with the specified vertices.
 | 
			
		||||
/// </remarks>
 | 
			
		||||
[System.Diagnostics.DebuggerDisplay("Vertices Count: {Vertices.Count}")]
 | 
			
		||||
public readonly struct Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
 | 
			
		||||
public class Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
 | 
			
		||||
{
 | 
			
		||||
    public static readonly Shape2D Triangle = CreateNgon(3, Vector2D.Up);
 | 
			
		||||
    public static readonly Shape2D Square = CreateNgon(4, Vector2D.One);
 | 
			
		||||
    public static readonly Shape2D Pentagon = CreateNgon(5, Vector2D.Up);
 | 
			
		||||
    public static readonly Shape2D Hexagon = CreateNgon(6, Vector2D.Right);
 | 
			
		||||
    public static Shape2D Triangle => CreateNgon(3, Vector2D.Up);
 | 
			
		||||
    public static Shape2D Square => CreateNgon(4, Vector2D.One);
 | 
			
		||||
    public static Shape2D Pentagon => CreateNgon(5, Vector2D.Up);
 | 
			
		||||
    public static Shape2D Hexagon => CreateNgon(6, Vector2D.Right);
 | 
			
		||||
 | 
			
		||||
    private readonly List<Vector2D> _verticesList = vertices;
 | 
			
		||||
    public event ShapeUpdatedEventHandler? OnShapeUpdated = null;
 | 
			
		||||
 | 
			
		||||
    private List<Vector2D> _vertices = vertices;
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Gets the vertices of the <see cref="Shape2D"/>.
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    public IReadOnlyList<Vector2D> Vertices => _verticesList;
 | 
			
		||||
    public IReadOnlyList<Vector2D> Vertices
 | 
			
		||||
    {
 | 
			
		||||
        get => _vertices;
 | 
			
		||||
        set
 | 
			
		||||
        {
 | 
			
		||||
            _vertices.Clear();
 | 
			
		||||
 | 
			
		||||
            foreach (Vector2D vertex in value)
 | 
			
		||||
                _vertices.Add(vertex);
 | 
			
		||||
 | 
			
		||||
            OnShapeUpdated?.InvokeSafe(this);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// The vertex at the specified index.
 | 
			
		||||
@@ -207,13 +221,15 @@ public readonly struct Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
 | 
			
		||||
    /// <param name="from">The <see cref="Shape2D"/> to transform.</param>
 | 
			
		||||
    /// <param name="transform">The <see cref="ITransform2D"/> to apply.</param>
 | 
			
		||||
    /// <param name="to">The transformed <see cref="Shape2D"/>.</param>
 | 
			
		||||
    public static void Transform(Shape2D from, ITransform2D transform, ref Shape2D to)
 | 
			
		||||
    public static void Transform(Shape2D from, ITransform2D transform, Shape2D to)
 | 
			
		||||
    {
 | 
			
		||||
        to._verticesList.Clear();
 | 
			
		||||
        to._vertices.Clear();
 | 
			
		||||
 | 
			
		||||
        int count = from._verticesList.Count;
 | 
			
		||||
        int count = from._vertices.Count;
 | 
			
		||||
        for (int i = 0; i < count; i++)
 | 
			
		||||
            to._verticesList.Add(transform.Transform(from[i]));
 | 
			
		||||
            to._vertices.Add(transform.Transform(from[i]));
 | 
			
		||||
 | 
			
		||||
        to.OnShapeUpdated?.InvokeSafe(to);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
@@ -240,6 +256,8 @@ public readonly struct Shape2D(List<Vector2D> vertices) : IEnumerable<Vector2D>
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc/>
 | 
			
		||||
    IEnumerator IEnumerable.GetEnumerator() => Vertices.GetEnumerator();
 | 
			
		||||
 | 
			
		||||
    public delegate void ShapeUpdatedEventHandler(Shape2D shape2D);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/// <summary>
 | 
			
		||||
@@ -275,13 +293,13 @@ public static class Shape2DExtensions
 | 
			
		||||
    public static Shape2D Transform(this ITransform2D transform, Shape2D shape) => Shape2D.Transform(shape, transform);
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc cref="Shape2D.Transform(Shape2D, ITransform2D, Shape2D)" />
 | 
			
		||||
    public static void Transform(this ITransform2D transform, Shape2D from, ref Shape2D to) => Shape2D.Transform(from, transform, ref to);
 | 
			
		||||
    public static void Transform(this ITransform2D transform, Shape2D from, Shape2D to) => Shape2D.Transform(from, transform, to);
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc cref="Shape2D.Transform(Shape2D, ITransform2D)" />
 | 
			
		||||
    public static Shape2D Transform(this Shape2D shape, ITransform2D transform) => Shape2D.Transform(shape, transform);
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc cref="Shape2D.Transform(Shape2D, ITransform2D, ref Shape2D)" />
 | 
			
		||||
    public static void Transform(this Shape2D from, ITransform2D transform, ref Shape2D to) => Shape2D.Transform(from, transform, ref to);
 | 
			
		||||
    /// <inheritdoc cref="Shape2D.Transform(Shape2D, ITransform2D,Shape2D)" />
 | 
			
		||||
    public static void Transform(this Shape2D from, ITransform2D transform, Shape2D to) => Shape2D.Transform(from, transform, to);
 | 
			
		||||
 | 
			
		||||
    /// <inheritdoc cref="Shape2D.ApproximatelyEquals(Shape2D, Shape2D, float)" />
 | 
			
		||||
    public static bool ApproximatelyEquals(this Shape2D left, Shape2D right, float epsilon = float.Epsilon) => Shape2D.ApproximatelyEquals(left, right, epsilon);
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,7 @@ public class Collider2DShapeBehaviour : Collider2DBehaviourBase, IShapeCollider2
 | 
			
		||||
    private Shape2D _shapeWorld = Shape2D.Square.CreateCopy();
 | 
			
		||||
    private Shape2D _shapeLocal = Shape2D.Square;
 | 
			
		||||
 | 
			
		||||
    public override void CalculateCollider() => Transform.Transform(ShapeLocal, ref _shapeWorld);
 | 
			
		||||
    public override void CalculateCollider() => ShapeLocal.Transform(Transform, _shapeWorld);
 | 
			
		||||
 | 
			
		||||
    public Collider2DShapeBehaviour() { }
 | 
			
		||||
    public Collider2DShapeBehaviour(Shape2D shape)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user