/// <summary> /// Saves the current game state to isolated storage. /// </summary> private void SaveStateToIsolatedStorage() { GameplayScreen gameplayScreen = GetGameplayScreen(); if (gameplayScreen == null) { // There's no game to save. Clean saved data that may relate to a previous save. NinjAcademyGame.CleanIsolatedStorage(); return; } using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { // Save the game's current state (only a portion of the state is saved) using (IsolatedStorageFileStream fileStream = isolatedStorageFile.CreateFile(SaveFileName)) { using (StreamWriter streamWriter = new StreamWriter(fileStream)) { streamWriter.WriteLine(gameplayScreen.Score); streamWriter.WriteLine(gameplayScreen.HitPoints); streamWriter.WriteLine(gameplayScreen.GamePhasesPassed); streamWriter.WriteLine(gameplayScreen.ElapsedPhaseTime); } } } }
/// <summary> /// Performs necessary update logic. /// </summary> /// <param name="gameTime">Game time information.</param> /// <param name="otherScreenHasFocus">Whether another screen has the focus.</param> /// <param name="coveredByOtherScreen">Whether this screen is covered by another.</param> public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen); // Save the high-score when exiting the game if (isExiting) { if (!HighScoreScreen.HighscoreSaved) { HighScoreScreen.SaveHighscore(); } else { isExiting = false; // When exiting intentionally, clear the saved game data NinjAcademyGame.CleanIsolatedStorage(); ScreenManager.Game.Exit(); return; } } // Move on to the instruction screen if necessary if (isMovingToLoading) { foreach (GameScreen screen in ScreenManager.GetScreens()) { screen.ExitScreen(); } ScreenManager.AddScreen(new BackgroundScreen("Instructions"), null); ScreenManager.AddScreen(new LoadingScreen(instructionsTexture, loadingTexture), null); AudioManager.StopMusic(); return; } // Cause the ninja and title text to appear gradually ninjaTimer += gameTime.ElapsedGameTime; titleTimer += gameTime.ElapsedGameTime; switch (ninjaState) { case ElementState.Invisible: if (ninjaTimer >= ninjaAppearDelay) { ninjaState = ElementState.Appearing; ninjaTimer = TimeSpan.Zero; } break; case ElementState.Appearing: ninjaOffset = ninjaInitialOffset * (float)Math.Pow(1 - ninjaTimer.TotalMilliseconds / ninjaAppearDuration.TotalMilliseconds, 2); if (ninjaTimer > ninjaAppearDuration) { ninjaState = ElementState.Visible; } break; case ElementState.Visible: // Nothing to do in this state break; default: break; } switch (titleState) { case ElementState.Invisible: if (titleTimer >= titleAppearDelay) { titleState = ElementState.Appearing; titleTimer = TimeSpan.Zero; } break; case ElementState.Appearing: titleOffset = titleInitialOffset * (float)Math.Pow(1 - titleTimer.TotalMilliseconds / titleAppearDuration.TotalMilliseconds, 2); if (titleTimer > titleAppearDuration) { titleState = ElementState.Visible; } break; case ElementState.Visible: // Nothing to do in this state break; default: break; } }