refactor: Apos to Triangles

This commit is contained in:
Syntriax 2025-06-15 16:36:01 +03:00
parent 648be2738f
commit 0987bf7e91
8 changed files with 27 additions and 125 deletions

2
Engine

@ -1 +1 @@
Subproject commit 2335c3ec628a3dc55fc5ba96cd6c1eea2ba1a3f9
Subproject commit cf7061fd58189827edbf4ef3915636a6fd537d7c

View File

@ -1,8 +0,0 @@
using Apos.Shapes;
namespace Syntriax.Engine.Core;
public interface IDrawableShape : IBehaviour
{
void Draw(ShapeBatch shapeBatch);
}

View File

@ -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; }
}

View File

@ -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<Microsoft.Xna.Framework.Input.Keys>? 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; }
}

View File

@ -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<IBehaviour> SortByPriority() => Comparer<IBehaviour>.Create((x, y) => y.Priority.CompareTo(x.Priority));
private ShapeBatch shapeBatch = null!;
private MonoGameCamera2DBehaviour camera2D = null!;
private readonly ActiveBehaviourCollectorSorted<IDrawableShape> drawableShapes = new() { SortBy = SortByPriority() };
public void FirstActiveFrame()
{
MonoGameWindowContainer windowContainer = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameWindowContainer>();
camera2D = BehaviourController.UniverseObject.Universe.FindRequiredBehaviour<MonoGameCamera2DBehaviour>();
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();
}
}

View File

@ -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<Triangle> 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; }
}

View File

@ -33,7 +33,7 @@ public static class PongUniverse
client.Connect("localhost", 8888);
DrawManager drawManager = universe.InstantiateUniverseObject().SetUniverseObject("Draw Manager").BehaviourController.AddBehaviour<DrawManager>();
universe.InstantiateUniverseObject().SetUniverseObject("Shape Batcher", drawManager.UniverseObject).BehaviourController.AddBehaviour<ShapeBatcher>();
universe.InstantiateUniverseObject().SetUniverseObject("Triangle Batcher", drawManager.UniverseObject).BehaviourController.AddBehaviour<TriangleBatcher>();
universe.InstantiateUniverseObject().SetUniverseObject("Sprite Batcher", drawManager.UniverseObject).BehaviourController.AddBehaviour<SpriteBatcher>();
////////////////////////////////////////////////////////////////////////////////////

View File

@ -4,7 +4,6 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Apos.Shapes" Version="0.2.3" />
<PackageReference Include="LiteNetLib" Version="1.3.1" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.2.1105">