/// <summary> /// Handle the end of the session. /// </summary> void networkSession_SessionEnded(object sender, NetworkSessionEndedEventArgs e) { if ((world != null) && !world.GameExited) { world.GameExited = true; world = null; } if (!IsExiting) { ExitScreen(); } networkSession = null; }
/// <summary> /// Event handler for when the user selects "yes" on the "are you sure /// you want to exit" message box. /// </summary> private void ExitMessageBoxAccepted(object sender, EventArgs e) { world.GameExited = true; world = null; }
/// <summary> /// Handle the end of the game session. /// </summary> void networkSession_GameEnded(object sender, GameEndedEventArgs e) { if ((world != null) && !world.GameWon && !world.GameExited) { world.GameExited = true; } if (!IsExiting && ((world == null) || world.GameExited)) { world = null; ExitScreen(); networkSession = null; } }
/// <summary> /// Construct a new GameplayScreen object. /// </summary> /// <param name="networkSession">The network session for this game.</param> /// <param name="world">The primary gameplay object.</param> public GameplayScreen(NetworkSession networkSession, World world) { // safety-check the parameters if (networkSession == null) { throw new ArgumentNullException("networkSession"); } if (world == null) { throw new ArgumentNullException("world"); } // apply the parameters this.networkSession = networkSession; this.world = world; // set up the network events sessionEndedHandler = new EventHandler<NetworkSessionEndedEventArgs>( networkSession_SessionEnded); networkSession.SessionEnded += sessionEndedHandler; gameEndedHandler = new EventHandler<GameEndedEventArgs>( networkSession_GameEnded); networkSession.GameEnded += gameEndedHandler; gamerLeftHandler = new EventHandler<GamerLeftEventArgs>( networkSession_GamerLeft); networkSession.GamerLeft += gamerLeftHandler; // cache the local player's ship object if (networkSession.LocalGamers.Count > 0) { PlayerData playerData = networkSession.LocalGamers[0].Tag as PlayerData; if (playerData != null) { localShip = playerData.Ship; } } // set the transition times TransitionOnTime = TimeSpan.FromSeconds(1.0); TransitionOffTime = TimeSpan.FromSeconds(1.0); }
/// <summary> /// Lets the game respond to player input. Unlike the Update method, /// this will only be called when the gameplay screen is active. /// </summary> public override void HandleInput(InputState input) { if (input == null) throw new ArgumentNullException("input"); if (!IsExiting) { if ((world != null) && !world.GameExited) { if (input.PauseGame && !world.GameWon) { // If they pressed pause, bring up the pause menu screen. const string message = "Exit the game?"; MessageBoxScreen messageBox = new MessageBoxScreen(message, false); messageBox.Accepted += ExitMessageBoxAccepted; ScreenManager.AddScreen(messageBox); } if (input.MenuSelect && world.GameWon) { world.GameExited = true; world = null; if (!IsExiting) { ExitScreen(); } networkSession = null; } } } }
/// <summary> /// Disposes this object. /// </summary> /// <param name="disposing"> /// True if this method was called as part of the Dispose method. /// </param> protected virtual void Dispose(bool disposing) { if (disposing) { lock (this) { if (world != null) { world.Dispose(); world = null; } if (packetWriter != null) { packetWriter.Close(); packetWriter = null; } } } }
/// <summary> /// Handle the end of the network session. /// </summary> void networkSession_SessionEnded(object sender, NetworkSessionEndedEventArgs e) { if (!IsExiting) { ExitScreen(); } if (world != null) { world.Dispose(); world = null; } if (networkSession != null) { networkSession.Dispose(); networkSession = null; } }
/// <summary> /// When the user cancels the main menu, ask if they want to exit the sample. /// </summary> protected override void OnCancel() { if (!IsExiting) { ExitScreen(); } if (world != null) { world.Dispose(); world = null; } if (networkSession != null) { networkSession.Dispose(); networkSession = null; } }
/// <summary> /// Updates the lobby. This method checks the GameScreen.IsActive /// property, so the game will stop updating when the pause menu is active, /// or if you tab away to a different application. /// </summary> public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { if (networkSession != null) { // update the network session try { networkSession.Update(); } catch (NetworkException ne) { System.Console.WriteLine( "Network failed to update: " + ne.ToString()); if (networkSession != null) { networkSession.Dispose(); networkSession = null; } } } // update the world if ((world != null) && !otherScreenHasFocus && !coveredByOtherScreen) { if (world.GameWon) { // unload the existing world world.Dispose(); world = null; // make sure that all of the ships have cleaned up foreach (NetworkGamer networkGamer in networkSession.AllGamers) { PlayerData playerData = networkGamer.Tag as PlayerData; if ((playerData != null) && (playerData.Ship != null)) { playerData.Ship.Die(null, true); } } // make sure the collision manager is up-to-date CollisionManager.Collection.ApplyPendingRemovals(); // create a new world world = new World(ScreenManager.GraphicsDevice, ScreenManager.Content, networkSession); } else if (world.GameExited) { if (!IsExiting) { ExitScreen(); } if (world != null) { world.Dispose(); world = null; } if (networkSession != null) { networkSession.Dispose(); networkSession = null; } base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen); return; } else { world.Update((float)gameTime.ElapsedGameTime.TotalSeconds, true); } } // update the menu entry text if (otherScreenHasFocus == false) { if ((networkSession.LocalGamers.Count > 0) && (networkSession.SessionState == NetworkSessionState.Lobby)) { if (!networkSession.LocalGamers[0].IsReady) { MenuEntries[0] = "Press X to Mark as Ready"; } else if (!networkSession.IsEveryoneReady) { MenuEntries[0] = "Waiting for all players to mark as ready..."; } else if (!networkSession.IsHost) { MenuEntries[0] = "Waiting for the host to start game..."; } else { MenuEntries[0] = "Starting the game..."; networkSession.StartGame(); } } else if (networkSession.SessionState == NetworkSessionState.Playing) { MenuEntries[0] = "Game starting..."; } // if the game is playing and the world is initialized, then start up if ((networkSession.SessionState == NetworkSessionState.Playing) && (world != null) && world.Initialized) { GameplayScreen gameplayScreen = new GameplayScreen(networkSession, world); gameplayScreen.ScreenManager = this.ScreenManager; ScreenManager.AddScreen(gameplayScreen); } } base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen); }
/// <summary> /// Load graphics content for the game. /// </summary> public override void LoadContent() { base.LoadContent(); // create the world object world = new World(ScreenManager.GraphicsDevice, ScreenManager.Content, networkSession); // set the networking events networkSession.GamerJoined += gamerJoinedHandler; networkSession.GameStarted += gameStartedHandler; networkSession.SessionEnded += sessionEndedHandler; }