diff --git a/Game/Game1.cs b/Game/Game1.cs index ecf61cd..5ea16da 100644 --- a/Game/Game1.cs +++ b/Game/Game1.cs @@ -23,7 +23,7 @@ public class Game1 : Game public static GameManager gameManager = null!; public static Sprite spriteBox = null!; private MonoGameCameraBehaviour cameraBehaviour = null!; - + private PongScoreboard pongScoreboard; public Game1() { @@ -57,6 +57,7 @@ public class Game1 : Game { _spriteBatch = new SpriteBatch(GraphicsDevice); _shapeBatch = new ShapeBatch(GraphicsDevice, Content); + SpriteFont spriteFont = Content.Load("UbuntuMono"); // spriteBox = new Sprite() { Texture2D = Content.Load("Sprites/Pixel") }; // Sprite spriteBall = new Sprite() { Texture2D = Content.Load("Sprites/Circle") }; @@ -69,13 +70,21 @@ public class Game1 : Game cameraBehaviour.Viewport = GraphicsDevice.Viewport; gameManager.Camera = cameraBehaviour; + //////////////////////////////////////////////////////////////////////////////////// + IGameObject gameObjectPongManager = gameManager.InstantiateGameObject(); + gameObjectPongManager.Name = "Pong Game Manager"; + pongScoreboard = gameObjectPongManager.BehaviourController.AddBehaviour(5); + + //////////////////////////////////////////////////////////////////////////////////// + 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()); + MovementBallBehaviour movementBallBehaviour = gameObjectBall.BehaviourController.AddBehaviour(); + RigidBody2D rigidBodyBall = gameObjectBall.BehaviourController.AddBehaviour(); + engine.AddRigidBody(rigidBodyBall); //////////////////////////////////////////////////////////////////////////////////// @@ -123,6 +132,7 @@ public class Game1 : Game gameObjectWallRight.Name = "WallRight"; gameObjectWallRight.Transform.Position = new Vector2D(522f, 0f); gameObjectWallRight.Transform.Scale = new Vector2D(20f, 318f); + gameObjectWallRight.BehaviourController.AddBehaviour((Action)pongScoreboard.ScoreToLeft); gameObjectWallRight.BehaviourController.AddBehaviour(Shape.Box); RigidBody2D rigidBodyWallRight = gameObjectWallRight.BehaviourController.AddBehaviour(); rigidBodyWallRight.IsStatic = true; @@ -132,10 +142,49 @@ public class Game1 : Game gameObjectWallLeft.Name = "WallLeft"; gameObjectWallLeft.Transform.Position = new Vector2D(-522f, 0f); gameObjectWallLeft.Transform.Scale = new Vector2D(20f, 318f); + gameObjectWallLeft.BehaviourController.AddBehaviour((Action)pongScoreboard.ScoreToRight); gameObjectWallLeft.BehaviourController.AddBehaviour(Shape.Box); RigidBody2D rigidBodyWallLeft = gameObjectWallLeft.BehaviourController.AddBehaviour(); rigidBodyWallLeft.IsStatic = true; engine.AddRigidBody(rigidBodyWallLeft); + + //////////////////////////////////////////////////////////////////////////////////// + /// + /// + IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject(); + gameObjectLeftScoreText.Name = "Score Left"; + gameObjectLeftScoreText.Transform.Position = new Vector2D(-250f, 250f); + gameObjectLeftScoreText.Transform.Scale = Vector2D.One * .25f; + TextBehaviour textBehaviourLeft = gameObjectLeftScoreText.BehaviourController.AddBehaviour(); + textBehaviourLeft.Font = spriteFont; + textBehaviourLeft.Text = "0"; + IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject(); + gameObjectRightScoreText.Name = "Score Right"; + gameObjectRightScoreText.Transform.Position = new Vector2D(250f, 250f); + gameObjectRightScoreText.Transform.Scale = Vector2D.One * .25f; + TextBehaviour textBehaviourRight = gameObjectRightScoreText.BehaviourController.AddBehaviour(); + textBehaviourRight.Font = spriteFont; + textBehaviourRight.Text = "0"; + + pongScoreboard.OnScored += () => + { + gameObjectBall.Transform.Position = Vector2D.Zero; + textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString(); + textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString(); + }; + pongScoreboard.OnFinished += () => + { + rigidBodyBall.BehaviourController.GameObject.Transform.Position = Vector2D.Zero; + rigidBodyBall.Velocity = Vector2D.Zero; + }; + + pongScoreboard.OnReset += () => + { + gameObjectBall.StateEnable.Enabled = true; + rigidBodyBall.Velocity = Vector2D.One * movementBallBehaviour.Speed; + textBehaviourLeft.Text = pongScoreboard.ScoreLeft.ToString(); + textBehaviourRight.Text = pongScoreboard.ScoreRight.ToString(); + }; } protected override void Update(GameTime gameTime) @@ -170,56 +219,18 @@ public class Game1 : Game if (Keyboard.GetState().IsKeyDown(Keys.E)) cameraBehaviour.BehaviourController.GameObject.Transform.Rotation -= gameTime.ElapsedGameTime.Nanoseconds * 0.000025f; - if (Keyboard.GetState().IsKeyDown(Keys.N)) - { - seconds = 70f; - while (physicsTimer + 0.01f < seconds) - { - physicsTimer += 0.01f; - engine.Step(.01f); - } - } - if (Keyboard.GetState().IsKeyDown(Keys.M)) - { - seconds = 0f; - while (physicsTimer - 0.01f > seconds) - { - physicsTimer -= 0.01f; - engine.Step(-.01f); - } - } if (Keyboard.GetState().IsKeyDown(Keys.Space)) - { - seconds += gameTime.ElapsedGameTime.Milliseconds * .005f; - while (physicsTimer + 0.01f < seconds) - { - Console.WriteLine($"Physics Timer: {physicsTimer}"); - physicsTimer += 0.01f; - engine.Step(.01f); - } - } - if (Keyboard.GetState().IsKeyDown(Keys.B)) - { - seconds -= gameTime.ElapsedGameTime.Milliseconds * .005f; - while (physicsTimer - 0.01f > seconds) - { - Console.WriteLine($"Physics Timer: {physicsTimer}"); - physicsTimer -= 0.01f; - engine.Step(-.01f); - } - } + pongScoreboard.Reset(); gameManager.Update(gameTime.ToEngineTime()); while (physicsTimer + 0.01f < gameTime.TotalGameTime.TotalMilliseconds * .001f)//seconds) { - // Console.WriteLine($"Physics Timer: {physicsTimer}"); physicsTimer += 0.01f; engine.Step(.01f); } base.Update(gameTime); } static float physicsTimer = 0f; - static float seconds = 0f; protected override void Draw(GameTime gameTime) {