From 5b484eb38c7211b780a160cf0dd9b3068c4130d7 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Wed, 24 Jan 2024 18:40:12 +0300 Subject: [PATCH] feat: ShapeAABB --- Engine | 2 +- Game/Behaviours/ShapeAABBBehaviour.cs | 42 +++++++++++++++++++++++++++ Game/Behaviours/ShapeBehaviour.cs | 17 ++++------- Game/Game1.cs | 7 +++-- 4 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 Game/Behaviours/ShapeAABBBehaviour.cs diff --git a/Engine b/Engine index e5732f0..3428fcc 160000 --- a/Engine +++ b/Engine @@ -1 +1 @@ -Subproject commit e5732f0ac57886f1b127e154d52b6a9655d7bf85 +Subproject commit 3428fcc6ca759805c26f742f9b2ca820953c3ba3 diff --git a/Game/Behaviours/ShapeAABBBehaviour.cs b/Game/Behaviours/ShapeAABBBehaviour.cs new file mode 100644 index 0000000..c0b7e5e --- /dev/null +++ b/Game/Behaviours/ShapeAABBBehaviour.cs @@ -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 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); + } +} diff --git a/Game/Behaviours/ShapeBehaviour.cs b/Game/Behaviours/ShapeBehaviour.cs index 4771049..cd0b2bd 100644 --- a/Game/Behaviours/ShapeBehaviour.cs +++ b/Game/Behaviours/ShapeBehaviour.cs @@ -4,8 +4,6 @@ using Microsoft.Xna.Framework; using Apos.Shapes; -using Engine.Physics2D; - using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D.Primitives; @@ -15,6 +13,7 @@ namespace Pong.Behaviours; public class ShapeBehaviour : BehaviourOverride, IDisplayableShape { private readonly List vectors = []; + private readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right; public ShapeBehaviour(Shape Shape) { this.Shape = Shape; } 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) { 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++) - 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[0].Scale(Vector2D.Down + Vector2D.Right).ToVector2(), vectors[^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].ToVector2(), vectors[^1].ToVector2(), Thickness, Color, Color); } } @@ -43,12 +44,6 @@ public static class ShapeTransform int count = shape.Vertices.Count; for (int i = 0; i < count; i++) - vectors.Add - ( - shape[i] - .Scale(transform.Scale) - .Rotate(transform.Rotation * Physics2D.DegreeToRadian) - .Add(transform.Position) - ); + vectors.Add(transform.TransformVector2D(shape[i])); } } diff --git a/Game/Game1.cs b/Game/Game1.cs index 42b3fd2..2b0482a 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -72,7 +72,8 @@ public class Game1 : Game gameObjectDiamond.Transform.Scale = new Vector2D(100f, 100f); gameObjectDiamond.BehaviourController.AddBehaviour(); gameObjectDiamond.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); - gameObjectDiamond.BehaviourController.AddBehaviour(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Left])); + gameObjectDiamond.BehaviourController.AddBehaviour(new Shape([Vector2D.Up, Vector2D.One * 2f, Vector2D.Right, Vector2D.Down, Vector2D.Zero, Vector2D.Left])); + gameObjectDiamond.BehaviourController.AddBehaviour(); } protected override void Update(GameTime gameTime) @@ -177,13 +178,13 @@ public class Game1 : Game _spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform); foreach (IGameObject gameObject in gameManager) - if (gameObject.BehaviourController.TryGetBehaviour(out IDisplayable? displayable)) + foreach (var displayable in gameObject.BehaviourController.GetBehaviours()) displayable.Draw(_spriteBatch); _spriteBatch.End(); _shapeBatch.Begin(cameraBehaviour.MatrixTransform); foreach (IGameObject gameObject in gameManager) - if (gameObject.BehaviourController.TryGetBehaviour(out IDisplayableShape? displayableShape)) + foreach (var displayableShape in gameObject.BehaviourController.GetBehaviours()) displayableShape.Draw(_shapeBatch); _shapeBatch.End();