diff --git a/Engine.Core/Primitives/Shape2D.cs b/Engine.Core/Primitives/Shape2D.cs index afde2d8..914b0cd 100644 --- a/Engine.Core/Primitives/Shape2D.cs +++ b/Engine.Core/Primitives/Shape2D.cs @@ -11,19 +11,33 @@ namespace Syntriax.Engine.Core; /// Initializes a new instance of a struct with the specified vertices. /// [System.Diagnostics.DebuggerDisplay("Vertices Count: {Vertices.Count}")] -public readonly struct Shape2D(List vertices) : IEnumerable +public class Shape2D(List vertices) : IEnumerable { - 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 _verticesList = vertices; + public event ShapeUpdatedEventHandler? OnShapeUpdated = null; + + private List _vertices = vertices; /// /// Gets the vertices of the . /// - public IReadOnlyList Vertices => _verticesList; + public IReadOnlyList Vertices + { + get => _vertices; + set + { + _vertices.Clear(); + + foreach (Vector2D vertex in value) + _vertices.Add(vertex); + + OnShapeUpdated?.InvokeSafe(this); + } + } /// /// The vertex at the specified index. @@ -207,13 +221,15 @@ public readonly struct Shape2D(List vertices) : IEnumerable /// The to transform. /// The to apply. /// The transformed . - 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); } /// @@ -240,6 +256,8 @@ public readonly struct Shape2D(List vertices) : IEnumerable /// IEnumerator IEnumerable.GetEnumerator() => Vertices.GetEnumerator(); + + public delegate void ShapeUpdatedEventHandler(Shape2D shape2D); } /// @@ -275,13 +293,13 @@ public static class Shape2DExtensions public static Shape2D Transform(this ITransform2D transform, Shape2D shape) => Shape2D.Transform(shape, transform); /// - 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); /// public static Shape2D Transform(this Shape2D shape, ITransform2D transform) => Shape2D.Transform(shape, transform); - /// - public static void Transform(this Shape2D from, ITransform2D transform, ref Shape2D to) => Shape2D.Transform(from, transform, ref to); + /// + public static void Transform(this Shape2D from, ITransform2D transform, Shape2D to) => Shape2D.Transform(from, transform, to); /// public static bool ApproximatelyEquals(this Shape2D left, Shape2D right, float epsilon = float.Epsilon) => Shape2D.ApproximatelyEquals(left, right, epsilon); diff --git a/Engine.Physics2D/Collider2DShapeBehaviour.cs b/Engine.Physics2D/Collider2DShapeBehaviour.cs index 1d85056..2ba213e 100644 --- a/Engine.Physics2D/Collider2DShapeBehaviour.cs +++ b/Engine.Physics2D/Collider2DShapeBehaviour.cs @@ -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)