From ffe4fc044efeedddfb513f8e3d14acc486a41a3d Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sat, 27 Jan 2024 21:50:28 +0300 Subject: [PATCH] feat: Basic Pong Setting --- Engine | 2 +- Game/Behaviours/CircleBehaviour.cs | 2 + Game/Behaviours/MovementBallBehaviour.cs | 19 +++- Game/Behaviours/MovementBoxBehaviour.cs | 4 +- Game/Behaviours/ShapeBehaviour.cs | 10 +- Game/Game1.cs | 130 ++++++++++++----------- 6 files changed, 93 insertions(+), 74 deletions(-) diff --git a/Engine b/Engine index 6a104d8..c1c1676 160000 --- a/Engine +++ b/Engine @@ -1 +1 @@ -Subproject commit 6a104d8abd013334ed2787c4b4c6fa6481e4c6a4 +Subproject commit c1c1676b9a4c822b0e8f8adf23262127d7a11fba diff --git a/Game/Behaviours/CircleBehaviour.cs b/Game/Behaviours/CircleBehaviour.cs index 6e107a7..cf75d83 100644 --- a/Game/Behaviours/CircleBehaviour.cs +++ b/Game/Behaviours/CircleBehaviour.cs @@ -18,6 +18,8 @@ public class CircleBehaviour : Syntriax.Engine.Physics2D.Collider2DCircleBehavio public void Draw(ShapeBatch shapeBatch) { + Recalculate(); + shapeBatch.BorderCircle(CircleWorld.Center.ToDisplayVector2(), CircleWorld.Radius, Color); } } diff --git a/Game/Behaviours/MovementBallBehaviour.cs b/Game/Behaviours/MovementBallBehaviour.cs index 063c79e..23224ba 100644 --- a/Game/Behaviours/MovementBallBehaviour.cs +++ b/Game/Behaviours/MovementBallBehaviour.cs @@ -3,6 +3,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; using Syntriax.Engine.Core; using Syntriax.Engine.Input; +using Syntriax.Engine.Physics2D; using Syntriax.Engine.Physics2D.Abstract; namespace Pong.Behaviours; @@ -11,12 +12,24 @@ public class MovementBallBehaviour(Vector2D StartDirection, float Speed) : Behav public Vector2D StartDirection { get; private set; } = Vector2D.Normalize(StartDirection); public float Speed { get; set; } = Speed; + private IRigidBody2D rigidBody = null!; + protected override void OnFirstActiveFrame() { - if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidBody)) - throw new Exception($"Where's my {nameof(IRigidBody2D)}????"); + if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? foundRigidBody)) + throw new Exception($"{nameof(IRigidBody2D)} is missing on {GameObject.Name}."); + if (!BehaviourController.TryGetBehaviour(out ICollider2D? foundCollider)) + throw new Exception($"{nameof(ICollider2D)} is missing on {GameObject.Name}."); - rigidBody.Velocity = StartDirection * Speed; + foundRigidBody.Velocity = StartDirection * Speed; + foundCollider.OnCollisionDetected += OnCollisionDetected; + + rigidBody = foundRigidBody; + } + + private void OnCollisionDetected(ICollider2D collider2D, CollisionDetectionInformation information) + { + rigidBody.Velocity = rigidBody.Velocity.Reflect(information.Normal); } // protected override void OnUpdate(GameTime time) diff --git a/Game/Behaviours/MovementBoxBehaviour.cs b/Game/Behaviours/MovementBoxBehaviour.cs index fd9468a..86ae8e1 100644 --- a/Game/Behaviours/MovementBoxBehaviour.cs +++ b/Game/Behaviours/MovementBoxBehaviour.cs @@ -35,9 +35,7 @@ public class MovementBoxBehaviour(Keys Up, Keys Down, float High, float Low, flo protected override void OnFirstActiveFrame() { if (!BehaviourController.TryGetBehaviour>(out var behaviourResult)) - throw new Exception($"{nameof(IButtonInputs)} is missing on ${GameObject.Name}."); - - inputs = behaviourResult; + inputs = behaviourResult ?? BehaviourController.AddBehaviour(); inputs.RegisterOnPress(Up, OnUpPressed); inputs.RegisterOnRelease(Up, OnUpReleased); diff --git a/Game/Behaviours/ShapeBehaviour.cs b/Game/Behaviours/ShapeBehaviour.cs index c27ad33..568042d 100644 --- a/Game/Behaviours/ShapeBehaviour.cs +++ b/Game/Behaviours/ShapeBehaviour.cs @@ -8,16 +8,18 @@ namespace Pong.Behaviours; public class ShapeBehaviour : Syntriax.Engine.Physics2D.Collider2DShapeBehaviour, IDisplayableShape { - 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) : base(shape) { } + public ShapeBehaviour(Shape shape, float thickness) : base(shape) { Thickness = thickness; } + public ShapeBehaviour(Shape shape, Color color) : base(shape) { Color = color; } + public ShapeBehaviour(Shape shape, Color color, float thickness) : base(shape) { Thickness = thickness; Color = color; } public Color Color { get; set; } = Color.White; public float Thickness { get; set; } = .5f; public void Draw(ShapeBatch shapeBatch) { + Recalculate(); + int count = ShapeWorld.Vertices.Count; shapeBatch.BorderCircle(Transform.Position.ToDisplayVector2(), 5f, Color.DarkRed); diff --git a/Game/Game1.cs b/Game/Game1.cs index 9515341..ecf61cd 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -10,13 +10,14 @@ using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D; using Syntriax.Engine.Physics2D.Primitives; +using Syntriax.Engine.Physics2D.Abstract; namespace Pong; public class Game1 : Game { private GraphicsDeviceManager _graphics = null!; - private PhysicsEngine2D engine; + private IPhysicsEngine2D engine = null!; private SpriteBatch _spriteBatch = null!; private ShapeBatch _shapeBatch = null!; public static GameManager gameManager = null!; @@ -26,7 +27,10 @@ public class Game1 : Game public Game1() { - engine = new PhysicsEngine2D(); + engine = new PhysicsEngine2D + { + IterationCount = 3 + }; _graphics = new GraphicsDeviceManager(this) { @@ -65,72 +69,73 @@ public class Game1 : Game cameraBehaviour.Viewport = GraphicsDevice.Viewport; gameManager.Camera = cameraBehaviour; + IGameObject gameObjectBall = gameManager.InstantiateGameObject(); + gameObjectBall.Name = "Ball"; + gameObjectBall.Transform.Position = new Vector2D(0, 0f); + gameObjectBall.Transform.Scale = new Vector2D(10f, 10f); + gameObjectBall.BehaviourController.AddBehaviour(Vector2D.One, 500f); + gameObjectBall.BehaviourController.AddBehaviour(new Circle(Vector2D.Zero, 1f)); + engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour()); - // IGameObject gameObjectCircle = gameManager.InstantiateGameObject(); - // gameObjectCircle.Name = "Circle"; - // gameObjectCircle.Transform.Position = new Vector2D(0f, -50f); - // gameObjectCircle.Transform.Scale = new Vector2D(25f, 25f); - // gameObjectCircle.BehaviourController.AddBehaviour(); - // gameObjectCircle.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); - // gameObjectCircle.BehaviourController.AddBehaviour(new Circle(Vector2D.Zero, 1f)); - // engine.AddRigidBody(gameObjectCircle.BehaviourController.AddBehaviour()); + //////////////////////////////////////////////////////////////////////////////////// - IGameObject gameObjectCircle2 = gameManager.InstantiateGameObject(); - gameObjectCircle2.Name = "Circle2"; - gameObjectCircle2.Transform.Position = new Vector2D(5f, 50f); - gameObjectCircle2.Transform.Scale = new Vector2D(25f, 25f); - gameObjectCircle2.BehaviourController.AddBehaviour(); - gameObjectCircle2.BehaviourController.AddBehaviour(new Circle(Vector2D.Zero, 1f)); - engine.AddRigidBody(gameObjectCircle2.BehaviourController.AddBehaviour()); + IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject(); + gameObjectLeftPaddle.Name = "Left Paddle"; + gameObjectLeftPaddle.Transform.Position = new Vector2D(-468f, 0f); + gameObjectLeftPaddle.Transform.Scale = new Vector2D(15f, 60f); + gameObjectLeftPaddle.BehaviourController.AddBehaviour(Keys.W, Keys.S, 228f, -228f, 400f); + gameObjectLeftPaddle.BehaviourController.AddBehaviour(Shape.Box); + RigidBody2D rigidBodyLeftPaddle = gameObjectLeftPaddle.BehaviourController.AddBehaviour(); + rigidBodyLeftPaddle.IsStatic = true; + engine.AddRigidBody(rigidBodyLeftPaddle); - IGameObject gameObjectDiamond = gameManager.InstantiateGameObject(); - gameObjectDiamond.Name = "Diamond"; - gameObjectDiamond.Transform.Position = new Vector2D(-150f, -150f); - gameObjectDiamond.Transform.Scale = new Vector2D(50f, 50f); - 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.One, Vector2D.Left])); - gameObjectDiamond.BehaviourController.AddBehaviour(); - engine.AddRigidBody(gameObjectDiamond.BehaviourController.AddBehaviour()); + IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject(); + gameObjectRightPaddle.Name = "Right Paddle"; + gameObjectRightPaddle.Transform.Position = new Vector2D(468f, 0f); + gameObjectRightPaddle.Transform.Scale = new Vector2D(15f, 60f); + gameObjectRightPaddle.BehaviourController.AddBehaviour(Keys.Up, Keys.Down, 228f, -228f, 400f); + gameObjectRightPaddle.BehaviourController.AddBehaviour(Shape.Box); + RigidBody2D rigidBodyRightPaddle = gameObjectRightPaddle.BehaviourController.AddBehaviour(); + rigidBodyRightPaddle.IsStatic = true; + engine.AddRigidBody(rigidBodyRightPaddle); - IGameObject gameObjectBox = gameManager.InstantiateGameObject(); - gameObjectBox.Name = "Box"; - gameObjectBox.Transform.Position = new Vector2D(150f, -150f); - gameObjectBox.Transform.Scale = new Vector2D(100f, 100f); - gameObjectBox.BehaviourController.AddBehaviour(); - gameObjectBox.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); - gameObjectBox.BehaviourController.AddBehaviour(); - gameObjectBox.BehaviourController.AddBehaviour(Shape.Pentagon); - gameObjectBox.BehaviourController.AddBehaviour(); - engine.AddRigidBody(gameObjectBox.BehaviourController.AddBehaviour()); + //////////////////////////////////////////////////////////////////////////////////// - for (int i = 3; i < 10; i++) - { - IGameObject Test = gameManager.InstantiateGameObject(); - Test.Name = i.ToString(); - Test.Transform.Position = new Vector2D((i - 6) * 150, 0f); - Test.Transform.Scale = new Vector2D(75f, 75f); - Test.BehaviourController.AddBehaviour(); - Test.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); - Test.BehaviourController.AddBehaviour(); - Test.BehaviourController.AddBehaviour(Shape.CreateNgon(i)); - Test.BehaviourController.AddBehaviour(); - RigidBody2D rigidBody = Test.BehaviourController.AddBehaviour(); - rigidBody.AngularVelocity = 90f; - engine.AddRigidBody(rigidBody); - } + IGameObject gameObjectWallTop = gameManager.InstantiateGameObject(); + gameObjectWallTop.Name = "WallTop"; + gameObjectWallTop.Transform.Position = new Vector2D(0f, 298f); + gameObjectWallTop.Transform.Scale = new Vector2D(542f, 20f); + gameObjectWallTop.BehaviourController.AddBehaviour(Shape.Box); + RigidBody2D rigidBodyWallTop = gameObjectWallTop.BehaviourController.AddBehaviour(); + rigidBodyWallTop.IsStatic = true; + engine.AddRigidBody(rigidBodyWallTop); + IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject(); + gameObjectWallBottom.Name = "WallBottom"; + gameObjectWallBottom.Transform.Position = new Vector2D(0f, -298f); + gameObjectWallBottom.Transform.Scale = new Vector2D(542f, 20f); + gameObjectWallBottom.BehaviourController.AddBehaviour(Shape.Box); + RigidBody2D rigidBodyWallBottom = gameObjectWallBottom.BehaviourController.AddBehaviour(); + rigidBodyWallBottom.IsStatic = true; + engine.AddRigidBody(rigidBodyWallBottom); - // 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(); + IGameObject gameObjectWallRight = gameManager.InstantiateGameObject(); + gameObjectWallRight.Name = "WallRight"; + gameObjectWallRight.Transform.Position = new Vector2D(522f, 0f); + gameObjectWallRight.Transform.Scale = new Vector2D(20f, 318f); + gameObjectWallRight.BehaviourController.AddBehaviour(Shape.Box); + RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour(); + rigidBodyWallRight.IsStatic = true; + engine.AddRigidBody(rigidBodyWallRight); + + IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject(); + gameObjectWallLeft.Name = "WallLeft"; + gameObjectWallLeft.Transform.Position = new Vector2D(-522f, 0f); + gameObjectWallLeft.Transform.Scale = new Vector2D(20f, 318f); + gameObjectWallLeft.BehaviourController.AddBehaviour(Shape.Box); + RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour(); + rigidBodyWallLeft.IsStatic = true; + engine.AddRigidBody(rigidBodyWallLeft); } protected override void Update(GameTime gameTime) @@ -204,14 +209,13 @@ public class Game1 : Game } } - + gameManager.Update(gameTime.ToEngineTime()); while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds) { // Console.WriteLine($"Physics Timer: {physicsTimer}"); physicsTimer += 0.01f; engine.Step(.01f); } - gameManager.Update(gameTime.ToEngineTime()); base.Update(gameTime); } static float physicsTimer = 0f;