/// <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) { float elapsedTimeSeconds = (float)gameTime.ElapsedGameTime.TotalSeconds; float elapsedTimeMilliSeconds = (float)gameTime.ElapsedGameTime.TotalMilliseconds; bool playerJump = m_gameView.DidPlayerPressToJump(); bool playerGoesRight = m_gameView.DidPlayerPressGoRight(); bool playerGoesLeft = m_gameView.DidPlayerPressGoLeft(); bool playerPause = m_gameView.DidPlayerPressToPause(); switch (m_gameModel.GetGameState) { case GameModel.GameState.MAIN_MENU: if (m_sound.BackGroundSongPlaying) { m_sound.StopGameBackgroundSong(); } OptionSelected(); break; case GameModel.GameState.PLAY: if (m_sound.BackGroundSongPlaying == false) { m_sound.StartGameBackgroundSong(); } if (playerPause) { m_gameModel.SetGameState = GameModel.GameState.PAUSE; } if (playerGoesRight) { if (m_gameModel.CanPlayerMoveRight()) { m_gameModel.MoveRight(); m_gameView.AnimateMovement(elapsedTimeMilliSeconds, GameView.Movement.RIGHT); } } else if (playerGoesLeft) { m_gameModel.MoveLeft(); m_gameView.AnimateMovement(elapsedTimeMilliSeconds, GameView.Movement.LEFT); } else { m_gameModel.StandStill(); m_gameView.AnimateMovement(elapsedTimeMilliSeconds, GameView.Movement.STAND); } if (playerJump) { if (m_gameModel.CanPlayerJump()) { m_gameModel.Jump(); m_sound.PlayerJump(); } } m_gameModel.Update(elapsedTimeSeconds, m_sound, m_gameView); break; case GameModel.GameState.GAME_OVER: if (m_sound.BackGroundSongPlaying) { m_sound.StopGameBackgroundSong(); } OptionSelected(); m_gameView.ShowCoinSplatter = false; m_gameModel.RestartLevel(); break; case GameModel.GameState.LEVEL_FINISHED: if (m_sound.BackGroundSongPlaying) { m_sound.StopGameBackgroundSong(); } OptionSelected(); m_gameView.ShowCoinSplatter = false; m_gameModel.LoadLevel(); break; case GameModel.GameState.LAST_LEVEL_FINISHED: if (m_sound.BackGroundSongPlaying) { m_sound.StopGameBackgroundSong(); } OptionSelected(); // Player chooses to play again so reset level and attempts counter if (m_gameModel.GetGameState == GameModel.GameState.PLAY) { m_gameModel.ResetAttemptsCounter(); m_gameModel.ResetLevel(); m_gameModel.LoadLevel(); } m_gameView.ShowCoinSplatter = false; break; case GameModel.GameState.PAUSE: if (m_sound.BackGroundSongPlaying) { m_sound.StopGameBackgroundSong(); } OptionSelected(); break; } base.Update(gameTime); }