/// <summary> /// Reads the saved game from the save file, and restores the components' /// positions and values from it /// </summary> public bool LoadGame() { string lineContent = ""; string[] bits; if (!File.Exists(Shared.saveFileName)) { // display error message, return to the start scene return(false); } using (StreamReader reader = new StreamReader(Shared.saveFileName)) { if (reader.EndOfStream) { // display empty file message } else { try { int counter = 0; //initiate player's name lineContent = reader.ReadLine(); Shared.playerName = lineContent; // initiate score lineContent = reader.ReadLine(); Shared.score = int.Parse(lineContent); // add spaceship lineContent = reader.ReadLine(); bits = lineContent.Split('|'); shattle.Position = new Vector2(float.Parse(bits[0]), float.Parse(bits[1])); shattle.Rotation = float.Parse(bits[2]); shattle.Visible = true; shattle.Enabled = true; // initialize pot of gold lineContent = reader.ReadLine(); bits = lineContent.Split('|'); if (bits[0] != "none") { pot.Position = new Vector2(float.Parse(bits[0]), float.Parse(bits[1])); pot.Enabled = true; pot.Visible = true; Shared.goldOnScreen = true; } Shared.timerGold = float.Parse(bits[2]); lineContent = reader.ReadLine(); Shared.timerAddAsteroid = float.Parse(lineContent); //add the asteroids do { lineContent = reader.ReadLine(); bits = lineContent.Split('|'); if (counter < 5) { asteroids[counter].Position = new Vector2(float.Parse(bits[0]), float.Parse(bits[1])); asteroids[counter].Movement = new Vector2(float.Parse(bits[2]), float.Parse(bits[3])); asteroids[counter].Visible = true; asteroids[counter].Enabled = true; } else { Asteroid a = new Asteroid(game, spriteBatch, game.Content.Load <Texture2D>("Images/asteroid"), new Vector2(float.Parse(bits[0]), float.Parse(bits[1])), new Vector2(float.Parse(bits[2]), float.Parse(bits[3]))); a.Visible = true; a.Enabled = true; asteroids.Add(a); Components.Add(a); } counter++; } while (!reader.EndOfStream); cm.Enabled = true; MediaPlayer.Play(backgroundMusic); background = game.Content.Load <Texture2D>("Images/background"); } catch { // display error message } } } return(true); }
/// <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> public override void Update(GameTime gameTime) { // when Esc is pressed, a message box appears and asks if the user wants // to save the game before leaving if (Keyboard.GetState().IsKeyDown(Keys.Escape) && !Shared.isPaused) { Shared.isPaused = true; game.Components.Add(messageBox); game.Components.Add(messageBox.ButtonYes); game.Components.Add(messageBox.ButtonNo); game.Components.Add(messageBox.ButtonCancel); } // every 15 seconds a new asteroid is generated if (Shared.timerAddAsteroid >= Shared.periodAddAsteroid) { Random r = new Random(); int startInt = r.Next(0, (int)Shared.stageScene.X); int endInt = r.Next(0, (int)Shared.stageScene.Y); float speed = r.Next(1, 3); Vector2 direction = new Vector2(endInt - startInt, Shared.stageScene.Y); direction.Normalize(); asteroid = new Asteroid(game, spriteBatch, game.Content.Load <Texture2D>("Images/asteroid"), new Vector2(startInt, 0), speed * direction); asteroid.Visible = true; asteroid.Enabled = true; asteroids.Add(asteroid); Components.Add(asteroid); Shared.timerAddAsteroid = 0; } // pots appear and disappear from the screen only when the game is not paused if (!Shared.isPaused) { Shared.timerAddAsteroid += gameTime.ElapsedGameTime.TotalSeconds; Shared.timerGold += gameTime.ElapsedGameTime.TotalSeconds; // after each 10 seconds the gold pot was off the screen a new gold pot is added in // a random location if (!Shared.goldOnScreen && Shared.timerGold >= Shared.periodGoldShow && !Shared.gameOver) { Random r = new Random(); int x = r.Next(goldTex.Width / 2, (int)Shared.stageScene.X - goldTex.Width); int y = r.Next(goldTex.Height / 2, (int)Shared.stageScene.Y - goldTex.Height); Rectangle goldRct = new Rectangle(x, y, goldTex.Width, goldTex.Height); while (goldRct.Intersects(shattle.GetBounds())) { x = r.Next(0, (int)Shared.stageScene.X - goldTex.Width); y = r.Next(0, (int)Shared.stageScene.Y - goldTex.Height); goldRct = new Rectangle(x, y, goldTex.Width, goldTex.Height); } pot.Position = new Vector2(x, y); pot.Visible = true; pot.Enabled = true; Shared.goldOnScreen = true; Shared.timerGold = 0; } // after spending 5 sec on the screen, the pot disappears if (Shared.goldOnScreen && Shared.timerGold >= Shared.periodGoldHide) { pot.Enabled = false; pot.Visible = false; pot.Position = Shared.offScreen; Shared.goldOnScreen = false; Shared.timerGold = 0; } } // if a collision was detected, render an explosion animation if (Shared.exploded != Vector2.Zero) { explosion = new Explosion(game, spriteBatch, game.Content.Load <Texture2D>("Images/explosion"), Shared.exploded - new Vector2(15, 15)); Components.Add(explosion); pot.Position = Shared.offScreen; pot.Visible = false; pot.Enabled = false; Shared.timerGold = 0; Shared.exploded = Vector2.Zero; } // after the explosion animation was rendered, we display Game Over screen for 3 sec // after which the exit to the start scene is initiated if (Shared.gameOver == true) { MediaPlayer.Stop(); if (Shared.startGameOverSound) { SoundEffect gameOver = game.Content.Load <SoundEffect>("Audio/game-over-arcade"); gameOver.Play(); Shared.startGameOverSound = false; } background = game.Content.Load <Texture2D>("Images/GameOver"); for (int i = 0; i < asteroids.Count; i++) { asteroids[i].Enabled = false; asteroids[i].Visible = false; asteroids[i].Position = Shared.offScreen; } if (Shared.timerAddAsteroid >= 5.0f) { SaveScore(); MediaPlayer.Stop(); RemoveComponents(); ResetGlobalVariables(); Shared.exitInitiated = true; Shared.gameOver = false; } } // if the exit from game was initiated by user, game is saved (if requested), // components removed and global variable reset if (Shared.removeItems) { if (Shared.saveGame) { SaveGame(); Shared.saveGame = false; } RemoveComponents(); ResetGlobalVariables(); MediaPlayer.Stop(); Shared.removeItems = false; Shared.exitInitiated = true; } base.Update(gameTime); }