From 04653711e9151ee3ba5c4ecaedee257e2d05c318 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Wed, 24 Jan 2024 12:17:21 +0300 Subject: [PATCH] feat: Apo.Shapes Added --- Engine | 2 +- Game/Behaviours/IDisplayableShape.cs | 8 ++ Game/Behaviours/ShapeBehaviour.cs | 54 ++++++++++++++ Game/Game.csproj | 2 + Game/Game.sln | 18 ++--- Game/Game1.cs | 106 +++++++-------------------- 6 files changed, 98 insertions(+), 92 deletions(-) create mode 100644 Game/Behaviours/IDisplayableShape.cs create mode 100644 Game/Behaviours/ShapeBehaviour.cs diff --git a/Engine b/Engine index 5ed7ccd..e5732f0 160000 --- a/Engine +++ b/Engine @@ -1 +1 @@ -Subproject commit 5ed7ccdded04e075dca04ef93f3c75aefa338566 +Subproject commit e5732f0ac57886f1b127e154d52b6a9655d7bf85 diff --git a/Game/Behaviours/IDisplayableShape.cs b/Game/Behaviours/IDisplayableShape.cs new file mode 100644 index 0000000..0949f13 --- /dev/null +++ b/Game/Behaviours/IDisplayableShape.cs @@ -0,0 +1,8 @@ +using Apos.Shapes; + +namespace Pong.Behaviours; + +public interface IDisplayableShape +{ + void Draw(ShapeBatch shapeBatch); +} diff --git a/Game/Behaviours/ShapeBehaviour.cs b/Game/Behaviours/ShapeBehaviour.cs new file mode 100644 index 0000000..4771049 --- /dev/null +++ b/Game/Behaviours/ShapeBehaviour.cs @@ -0,0 +1,54 @@ +using System.Collections.Generic; + +using Microsoft.Xna.Framework; + +using Apos.Shapes; + +using Engine.Physics2D; + +using Syntriax.Engine.Core; +using Syntriax.Engine.Core.Abstract; +using Syntriax.Engine.Physics2D.Primitives; + +namespace Pong.Behaviours; + +public class ShapeBehaviour : BehaviourOverride, IDisplayableShape +{ + private readonly List vectors = []; + + 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 - 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); + } +} + +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 + ( + shape[i] + .Scale(transform.Scale) + .Rotate(transform.Rotation * Physics2D.DegreeToRadian) + .Add(transform.Position) + ); + } +} diff --git a/Game/Game.csproj b/Game/Game.csproj index 323a3f6..f15750f 100644 --- a/Game/Game.csproj +++ b/Game/Game.csproj @@ -20,12 +20,14 @@ + + diff --git a/Game/Game.sln b/Game/Game.sln index 58640a0..47a229d 100644 --- a/Game/Game.sln +++ b/Game/Game.sln @@ -3,9 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.5.002.0 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Game", "Game.csproj", "{42644486-9F9E-4242-B6C4-AF31BBFA31D2}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Engine.Core", "..\Engine\Engine.Core\Engine.Core.csproj", "{EF1FE4A2-40DF-4967-8003-CF6D98010D02}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Game", "Game.csproj", "{5C2459D3-9CB7-4778-BBDC-4F5B8298AACD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -13,19 +11,15 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {42644486-9F9E-4242-B6C4-AF31BBFA31D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {42644486-9F9E-4242-B6C4-AF31BBFA31D2}.Debug|Any CPU.Build.0 = Debug|Any CPU - {42644486-9F9E-4242-B6C4-AF31BBFA31D2}.Release|Any CPU.ActiveCfg = Release|Any CPU - {42644486-9F9E-4242-B6C4-AF31BBFA31D2}.Release|Any CPU.Build.0 = Release|Any CPU - {EF1FE4A2-40DF-4967-8003-CF6D98010D02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EF1FE4A2-40DF-4967-8003-CF6D98010D02}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EF1FE4A2-40DF-4967-8003-CF6D98010D02}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EF1FE4A2-40DF-4967-8003-CF6D98010D02}.Release|Any CPU.Build.0 = Release|Any CPU + {5C2459D3-9CB7-4778-BBDC-4F5B8298AACD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5C2459D3-9CB7-4778-BBDC-4F5B8298AACD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5C2459D3-9CB7-4778-BBDC-4F5B8298AACD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5C2459D3-9CB7-4778-BBDC-4F5B8298AACD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {AB3225EE-1621-439F-8F83-DF4515922FEF} + SolutionGuid = {5C7F09EA-E22F-4BDB-A0F1-1F295EBCE4A0} EndGlobalSection EndGlobal diff --git a/Game/Game1.cs b/Game/Game1.cs index 5056882..42b3fd2 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -4,10 +4,10 @@ using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Pong.Behaviours; +using Apos.Shapes; using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; -using Syntriax.Engine.Graphics.TwoDimensional; using Syntriax.Engine.Physics2D; using Syntriax.Engine.Physics2D.Primitives; @@ -18,6 +18,7 @@ public class Game1 : Game private GraphicsDeviceManager _graphics = null!; private PhysicsEngine2D engine; private SpriteBatch _spriteBatch = null!; + private ShapeBatch _shapeBatch = null!; public static GameManager gameManager = null!; public static Sprite spriteBox = null!; private MonoGameCameraBehaviour cameraBehaviour = null!; @@ -30,7 +31,8 @@ public class Game1 : Game _graphics = new GraphicsDeviceManager(this) { PreferredBackBufferWidth = 1024, - PreferredBackBufferHeight = 576 + PreferredBackBufferHeight = 576, + GraphicsProfile = GraphicsProfile.HiDef }; Content.RootDirectory = "Content"; @@ -50,9 +52,10 @@ public class Game1 : Game protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); + _shapeBatch = new ShapeBatch(GraphicsDevice, Content); - spriteBox = new Sprite() { Texture2D = Content.Load("Sprites/Pixel") }; - Sprite spriteBall = new Sprite() { Texture2D = Content.Load("Sprites/Circle") }; + // spriteBox = new Sprite() { Texture2D = Content.Load("Sprites/Pixel") }; + // Sprite spriteBall = new Sprite() { Texture2D = Content.Load("Sprites/Circle") }; IGameObject gameObjectCamera = gameManager.InstantiateGameObject(); gameObjectCamera.Name = "Camera"; @@ -62,76 +65,14 @@ public class Game1 : Game cameraBehaviour.Viewport = GraphicsDevice.Viewport; gameManager.Camera = cameraBehaviour; - GameObject gameObjectBall = gameManager.InstantiateGameObject(); - gameObjectBall.Name = "Ball"; - gameObjectBall.Transform.Position = Vector2D.Zero; - gameObjectBall.Transform.Scale = new Vector2D(1f / 51.2f, 1f / 51.2f); - engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour()); - gameObjectBall.BehaviourController.AddBehaviour(new Vector2D(.1f, .1f), 500f); - gameObjectBall.BehaviourController.AddBehaviour().Assign(spriteBall); - gameObjectBall.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2D.One * 512f * .5f, Vector2D.One * 512f * .5f); - // gameObjectBall = gameManager.InstantiateGameObject(); - // gameObjectBall.Name = "Ball"; - // gameObjectBall.Transform.Position = Vector2D.UnitY * 30f; - // gameObjectBall.Transform.Scale = new Vector2D(1f / 51.2f, 1f / 51.2f); - // engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour()); - // gameObjectBall.BehaviourController.AddBehaviour(new Vector2D(.1f, .01f), 500f); - // gameObjectBall.BehaviourController.AddBehaviour().Assign(spriteBall); - // gameObjectBall.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2D.One * 512f * .5f, Vector2D.One * 512f * .5f); - // IGameObject gameObjectLeft = gameManager.InstantiateGameObject(); - // gameObjectLeft.Name = "Left"; - // gameObjectLeft.Transform.Position = new Vector2D(-452, 0f); - // gameObjectLeft.Transform.Scale = new Vector2D(10f, 40f); - // gameObjectLeft.BehaviourController.AddBehaviour(); - // gameObjectLeft.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); - // gameObjectLeft.BehaviourController.AddBehaviour().Assign(spriteBox); - // gameObjectLeft.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f); - // engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour()); - - // IGameObject gameObjectRight = gameManager.InstantiateGameObject(); - // gameObjectRight.Name = "Right"; - // gameObjectRight.Transform.Position = new Vector2D(452, 0f); - // gameObjectRight.Transform.Scale = new Vector2D(10f, 40f); - // gameObjectRight.BehaviourController.AddBehaviour(); - // gameObjectRight.BehaviourController.AddBehaviour(Keys.Up, Keys.Down, 268f, -268f, 400f); - // gameObjectRight.BehaviourController.AddBehaviour().Assign(spriteBox); - // gameObjectRight.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f); - // engine.AddRigidBody(gameObjectRight.BehaviourController.AddBehaviour()); - - - IGameObject goPlayAreaTop = gameManager.InstantiateGameObject(); - goPlayAreaTop.Transform.Position = new Vector2D(0f, 288f + 20f); - goPlayAreaTop.Transform.Scale = new Vector2D(10240f, 40f); - goPlayAreaTop.BehaviourController.AddBehaviour().Assign(spriteBox); - goPlayAreaTop.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f); - engine.AddRigidBody(goPlayAreaTop.BehaviourController.AddBehaviour()); - IGameObject goPlayAreaBottom = gameManager.InstantiateGameObject(); - goPlayAreaBottom.Transform.Position = new Vector2D(0f, -(288f + 20f)); - goPlayAreaBottom.Transform.Scale = new Vector2D(10240f, 40f); - goPlayAreaBottom.BehaviourController.AddBehaviour().Assign(spriteBox); - goPlayAreaBottom.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f); - engine.AddRigidBody(goPlayAreaBottom.BehaviourController.AddBehaviour()); - - IGameObject goPlayAreaRight = gameManager.InstantiateGameObject(); - goPlayAreaRight.Transform.Position = new Vector2D(512f + 20f, 0f); - goPlayAreaRight.Transform.Scale = new Vector2D(40f, 5760f); - goPlayAreaRight.BehaviourController.AddBehaviour().Assign(spriteBox); - goPlayAreaRight.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f); - engine.AddRigidBody(goPlayAreaRight.BehaviourController.AddBehaviour()); - IGameObject goPlayAreaLeft = gameManager.InstantiateGameObject(); - goPlayAreaLeft.Transform.Position = new Vector2D(-(512f + 20f), 0f); - goPlayAreaLeft.Transform.Scale = new Vector2D(40f, 5760f); - goPlayAreaLeft.BehaviourController.AddBehaviour().Assign(spriteBox); - goPlayAreaLeft.BehaviourController.AddBehaviour().AABBLocal = new AABB(-Vector2D.One * .5f, Vector2D.One * .5f); - engine.AddRigidBody(goPlayAreaLeft.BehaviourController.AddBehaviour()); - - // IGameObject goPlayAreaCenter = gameManager.InstantiateGameObject(); - // goPlayAreaCenter.Transform.Position = new Vector2D(100f, 100f); - // goPlayAreaCenter.Transform.Scale = new Vector2D(40f, 40f); - // // goPlayAreaCenter.BehaviourController.AddBehaviour().Assign(spriteBox); - // engine.AddRigidBody(goPlayAreaCenter.BehaviourController.AddBehaviour()); - // TODO: use this.Content to load your game content here + 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(new Shape([Vector2D.Up, Vector2D.One, Vector2D.Right, Vector2D.Down, Vector2D.Left])); } protected override void Update(GameTime gameTime) @@ -165,6 +106,10 @@ 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)) { @@ -217,6 +162,7 @@ public class Game1 : Game } static float physicsTimer = 0f; static float seconds = 0f; + private GameObject gameObjectDiamond; protected override void Draw(GameTime gameTime) { @@ -231,14 +177,16 @@ public class Game1 : Game _spriteBatch.Begin(SpriteSortMode.Deferred, transformMatrix: cameraBehaviour.MatrixTransform); foreach (IGameObject gameObject in gameManager) - { - if (!gameObject.BehaviourController.TryGetBehaviour(out IDisplayable? displayable)) - continue; - - displayable.Draw(_spriteBatch); - } + if (gameObject.BehaviourController.TryGetBehaviour(out IDisplayable? displayable)) + displayable.Draw(_spriteBatch); _spriteBatch.End(); + _shapeBatch.Begin(cameraBehaviour.MatrixTransform); + foreach (IGameObject gameObject in gameManager) + if (gameObject.BehaviourController.TryGetBehaviour(out IDisplayableShape? displayableShape)) + displayableShape.Draw(_shapeBatch); + _shapeBatch.End(); + base.Draw(gameTime); } }