feat: ShapeAABB

This commit is contained in:
Syntriax 2024-01-24 18:40:12 +03:00
parent 04653711e9
commit 5b484eb38c
4 changed files with 53 additions and 15 deletions

2
Engine

@ -1 +1 @@
Subproject commit e5732f0ac57886f1b127e154d52b6a9655d7bf85 Subproject commit 3428fcc6ca759805c26f742f9b2ca820953c3ba3

View File

@ -0,0 +1,42 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Apos.Shapes;
using Syntriax.Engine.Core;
using Syntriax.Engine.Physics2D.Primitives;
using Syntriax.Engine.Core.Abstract;
namespace Pong.Behaviours;
public class ShapeAABBBehaviour : BehaviourOverride, IDisplayableShape
{
private readonly List<Vector2D> vectors = [];
private ShapeBehaviour? shapeBehaviour = null;
private readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right;
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; }
public Color Color { get; set; } = Color.White;
public float Thickness { get; set; } = .5f;
protected override void OnFirstActiveFrame()
{
BehaviourController.TryGetBehaviour(out shapeBehaviour);
}
public void Draw(ShapeBatch shapeBatch)
{
if (shapeBehaviour is null)
return;
shapeBehaviour.Shape.TransformShape(Transform, vectors);
AABB aabb = AABB.FromVectors(vectors);
shapeBatch.DrawRectangle(aabb.Center.Scale(screenScale).Subtract(aabb.SizeHalf).ToVector2(), aabb.Size.ToVector2(), Color.Transparent, Color.Blue);
}
}

View File

@ -4,8 +4,6 @@ using Microsoft.Xna.Framework;
using Apos.Shapes; using Apos.Shapes;
using Engine.Physics2D;
using Syntriax.Engine.Core; using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Primitives; using Syntriax.Engine.Physics2D.Primitives;
@ -15,6 +13,7 @@ namespace Pong.Behaviours;
public class ShapeBehaviour : BehaviourOverride, IDisplayableShape public class ShapeBehaviour : BehaviourOverride, IDisplayableShape
{ {
private readonly List<Vector2D> vectors = []; private readonly List<Vector2D> vectors = [];
private readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right;
public ShapeBehaviour(Shape Shape) { this.Shape = Shape; } public ShapeBehaviour(Shape Shape) { this.Shape = Shape; }
public ShapeBehaviour(Shape Shape, float Thickness) { this.Shape = Shape; this.Thickness = Thickness; } public ShapeBehaviour(Shape Shape, float Thickness) { this.Shape = Shape; this.Thickness = Thickness; }
@ -28,10 +27,12 @@ public class ShapeBehaviour : BehaviourOverride, IDisplayableShape
public void Draw(ShapeBatch shapeBatch) public void Draw(ShapeBatch shapeBatch)
{ {
Shape.TransformShape(GameObject.Transform, vectors); Shape.TransformShape(GameObject.Transform, vectors);
for (int i = 0; i < vectors.Count; i++)
vectors[i] = vectors[i].Scale(screenScale);
for (int i = 0; i < vectors.Count - 1; i++) for (int i = 0; i < vectors.Count - 1; i++)
shapeBatch.DrawLine(vectors[i].Scale(Vector2D.Down + Vector2D.Right).ToVector2(), vectors[i + 1].Scale(Vector2D.Down + Vector2D.Right).ToVector2(), Thickness, Color, Color); shapeBatch.DrawLine(vectors[i].ToVector2(), vectors[i + 1].ToVector2(), Thickness, Color, Color);
shapeBatch.DrawLine(vectors[0].Scale(Vector2D.Down + Vector2D.Right).ToVector2(), vectors[^1].Scale(Vector2D.Down + Vector2D.Right).ToVector2(), Thickness, Color, Color); shapeBatch.DrawLine(vectors[0].ToVector2(), vectors[^1].ToVector2(), Thickness, Color, Color);
} }
} }
@ -43,12 +44,6 @@ public static class ShapeTransform
int count = shape.Vertices.Count; int count = shape.Vertices.Count;
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
vectors.Add vectors.Add(transform.TransformVector2D(shape[i]));
(
shape[i]
.Scale(transform.Scale)
.Rotate(transform.Rotation * Physics2D.DegreeToRadian)
.Add(transform.Position)
);
} }
} }

View File

@ -72,7 +72,8 @@ public class Game1 : Game
gameObjectDiamond.Transform.Scale = new Vector2D(100f, 100f); gameObjectDiamond.Transform.Scale = new Vector2D(100f, 100f);
gameObjectDiamond.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>(); gameObjectDiamond.BehaviourController.AddBehaviour<KeyboardInputsBehaviour>();
gameObjectDiamond.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f); gameObjectDiamond.BehaviourController.AddBehaviour<MovementBoxBehaviour>(Keys.W, Keys.S, 268f, -268f, 400f);
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Left])); gameObjectDiamond.BehaviourController.AddBehaviour<ShapeBehaviour>(new Shape([Vector2D.Up, Vector2D.One * 2f, Vector2D.Right, Vector2D.Down, Vector2D.Zero, Vector2D.Left]));
gameObjectDiamond.BehaviourController.AddBehaviour<ShapeAABBBehaviour>();
} }
protected override void Update(GameTime gameTime) protected override void Update(GameTime gameTime)
@ -177,13 +178,13 @@ public class Game1 : Game
_spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform); _spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform);
foreach (IGameObject gameObject in gameManager) foreach (IGameObject gameObject in gameManager)
if (gameObject.BehaviourController.TryGetBehaviour(out IDisplayable? displayable)) foreach (var displayable in gameObject.BehaviourController.GetBehaviours<IDisplayable>())
displayable.Draw(_spriteBatch); displayable.Draw(_spriteBatch);
_spriteBatch.End(); _spriteBatch.End();
_shapeBatch.Begin(cameraBehaviour.MatrixTransform); _shapeBatch.Begin(cameraBehaviour.MatrixTransform);
foreach (IGameObject gameObject in gameManager) foreach (IGameObject gameObject in gameManager)
if (gameObject.BehaviourController.TryGetBehaviour(out IDisplayableShape? displayableShape)) foreach (var displayableShape in gameObject.BehaviourController.GetBehaviours<IDisplayableShape>())
displayableShape.Draw(_shapeBatch); displayableShape.Draw(_shapeBatch);
_shapeBatch.End(); _shapeBatch.End();