diff --git a/Engine b/Engine index 3428fcc..ed15238 160000 --- a/Engine +++ b/Engine @@ -1 +1 @@ -Subproject commit 3428fcc6ca759805c26f742f9b2ca820953c3ba3 +Subproject commit ed15238dcdc094453c697a8f83d9308f6702dc21 diff --git a/Game/Behaviours/RotatableBehaviour.cs b/Game/Behaviours/RotatableBehaviour.cs new file mode 100644 index 0000000..15f3201 --- /dev/null +++ b/Game/Behaviours/RotatableBehaviour.cs @@ -0,0 +1,29 @@ +using Microsoft.Xna.Framework; + +using Apos.Shapes; + +using Syntriax.Engine.Core; + +namespace Pong.Behaviours; + +public class RotatableBehaviour : BehaviourOverride +{ + private KeyboardInputsBehaviour? inputs = null; + + protected override void OnFirstActiveFrame() + { + if (BehaviourController.TryGetBehaviour(out inputs)) + inputs.BehaviourController.AddBehaviour(); + } + + protected override void OnUpdate() + { + if (inputs is null) + return; + + if (inputs.IsPressed(Microsoft.Xna.Framework.Input.Keys.NumPad4)) + Transform.Rotation += Time.Elapsed.Nanoseconds * 0.0025f; + if (inputs.IsPressed(Microsoft.Xna.Framework.Input.Keys.NumPad6)) + Transform.Rotation -= Time.Elapsed.Nanoseconds * 0.0025f; + } +} diff --git a/Game/Behaviours/ShapeAABBBehaviour.cs b/Game/Behaviours/ShapeAABBBehaviour.cs index c0b7e5e..9f22fe9 100644 --- a/Game/Behaviours/ShapeAABBBehaviour.cs +++ b/Game/Behaviours/ShapeAABBBehaviour.cs @@ -1,19 +1,17 @@ -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; +using Syntriax.Engine.Physics2D.Abstract; namespace Pong.Behaviours; public class ShapeAABBBehaviour : BehaviourOverride, IDisplayableShape { - private readonly List vectors = []; - private ShapeBehaviour? shapeBehaviour = null; + private Shape transformedShape = new([]); + private IShapeCollider2D? shapeCollider = null; private readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right; public ShapeAABBBehaviour() { } @@ -26,16 +24,17 @@ public class ShapeAABBBehaviour : BehaviourOverride, IDisplayableShape protected override void OnFirstActiveFrame() { - BehaviourController.TryGetBehaviour(out shapeBehaviour); + BehaviourController.TryGetBehaviour(out shapeCollider); } public void Draw(ShapeBatch shapeBatch) { - if (shapeBehaviour is null) + if (shapeCollider is null) return; - shapeBehaviour.Shape.TransformShape(Transform, vectors); - AABB aabb = AABB.FromVectors(vectors); + AABB aabb = AABB.FromVectors(shapeCollider.ShapeWorld); + + shapeBatch.BorderCircle(aabb.Center.ToDisplayVector2(), 7.5f, Color.Beige); 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 cd0b2bd..e4c089e 100644 --- a/Game/Behaviours/ShapeBehaviour.cs +++ b/Game/Behaviours/ShapeBehaviour.cs @@ -1,49 +1,31 @@ -using System.Collections.Generic; - using Microsoft.Xna.Framework; using Apos.Shapes; using Syntriax.Engine.Core; -using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D.Primitives; namespace Pong.Behaviours; -public class ShapeBehaviour : BehaviourOverride, IDisplayableShape +public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour, IDisplayableShape { - private readonly List vectors = []; - private readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right; + public ShapeBehaviour(Shape Shape) { this.ShapeLocal = Shape; } + public ShapeBehaviour(Shape Shape, float Thickness) { this.ShapeLocal = Shape; this.Thickness = Thickness; } + public ShapeBehaviour(Shape Shape, Color color) { this.ShapeLocal = Shape; Color = color; } + public ShapeBehaviour(Shape Shape, Color color, float Thickness) { this.ShapeLocal = Shape; this.Thickness = Thickness; Color = color; } - public ShapeBehaviour(Shape Shape) { this.Shape = Shape; } - public ShapeBehaviour(Shape Shape, float Thickness) { this.Shape = Shape; this.Thickness = Thickness; } - public ShapeBehaviour(Shape Shape, Color color) { this.Shape = Shape; Color = color; } - public ShapeBehaviour(Shape Shape, Color color, float Thickness) { this.Shape = Shape; this.Thickness = Thickness; Color = color; } - - public Shape Shape { get; } = default!; public Color Color { get; set; } = Color.White; public float Thickness { get; set; } = .5f; public void Draw(ShapeBatch shapeBatch) { - Shape.TransformShape(GameObject.Transform, vectors); - for (int i = 0; i < vectors.Count; i++) - vectors[i] = vectors[i].Scale(screenScale); + Recalculate(); + int count = ShapeWorld.Vertices.Count; - for (int i = 0; i < vectors.Count - 1; i++) - shapeBatch.DrawLine(vectors[i].ToVector2(), vectors[i + 1].ToVector2(), Thickness, Color, Color); - shapeBatch.DrawLine(vectors[0].ToVector2(), vectors[^1].ToVector2(), Thickness, Color, Color); - } -} - -public static class ShapeTransform -{ - public static void TransformShape(this Shape shape, ITransform transform, List vectors) - { - vectors.Clear(); - - int count = shape.Vertices.Count; - for (int i = 0; i < count; i++) - vectors.Add(transform.TransformVector2D(shape[i])); + shapeBatch.BorderCircle(Transform.Position.ToDisplayVector2(), 5f, Color.DarkRed); + + 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); } } diff --git a/Game/EngineConverter.cs b/Game/EngineConverter.cs index 6e5aed4..e3937c3 100644 --- a/Game/EngineConverter.cs +++ b/Game/EngineConverter.cs @@ -6,10 +6,14 @@ namespace Pong; public static class EngineConverter { + public readonly static Vector2D screenScale = Vector2D.Down + Vector2D.Right; + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static EngineTime ToEngineTime(this GameTime gameTime) => new(gameTime.TotalGameTime, gameTime.ElapsedGameTime); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2D ToVector2D(this Vector2 vector) => new(vector.X, vector.Y); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector2 ToVector2(this Vector2D vector) => new(vector.X, vector.Y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static Vector2 ToDisplayVector2(this Vector2D vector) => vector.Scale(screenScale).ToVector2(); } diff --git a/Game/Game1.cs b/Game/Game1.cs index 643fec5..08ea495 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -66,14 +66,26 @@ public class Game1 : Game gameManager.Camera = cameraBehaviour; - gameObjectDiamond = gameManager.InstantiateGameObject(); + IGameObject gameObjectDiamond = gameManager.InstantiateGameObject(); gameObjectDiamond.Name = "Diamond"; gameObjectDiamond.Transform.Position = new Vector2D(0f, 0f); gameObjectDiamond.Transform.Scale = new Vector2D(100f, 100f); gameObjectDiamond.BehaviourController.AddBehaviour(); gameObjectDiamond.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); + gameObjectDiamond.BehaviourController.AddBehaviour(); gameObjectDiamond.BehaviourController.AddBehaviour(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Zero, Vector2D.Left])); gameObjectDiamond.BehaviourController.AddBehaviour(); + + + IGameObject gameObjectShape = gameManager.InstantiateGameObject(); + gameObjectShape.Name = "Shape"; + gameObjectShape.Transform.Position = new Vector2D(250f, 0f); + gameObjectShape.Transform.Scale = new Vector2D(100f, 100f); + gameObjectShape.BehaviourController.AddBehaviour(); + gameObjectShape.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); + gameObjectShape.BehaviourController.AddBehaviour(); + gameObjectShape.BehaviourController.AddBehaviour(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Zero, Vector2D.Left])); + // gameObjectShape.BehaviourController.AddBehaviour(); } protected override void Update(GameTime gameTime) @@ -107,10 +119,6 @@ public class Game1 : Game cameraBehaviour.BehaviourController.GameObject.Transform.Rotation += gameTime.ElapsedGameTime.Nanoseconds * 0.000025f; if (Keyboard.GetState().IsKeyDown(Keys.E)) cameraBehaviour.BehaviourController.GameObject.Transform.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f; - if (Keyboard.GetState().IsKeyDown(Keys.NumPad4)) - gameObjectDiamond.Transform.Rotation += gameTime.ElapsedGameTime.Nanoseconds * 0.0025f; - if (Keyboard.GetState().IsKeyDown(Keys.NumPad6)) - gameObjectDiamond.Transform.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.0025f; if (Keyboard.GetState().IsKeyDown(Keys.N)) { @@ -163,7 +171,6 @@ public class Game1 : Game } static float physicsTimer = 0f; static float seconds = 0f; - private GameObject gameObjectDiamond; protected override void Draw(GameTime gameTime) {