public void ResetLevelCommand() { Player1Camera.Limits = level.LevelAreas[0]; _backgroundP1 = new Background(spriteBatch, Player1Camera, BackgroundType.Overworld); if (Mode == GameMode.MultiPlayer) { Player2Camera.Limits = level.LevelAreas[0]; _backgroundP2 = new Background(spriteBatch, Player2Camera, BackgroundType.Overworld); } gameGrid.ClearGrid(); SoundPool.Instance.Reset(); foreach (HUD hud in HUDs) { hud.Dispose(); } HUDs.Clear(); level.Grid = gameGrid; level.Create(); Player1Camera.LookAt(Player1.Position); if (Mode == GameMode.MultiPlayer) { Player2Camera.LookAt(Player2.Position); } State = GameState.Playing; }
public void StartMultiPlayerRun(MultiplayerType type) { Mode = GameMode.MultiPlayer; MultiplayerMode = type; player2Controller.Clear(); Player2 = null; SetupMultiplayerViewports(); _backgroundP1 = new Background(spriteBatch, Player1Camera, BackgroundType.Overworld); _backgroundP2 = new Background(spriteBatch, Player2Camera, BackgroundType.Overworld); gameGrid.ClearGrid(); SoundPool.Instance.Reset(); foreach (HUD hud in HUDs) { hud.Dispose(); } HUDs.Clear(); level.Grid = gameGrid; level.Create(); MapPlayer2Controller(); Player1Camera.LookAt(Player1.Position); Player2Camera.LookAt(Player2.Position); State = GameState.Playing; }
public void StartSinglePlayerRun() { Mode = GameMode.SinglePlayer; player2Controller.Clear(); SetupSingleplayerViewport(); _backgroundP1 = new Background(spriteBatch, Player1Camera, BackgroundType.Overworld); gameGrid.ClearGrid(); SoundPool.Instance.Reset(); foreach (HUD hud in HUDs) { hud.Dispose(); } HUDs.Clear(); level.Grid = gameGrid; level.Create(); Player1Camera.LookAt(Player1.Position); Player2 = Player1; Player2Camera = Player1Camera; State = GameState.Playing; }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { if (State == GameState.Playing || State == GameState.Paused) { if (State != GameState.Paused) { if (transitioningAreaP1) { if (warpOpacityP1 > 255) { warpOpacityP1 = 255; opacityChangeP1 = -5; UpdateCameraForWarp(gameTime, warpArgsP1); } else if (warpOpacityP1 <= 0) { transitioningAreaP1 = false; warpArgsP1 = null; Player1.StateMachine.TransitionIdle(); } } if (transitioningAreaP2 && (Mode == GameMode.MultiPlayer)) { if (warpOpacityP2 > 255) { warpOpacityP2 = 255; opacityChangeP2 = -5; UpdateCameraForWarp(gameTime, warpArgsP2); } else if (warpOpacityP2 <= 0) { transitioningAreaP2 = false; warpArgsP2 = null; Player2.StateMachine.TransitionIdle(); } } } Vector2 parallax = new Vector2(1.0f); GraphicsDevice.Clear(Color.LightSkyBlue); GraphicsDevice.Viewport = player1Viewport; _backgroundP1.Draw(); gameGrid.CurrentLeftSideViewPort = Player1Camera.Position.X; gameGrid.CurrentTopSideViewPort = Player1Camera.Position.Y; List <AbstractGameObject> player1Objects = gameGrid.GetAllCurrentGameObjects; spriteBatch.Begin(SpriteSortMode.BackToFront, null, null, null, null, null, Player1Camera.GetViewMatrix(parallax)); if (transitioningAreaP1 && !(State == GameState.Paused)) { opacityChangeTimeDeltaP1 += gameTime.ElapsedGameTime.Milliseconds; if (opacityChangeTimeDeltaP1 >= 1) { opacityChangeTimeDeltaP1 = 0; warpOpacityP1 += opacityChangeP1; } } DrawPlayerHalfOfGame(gameTime, HUDs[0], Player1Camera, player1Objects, transitioningAreaP1, warpOpacityP1); spriteBatch.End(); spriteBatch.Begin(SpriteSortMode.BackToFront); if (Player1.LevelCompleted) { player1CompletedMenu.Draw(spriteBatch, gameTime); } spriteBatch.End(); if (Mode == GameMode.MultiPlayer) { GraphicsDevice.Viewport = player2Viewport; _backgroundP2.Draw(); gameGrid.CurrentLeftSideViewPort = Player2Camera.Position.X; gameGrid.CurrentTopSideViewPort = Player2Camera.Position.Y; List <AbstractGameObject> player2Objects = gameGrid.GetAllCurrentGameObjects; spriteBatch.Begin(SpriteSortMode.BackToFront, null, null, null, null, null, Player2Camera.GetViewMatrix(parallax)); //TODO probably remove time delta if (transitioningAreaP2 && !(State == GameState.Paused)) { opacityChangeTimeDeltaP2 += gameTime.ElapsedGameTime.Milliseconds; if (opacityChangeTimeDeltaP2 >= 1) { opacityChangeTimeDeltaP2 = 0; warpOpacityP2 += opacityChangeP2; } } DrawPlayerHalfOfGame(gameTime, HUDs[1], Player2Camera, player2Objects, transitioningAreaP2, warpOpacityP2); spriteBatch.End(); spriteBatch.Begin(SpriteSortMode.BackToFront); if (Player2.LevelCompleted) { player2CompletedMenu.Draw(spriteBatch, gameTime); } spriteBatch.End(); } GraphicsDevice.Viewport = new Viewport(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); if (Mode == GameMode.MultiPlayer) { spriteBatch.Begin(SpriteSortMode.BackToFront); using (Texture2D pixel = new Texture2D(GraphicsDevice, 1, 1)) { Color[] color = { Color.Crimson }; pixel.SetData(color); spriteBatch.Draw(pixel, new Rectangle(player1Viewport.Width, 0, player2Viewport.X - player1Viewport.Width, graphics.PreferredBackBufferHeight), Color.White); } spriteBatch.End(); } if (State == GameState.Paused) { spriteBatch.Begin(SpriteSortMode.BackToFront); pauseMenu.Draw(spriteBatch, gameTime); spriteBatch.End(); base.Draw(gameTime); } base.Draw(gameTime); } else if (State == GameState.GameStart) { GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(SpriteSortMode.BackToFront); mainMenu.Draw(spriteBatch, gameTime); spriteBatch.End(); base.Draw(gameTime); } else if (State == GameState.GameOver) { GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(SpriteSortMode.BackToFront); gameOverScreen.Draw(spriteBatch, gameTime); spriteBatch.End(); base.Draw(gameTime); } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (State == GameState.GameStart) { mainMenu.Update(gameTime); } else if (State == GameState.Paused) { pauseMenu.Update(gameTime); } else if (State == GameState.Playing) { generalController.UpdateAndExecuteInputs(); List <AbstractGameObject> collidables = new List <AbstractGameObject>(); if (!Player1.LevelCompleted) { player1Controller.UpdateAndExecuteInputs(); Player1Camera.LookAt(Player1.Position); if (!transitioningAreaP1) { gameGrid.CurrentLeftSideViewPort = Player1Camera.Position.X; gameGrid.CurrentTopSideViewPort = Player1Camera.Position.Y; collidables = gameGrid.GetCurrentMovingAndPlayerGameObjects; } } else { player1CompletedMenu.Update(gameTime); } if (Mode == GameMode.MultiPlayer && !Player2.LevelCompleted) { player2Controller.UpdateAndExecuteInputs(); Player2Camera.LookAt(Player2.Position); if (!transitioningAreaP2) { gameGrid.CurrentLeftSideViewPort = Player2Camera.Position.X; gameGrid.CurrentTopSideViewPort = Player2Camera.Position.Y; collidables = collidables.Union(gameGrid.GetCurrentMovingAndPlayerGameObjects).ToList(); } } else { player2CompletedMenu.Update(gameTime); } if (transitioningAreaP1 || Player1.LevelCompleted) { collidables.RemoveAll((x) => ReferenceEquals(x, Player1)); } if ((transitioningAreaP2 || Player2.LevelCompleted) && (Mode == GameMode.MultiPlayer)) { collidables.RemoveAll((x) => ReferenceEquals(x, Player2)); } List <AbstractGameObject> removed = CollisionManager.ProcessFrame(gameTime, collidables, gameGrid); Player1Camera.LookAt(Player1.Position); List <AbstractGameObject> otherObjects = new List <AbstractGameObject>(); if (!transitioningAreaP1 && !Player1.LevelCompleted) { gameGrid.CurrentLeftSideViewPort = Player1Camera.Position.X; gameGrid.CurrentTopSideViewPort = Player1Camera.Position.Y; otherObjects = gameGrid.GetAllCurrentStaticGameObjects; } if (Mode == GameMode.MultiPlayer) { Player2Camera.LookAt(Player2.Position); if (!transitioningAreaP2 && !Player2.LevelCompleted) { gameGrid.CurrentLeftSideViewPort = Player2Camera.Position.X; gameGrid.CurrentTopSideViewPort = Player2Camera.Position.Y; otherObjects = otherObjects.Union(gameGrid.GetAllCurrentStaticGameObjects).ToList(); } } foreach (AbstractGameObject obj in otherObjects) { if (obj.Update(gameTime, 1)) { removed.Add(obj); } } foreach (AbstractGameObject obj in removed) { gameGrid.Remove(obj); } foreach (HUD hud in HUDs) { hud.Update(gameTime); } } else if (State == GameState.GameOver) { gameOverScreen.Update(gameTime); } SoundPool.Instance.Update(); base.Update(gameTime); }