/// <summary> /// Updates the background screen. Unlike most screens, this should not /// transition off even if it has been covered by another screen: it is /// supposed to be covered, after all! This overload forces the /// coveredByOtherScreen parameter to false in order to stop the base /// Update method wanting to transition off. /// </summary> public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { starField.Update(gameTime); asteroidManager.Update(gameTime); base.Update(gameTime, otherScreenHasFocus, false); }
/// <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 (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // TODO: Add your update logic here kbState = Keyboard.GetState(); switch (currState) { case GameStates.MainMenu: if (kbState.IsKeyDown(Keys.Space)) { currState = GameStates.GamePlaying; } break; case GameStates.GamePlaying: bm.Update(gameTime); am.Update(gameTime); ship.Update(gameTime, kbState); if (ship.Lives <= 0) { currState = GameStates.GameOver; } break; case GameStates.GameOver: if (kbState.IsKeyDown(Keys.Space)) { currState = GameStates.MainMenu; am.Reset(); ship.Reset(); } break; } base.Update(gameTime); }
/// <summary> /// Updates the state of the game. /// </summary> public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { base.Update(gameTime, otherScreenHasFocus, false); // Gradually fade in or out depending on whether we are covered by the pause screen. if (coveredByOtherScreen) { pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1); } else { pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0); } if (IsActive) { starField.Update(gameTime); if (networkSession != null) { // Process Incoming Packets ReceiveNetworkData(); if (networkSession.IsHost) { LocalNetworkGamer gamer = networkSession.Host as LocalNetworkGamer; if (updatesSinceGameDataSend >= updatesBetweenGameDataPackets) { updatesSinceGameDataSend = 0; SendGameData(); } else { updatesSinceGameDataSend++; } } // Update players foreach (NetworkGamer gamer in networkSession.AllGamers) { Player p = gamer.Tag as Player; if (p != null) { p.Update(gameTime); if (gamer.IsLocal && p.wasKilled) { // Reset the death flag p.wasKilled = false; SendLocalShipDeath(); } } } // Send Player Data if (updatesSincePlayerDataSend >= updatesBetweenPlayerDataPackets) { updatesSincePlayerDataSend = 0; SendLocalPlayerData(); } else { updatesSincePlayerDataSend++; } // Send Ship Data if (updatesSinceStatusPacket >= updatesBetweenStatusPackets) { updatesSinceStatusPacket = 0; SendLocalShipData(); } else { updatesSinceStatusPacket++; } } else { players[(int)ControllingPlayer].Update(gameTime); } // Update Asteroids asteroidManager.Update(gameTime); // See if we need to increment the level if (asteroidManager.Asteroids.Count == 0) { asteroidManager.StartLevel(level++); // Enable Spawn Protection if (networkSession == null) { players[0].EnableSpawnProtection(); } else { foreach (NetworkGamer gamer in networkSession.AllGamers) { Player p = gamer.Tag as Player; if (p != null) { p.EnableSpawnProtection(); } } } } // Handle collision detection if (networkSession == null) { CheckCollisions(); } else { CheckMultiplayerCollisions(); } } // Check for gameOver state if (networkSession != null) { bool shouldReturnToLobby = true; foreach (NetworkGamer gamer in networkSession.AllGamers) { Player p = gamer.Tag as Player; if (p.IsGameOver == false) { shouldReturnToLobby = false; } } // If all players are in the gameOver state then return to the lobby if (networkSession.IsHost && shouldReturnToLobby) { if (networkSession.SessionState == NetworkSessionState.Playing) { networkSession.EndGame(); } } } // If we are in a network game, check if we should return to the lobby. if ((networkSession != null) && !IsExiting) { if (networkSession.SessionState == NetworkSessionState.Lobby) { LoadingScreen.Load(ScreenManager, false, null, new TitleBackgroundScreen(), new LobbyScreen(networkSession)); } } }