diff --git a/Game/Behaviours/MovementBallBehaviour.cs b/Game/Behaviours/MovementBallBehaviour.cs index b7cd436..d6c6b2a 100644 --- a/Game/Behaviours/MovementBallBehaviour.cs +++ b/Game/Behaviours/MovementBallBehaviour.cs @@ -3,43 +3,49 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; using Syntriax.Engine.Core; using Syntriax.Engine.Input; +using Syntriax.Engine.Physics2D.Abstract; namespace Pong.Behaviours; -public class MovementBallBehaviour(Vector2 StartDirection, PlayAreaBehaviour PlayAreaBehaviour, float Speed) : BehaviourOverride +public class MovementBallBehaviour(Vector2 StartDirection, float Speed) : BehaviourOverride { - public Vector2 StartDirection { get; private set; } = StartDirection; - public PlayAreaBehaviour PlayAreaBehaviour { get; } = PlayAreaBehaviour; + public Vector2 StartDirection { get; private set; } = Vector2.Normalize(StartDirection); public float Speed { get; set; } = Speed; - protected override void OnInitialize() => StartDirection.Normalize(); - - protected override void OnUpdate(GameTime time) + protected override void OnFirstActiveFrame(GameTime time) { - GameObject.Transform.Position += StartDirection * (time.ElapsedGameTime.Nanoseconds * .001f) * Speed; + if (!BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidBody)) + throw new Exception($"Where's my {nameof(IRigidBody2D)}????"); - float absY = MathF.Abs(GameObject.Transform.Position.Y); - float differenceY = absY - PlayAreaBehaviour.PlayArea.Y * 0.5f; - if (differenceY > 0f) - { - if (GameObject.Transform.Position.Y > 0f) - GameObject.Transform.Position -= Vector2.UnitY * differenceY * 2f; - else - GameObject.Transform.Position += Vector2.UnitY * differenceY * 2f; - - StartDirection = new(StartDirection.X, -StartDirection.Y); - } - - float absX = MathF.Abs(GameObject.Transform.Position.X); - float differenceX = absX - PlayAreaBehaviour.PlayArea.X * 0.5f; - if (differenceX > 0f) - { - if (GameObject.Transform.Position.X > 0f) - GameObject.Transform.Position -= Vector2.UnitX * differenceX * 2f; - else - GameObject.Transform.Position += Vector2.UnitX * differenceX * 2f; - - StartDirection = new(-StartDirection.X, StartDirection.Y); - } + rigidBody.Velocity = StartDirection * Speed; } + + // protected override void OnUpdate(GameTime time) + // { + // GameObject.Transform.Position += StartDirection * (time.ElapsedGameTime.Nanoseconds * .001f) * Speed; + + // float absY = MathF.Abs(GameObject.Transform.Position.Y); + // float differenceY = absY - PlayAreaBehaviour.PlayArea.Y * 0.5f; + // if (differenceY > 0f) + // { + // if (GameObject.Transform.Position.Y > 0f) + // GameObject.Transform.Position -= Vector2.UnitY * differenceY * 2f; + // else + // GameObject.Transform.Position += Vector2.UnitY * differenceY * 2f; + + // StartDirection = new(StartDirection.X, -StartDirection.Y); + // } + + // float absX = MathF.Abs(GameObject.Transform.Position.X); + // float differenceX = absX - PlayAreaBehaviour.PlayArea.X * 0.5f; + // if (differenceX > 0f) + // { + // if (GameObject.Transform.Position.X > 0f) + // GameObject.Transform.Position -= Vector2.UnitX * differenceX * 2f; + // else + // GameObject.Transform.Position += Vector2.UnitX * differenceX * 2f; + + // StartDirection = new(-StartDirection.X, StartDirection.Y); + // } + // } } diff --git a/Game/Game1.cs b/Game/Game1.cs index 3c82bca..9cb06d1 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -45,7 +45,7 @@ public class Game1 : Game engine = new PhysicsEngine2D(); _spriteBatch = new SpriteBatch(GraphicsDevice); - // Sprite spriteBox = new Sprite() { Texture2D = Content.Load("Sprites/Pixel") }; + Sprite spriteBox = new Sprite() { Texture2D = Content.Load("Sprites/Pixel") }; Sprite spriteBall = new Sprite() { Texture2D = Content.Load("Sprites/Circle") }; IGameObject gameObjectCamera = gameManager.InstantiateGameObject(); @@ -55,52 +55,58 @@ public class Game1 : Game gameManager.Camera = gameObjectCamera.BehaviourController.AddBehaviour(); gameManager.Camera.Viewport = GraphicsDevice.Viewport; - // IGameObject gameObjectPlayArea = gameManager.InstantiateGameObject(); - // PlayAreaBehaviour playAreaBehaviour = gameObjectPlayArea.BehaviourController.AddBehaviour(); - // playAreaBehaviour.PlayArea = new Vector2(_graphics.PreferredBackBufferWidth, _graphics.PreferredBackBufferHeight); - IGameObject gameObjectBall = gameManager.InstantiateGameObject(); gameObjectBall.Name = "Ball"; gameObjectBall.Transform.Position = Vector2.Zero; gameObjectBall.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f); engine.AddRigidBody(gameObjectBall.BehaviourController.AddBehaviour()); gameObjectBall.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(1, 1) * 5f, new Vector2(-1, 1) * 5f, new Vector2(1, -1) * 5f, new Vector2(-1, -1) * 5f]).OffsetScale = Vector2.One * 51.2f; - // gameObjectBall.BehaviourController.AddBehaviour(new Vector2(.1f, .1f), playAreaBehaviour, 100f); + gameObjectBall.BehaviourController.AddBehaviour(new Vector2(.1f, .1f), 500f); gameObjectBall.BehaviourController.AddBehaviour().Assign(spriteBall); - IGameObject gameObjectBall2 = gameManager.InstantiateGameObject(); - gameObjectBall2.Name = "Ball2"; - gameObjectBall2.Transform.Position = Vector2.UnitY * -50f; - gameObjectBall2.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f); - RigidBody2D rigidBody = gameObjectBall2.BehaviourController.AddBehaviour(); - rigidBody.Velocity = Vector2.UnitY * 100f; - engine.AddRigidBody(rigidBody); - gameObjectBall2.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(1, 1) * 5f, new Vector2(-1, 1) * 5f, new Vector2(1, -1) * 5f, new Vector2(-1, -1) * 5f]).OffsetScale = Vector2.One * 51.2f; - // gameObjectBall2.BehaviourController.AddBehaviour(new Vector2(.1f, .1f), playAreaBehaviour, 100f); - gameObjectBall2.BehaviourController.AddBehaviour().Assign(spriteBall); - IGameObject gameObjectBall3 = gameManager.InstantiateGameObject(); - gameObjectBall3.Name = "Ball"; - gameObjectBall3.Transform.Position = Vector2.UnitY * -120f + Vector2.UnitX * 10f; - gameObjectBall3.Transform.Scale = new Vector2(1f / 51.2f, 1f / 51.2f); - engine.AddRigidBody(gameObjectBall3.BehaviourController.AddBehaviour()); - gameObjectBall3.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(1, 1) * 5f, new Vector2(-1, 1) * 5f, new Vector2(1, -1) * 5f, new Vector2(-1, -1) * 5f]).OffsetScale = Vector2.One * 51.2f; - // gameObjectBall3.BehaviourController.AddBehaviour(new Vector2(.1f, .1f), playAreaBehaviour, 100f); - gameObjectBall3.BehaviourController.AddBehaviour().Assign(spriteBall); - // IGameObject gameObjectLeft = gameManager.InstantiateGameObject(); - // gameObjectLeft.Name = "Left"; - // gameObjectLeft.Transform.Position = new Vector2(-452, 0f); - // gameObjectLeft.Transform.Scale = new Vector2(10f, 40f); - // gameObjectLeft.BehaviourController.AddBehaviour(); - // gameObjectLeft.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); - // gameObjectLeft.BehaviourController.AddBehaviour().Assign(spriteBox); + IGameObject gameObjectLeft = gameManager.InstantiateGameObject(); + gameObjectLeft.Name = "Left"; + gameObjectLeft.Transform.Position = new Vector2(-452, 0f); + gameObjectLeft.Transform.Scale = new Vector2(10f, 40f); + gameObjectLeft.BehaviourController.AddBehaviour(); + gameObjectLeft.BehaviourController.AddBehaviour(Keys.W, Keys.S, 268f, -268f, 400f); + gameObjectLeft.BehaviourController.AddBehaviour().Assign(spriteBox); + gameObjectLeft.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); + engine.AddRigidBody(gameObjectLeft.BehaviourController.AddBehaviour()); + + IGameObject gameObjectRight = gameManager.InstantiateGameObject(); + gameObjectRight.Name = "Right"; + gameObjectRight.Transform.Position = new Vector2(452, 0f); + gameObjectRight.Transform.Scale = new Vector2(10f, 40f); + gameObjectRight.BehaviourController.AddBehaviour(); + gameObjectRight.BehaviourController.AddBehaviour(Keys.Up, Keys.Down, 268f, -268f, 400f); + gameObjectRight.BehaviourController.AddBehaviour().Assign(spriteBox); + gameObjectRight.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); + engine.AddRigidBody(gameObjectRight.BehaviourController.AddBehaviour()); + + + IGameObject goPlayAreaTop = gameManager.InstantiateGameObject(); + goPlayAreaTop.Transform.Position = new Vector2(0f, 288f + 20f); + goPlayAreaTop.Transform.Scale = new Vector2(1024f, 40f); + goPlayAreaTop.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); + engine.AddRigidBody(goPlayAreaTop.BehaviourController.AddBehaviour()); + IGameObject goPlayAreaBottom = gameManager.InstantiateGameObject(); + goPlayAreaBottom.Transform.Position = new Vector2(0f, -(288f + 20f)); + goPlayAreaBottom.Transform.Scale = new Vector2(1024f, 40f); + goPlayAreaBottom.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); + engine.AddRigidBody(goPlayAreaBottom.BehaviourController.AddBehaviour()); + + IGameObject goPlayAreaRight = gameManager.InstantiateGameObject(); + goPlayAreaRight.Transform.Position = new Vector2(512f + 20f, 0f); + goPlayAreaRight.Transform.Scale = new Vector2(40f, 576f); + goPlayAreaRight.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); + engine.AddRigidBody(goPlayAreaRight.BehaviourController.AddBehaviour()); + IGameObject goPlayAreaLeft = gameManager.InstantiateGameObject(); + goPlayAreaLeft.Transform.Position = new Vector2(-(512f + 20f), 0f); + goPlayAreaLeft.Transform.Scale = new Vector2(40f, 576f); + goPlayAreaLeft.BehaviourController.AddBehaviour((System.Collections.Generic.IList)[new Vector2(.5f, .5f), new Vector2(-.5f, .5f), new Vector2(.5f, -.5f), new Vector2(-.5f, -.5f)]); + engine.AddRigidBody(goPlayAreaLeft.BehaviourController.AddBehaviour()); - // IGameObject gameObjectRight = gameManager.InstantiateGameObject(); - // gameObjectRight.Name = "Right"; - // gameObjectRight.Transform.Position = new Vector2(452, 0f); - // gameObjectRight.Transform.Scale = new Vector2(10f, 40f); - // gameObjectRight.BehaviourController.AddBehaviour(); - // gameObjectRight.BehaviourController.AddBehaviour(Keys.Up, Keys.Down, 268f, -268f, 400f); - // gameObjectRight.BehaviourController.AddBehaviour().Assign(spriteBox); // TODO: use this.Content to load your game content here } diff --git a/Game/Physics2D/PhysicsEngine2D.cs b/Game/Physics2D/PhysicsEngine2D.cs index c5a6f72..83f50f4 100644 --- a/Game/Physics2D/PhysicsEngine2D.cs +++ b/Game/Physics2D/PhysicsEngine2D.cs @@ -63,9 +63,9 @@ public class PhysicsEngine2D : IPhysicsEngine2D continue; if (colliders[colliderIX].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidX)) - rigidX.Velocity = -normal * rigidX.Velocity.Length(); + rigidX.Velocity = Vector2.Reflect(rigidX.Velocity, normal); if (colliders[colliderIY].BehaviourController.TryGetBehaviour(out IRigidBody2D? rigidY)) - rigidY.Velocity = normal * rigidY.Velocity.Length(); + rigidY.Velocity = Vector2.Reflect(rigidY.Velocity, normal); // Console.WriteLine($"Collision"); break;