From 0987bf7e91187cc9397f594930d5b526e84cfd6e Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 15 Jun 2025 16:36:01 +0300 Subject: [PATCH] refactor: Apos to Triangles --- Engine | 2 +- Shared/Abstract/IDrawableShape.cs | 8 ----- Shared/Behaviours/CircleBehaviour.cs | 30 ++++++++-------- Shared/Behaviours/ShapeAABBBehaviour.cs | 47 ------------------------- Shared/Behaviours/ShapeBatcher.cs | 35 ------------------ Shared/Behaviours/ShapeBehaviour.cs | 27 ++++++-------- Shared/PongUniverse.cs | 2 +- Shared/Shared.csproj | 1 - 8 files changed, 27 insertions(+), 125 deletions(-) delete mode 100644 Shared/Abstract/IDrawableShape.cs delete mode 100644 Shared/Behaviours/ShapeAABBBehaviour.cs delete mode 100644 Shared/Behaviours/ShapeBatcher.cs diff --git a/Engine b/Engine index 2335c3e..cf7061f 160000 --- a/Engine +++ b/Engine @@ -1 +1 @@ -Subproject commit 2335c3ec628a3dc55fc5ba96cd6c1eea2ba1a3f9 +Subproject commit cf7061fd58189827edbf4ef3915636a6fd537d7c diff --git a/Shared/Abstract/IDrawableShape.cs b/Shared/Abstract/IDrawableShape.cs deleted file mode 100644 index 2be25ff..0000000 --- a/Shared/Abstract/IDrawableShape.cs +++ /dev/null @@ -1,8 +0,0 @@ -using Apos.Shapes; - -namespace Syntriax.Engine.Core; - -public interface IDrawableShape : IBehaviour -{ - void Draw(ShapeBatch shapeBatch); -} diff --git a/Shared/Behaviours/CircleBehaviour.cs b/Shared/Behaviours/CircleBehaviour.cs index d342718..b454cf3 100644 --- a/Shared/Behaviours/CircleBehaviour.cs +++ b/Shared/Behaviours/CircleBehaviour.cs @@ -1,29 +1,29 @@ -using Microsoft.Xna.Framework; - -using Apos.Shapes; - using Syntriax.Engine.Core; using Syntriax.Engine.Integration.MonoGame; namespace Pong.Behaviours; -public class CircleBehaviour : Syntriax.Engine.Physics2D.Collider2DCircleBehaviour, IDrawableShape +public class CircleBehaviour : Syntriax.Engine.Physics2D.Collider2DCircleBehaviour, IDrawableTriangle { - public Color Color { get; set; } = Color.White; - public float Thickness { get; set; } = .5f; + private const float CIRCLE_SEGMENT_COUNT = 32f; - public void Draw(ShapeBatch shapeBatch) + public ColorRGBA Color { get; set; } = new ColorRGBA(255, 255, 255); + + public void Draw(ITriangleBatch triangleBatch) { - if (!IsActive) - return; - Recalculate(); - shapeBatch.BorderCircle(CircleWorld.Center.ToDisplayVector2(), CircleWorld.Radius, Color); + for (int i = 0; i < CIRCLE_SEGMENT_COUNT; i++) + { + float iPi1 = i / CIRCLE_SEGMENT_COUNT * 2f * Math.PI; + float iPi2 = (i + 1f).Mod(CIRCLE_SEGMENT_COUNT) / CIRCLE_SEGMENT_COUNT * 2f * Math.PI; + + Vector2D firstVertex = new Vector2D(Math.Sin(iPi1), Math.Cos(iPi1)) * CircleWorld.Radius; + Vector2D secondVertex = new Vector2D(Math.Sin(iPi2), Math.Cos(iPi2)) * CircleWorld.Radius; + triangleBatch.Draw(new(CircleWorld.Center, CircleWorld.Center + firstVertex, CircleWorld.Center + secondVertex), Color); + } } public CircleBehaviour(Circle circle) : base(circle) { } - public CircleBehaviour(Circle circle, float thickness) : base(circle) { Thickness = thickness; } - public CircleBehaviour(Circle circle, Color color) : base(circle) { Color = color; } - public CircleBehaviour(Circle circle, Color color, float thickness) : base(circle) { Thickness = thickness; Color = color; } + public CircleBehaviour(Circle circle, ColorRGBA color) : base(circle) { Color = color; } } diff --git a/Shared/Behaviours/ShapeAABBBehaviour.cs b/Shared/Behaviours/ShapeAABBBehaviour.cs deleted file mode 100644 index 0ab4fd9..0000000 --- a/Shared/Behaviours/ShapeAABBBehaviour.cs +++ /dev/null @@ -1,47 +0,0 @@ -using Microsoft.Xna.Framework; - -using Apos.Shapes; - -using Syntriax.Engine.Core; -using Syntriax.Engine.Integration.MonoGame; -using Syntriax.Engine.Physics2D; -using Syntriax.Engine.Systems.Input; - -namespace Pong.Behaviours; - -public class ShapeAABBBehaviour : Behaviour2D, IDrawableShape, IFirstFrameUpdate -{ - private IShapeCollider2D? shapeCollider = null; - - public Color Color { get; set; } = Color.White; - public float Thickness { get; set; } = .5f; - public bool display = true; - - public void FirstActiveFrame() - { - BehaviourController.TryGetBehaviour(out shapeCollider); - - if (BehaviourController.TryGetBehaviour(out IButtonInputs? keys)) - keys.RegisterOnPress(Microsoft.Xna.Framework.Input.Keys.D, (_1, _2) => display = !display); - } - - public void Draw(ShapeBatch shapeBatch) - { - if (!display) - return; - - if (shapeCollider is null) - return; - - AABB aabb = AABB.FromVectors(shapeCollider.ShapeWorld); - - shapeBatch.BorderCircle(aabb.Center.ToDisplayVector2(), 7.5f, Color.Beige); - - shapeBatch.DrawRectangle(aabb.Center.ApplyDisplayScale().Subtract(aabb.SizeHalf).ToVector2(), aabb.Size.ToVector2(), Color.Transparent, Color.Blue); - } - - public ShapeAABBBehaviour() { } - public ShapeAABBBehaviour(float Thickness) { this.Thickness = Thickness; } - public ShapeAABBBehaviour(Color color) { Color = color; } - public ShapeAABBBehaviour(Color color, float Thickness) { this.Thickness = Thickness; Color = color; } -} diff --git a/Shared/Behaviours/ShapeBatcher.cs b/Shared/Behaviours/ShapeBatcher.cs deleted file mode 100644 index 983c3b3..0000000 --- a/Shared/Behaviours/ShapeBatcher.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Collections.Generic; - -using Apos.Shapes; - -using Syntriax.Engine.Core; -using Syntriax.Engine.Integration.MonoGame; - -namespace Pong.Behaviours; - -public class ShapeBatcher : BehaviourBase, IFirstFrameUpdate, IDraw -{ - private static Comparer SortByPriority() => Comparer.Create((x, y) => y.Priority.CompareTo(x.Priority)); - private ShapeBatch shapeBatch = null!; - private MonoGameCamera2DBehaviour camera2D = null!; - private readonly ActiveBehaviourCollectorSorted drawableShapes = new() { SortBy = SortByPriority() }; - - public void FirstActiveFrame() - { - MonoGameWindowContainer windowContainer = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour(); - camera2D = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour(); - - shapeBatch = new(windowContainer.Window.GraphicsDevice, windowContainer.Window.Content); - - drawableShapes.Unassign(); - drawableShapes.Assign(Universe); - } - - public void Draw() - { - shapeBatch.Begin(camera2D.MatrixTransform); - for (int i = drawableShapes.Count - 1; i >= 0; i--) - drawableShapes[i].Draw(shapeBatch); - shapeBatch.End(); - } -} diff --git a/Shared/Behaviours/ShapeBehaviour.cs b/Shared/Behaviours/ShapeBehaviour.cs index aaa104e..a338ca9 100644 --- a/Shared/Behaviours/ShapeBehaviour.cs +++ b/Shared/Behaviours/ShapeBehaviour.cs @@ -1,33 +1,26 @@ -using Microsoft.Xna.Framework; - -using Apos.Shapes; +using System.Collections.Generic; using Syntriax.Engine.Core; using Syntriax.Engine.Integration.MonoGame; namespace Pong.Behaviours; -public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour, IDrawableShape +public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour, IDrawableTriangle { - public Color Color { get; set; } = Color.White; - public float Thickness { get; set; } = .5f; + private readonly IList triangles = []; - public void Draw(ShapeBatch shapeBatch) + public ColorRGBA Color { get; set; } = new ColorRGBA(255, 255, 255); + + public void Draw(ITriangleBatch triangleBatch) { - if (!IsActive) - return; - Recalculate(); - int count = ShapeWorld.Vertices.Count; + ShapeWorld.ToTrianglesConvex(triangles); - for (int i = 0; i < count - 1; i++) - shapeBatch.DrawLine(ShapeWorld[i].ToDisplayVector2(), ShapeWorld[i + 1].ToDisplayVector2(), Thickness, Color, Color); - shapeBatch.DrawLine(ShapeWorld[0].ToDisplayVector2(), ShapeWorld[^1].ToDisplayVector2(), Thickness, Color, Color); + foreach (Triangle triangle in triangles) + triangleBatch.Draw(triangle, Color); } public ShapeBehaviour(Shape2D shape) : base(shape) { } - public ShapeBehaviour(Shape2D shape, float thickness) : base(shape) { Thickness = thickness; } - public ShapeBehaviour(Shape2D shape, Color color) : base(shape) { Color = color; } - public ShapeBehaviour(Shape2D shape, Color color, float thickness) : base(shape) { Thickness = thickness; Color = color; } + public ShapeBehaviour(Shape2D shape, ColorRGBA color) : base(shape) { Color = color; } } diff --git a/Shared/PongUniverse.cs b/Shared/PongUniverse.cs index 2f5cc32..a08c28c 100644 --- a/Shared/PongUniverse.cs +++ b/Shared/PongUniverse.cs @@ -33,7 +33,7 @@ public static class PongUniverse client.Connect("localhost", 8888); DrawManager drawManager = universe.InstantiateUniverseObject().SetUniverseObject("Draw Manager").BehaviourController.AddBehaviour(); - universe.InstantiateUniverseObject().SetUniverseObject("Shape Batcher", drawManager.UniverseObject).BehaviourController.AddBehaviour(); + universe.InstantiateUniverseObject().SetUniverseObject("Triangle Batcher", drawManager.UniverseObject).BehaviourController.AddBehaviour(); universe.InstantiateUniverseObject().SetUniverseObject("Sprite Batcher", drawManager.UniverseObject).BehaviourController.AddBehaviour(); //////////////////////////////////////////////////////////////////////////////////// diff --git a/Shared/Shared.csproj b/Shared/Shared.csproj index 2a19c0d..42bbbfe 100644 --- a/Shared/Shared.csproj +++ b/Shared/Shared.csproj @@ -4,7 +4,6 @@ enable -