/// <summary> /// This event handler takes the case when the available sessions are found /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void foundAvailableSessions(object sender, EventArgs e) { searchingForAvailableSessions = false; if (availableSessions.Count == 0) { currentSessionName = "No Sessions Found"; } else { currentSessionName = string.Format("Session %s : %d / %d", availableSessions[0].HostGamertag, availableSessions[0].CurrentGamerCount, availableSessions[0].CurrentGamerCount + availableSessions[0].OpenPublicGamerSlots); } selectedSession = 0; menuState = NetworkMenuState.JoinSession; menuSessionState = 0; menuSpriteLocation = TextLocations[4] - new Vector2(10, 0); }
/// <summary> /// This event handler takes the case when the available sessions are found /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void foundAvailableSessions(object sender, EventArgs e) { searchingForAvailableSessions = false; if (availableSessions.Count == 0) currentSessionName = "No Sessions Found"; else currentSessionName = string.Format("Session %s : %d / %d", availableSessions[0].HostGamertag, availableSessions[0].CurrentGamerCount, availableSessions[0].CurrentGamerCount + availableSessions[0].OpenPublicGamerSlots); selectedSession = 0; menuState = NetworkMenuState.JoinSession; menuSessionState = 0; menuSpriteLocation = TextLocations[4] - new Vector2(10, 0); }
/// <summary> /// Updates the NetworkSystem to /// 1) Create and maintain the network session /// 2) update all networked element's components and /// 3) send local element's component's updates to remote systems /// </summary> /// <param name="elapsedTime"> /// The time elapsed between this and the previous frame /// </param> public void Update(float elapsedTime) { if (searchingForAvailableSessions) { searchingtimer += elapsedTime; searchingtimer %= 1; return; } if (game.GameState == GameState.NetworkSetup) { switch (menuState) { case NetworkMenuState.SelectMode: if (menuSessionState == 1) { if (Keyboard.GetState().IsKeyDown(Keys.Left) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.X <= -1) { if (!pressedKeys.left) { menuSessionState--; menuSpriteLocation = TextLocations[1] - new Vector2(10, 0); pressedKeys.left = true; menuSelectSound.Play(); break; } } else { pressedKeys.left = false; } if (Keyboard.GetState().IsKeyDown(Keys.Space) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.A)) { if (!pressedKeys.space) { try { menuSelectSound.Play(); backgroundWorker = new BackgroundWorker(); backgroundWorker.DoWork += new DoWorkEventHandler(findAvailableSessions); backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(foundAvailableSessions); backgroundWorker.RunWorkerAsync(); searchingForAvailableSessions = true; pressedKeys.space = true; } catch (Exception e) { //Throw error } break; } } else { pressedKeys.space = false; } } if (menuSessionState == 0) { if (Keyboard.GetState().IsKeyDown(Keys.Right) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.X >= 1) { if(!pressedKeys.right) { menuSessionState++; menuSpriteLocation = TextLocations[2] - new Vector2(10, 0); pressedKeys.right = true; menuSelectSound.Play(); break; } } else { pressedKeys.right = false; } if (Keyboard.GetState().IsKeyDown(Keys.Space) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.A)) { if (!pressedKeys.space) { menuSelectSound.Play(); menuState = NetworkMenuState.CreateSession; menuSpriteLocation = TextLocations[3] - new Vector2(10, 0); pressedKeys.space = true; break; } } else { pressedKeys.space = false; } } break; case NetworkMenuState.JoinSession: if (menuSessionState == 0) //select game { if (Keyboard.GetState().IsKeyDown(Keys.Down) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y >= 1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.Y >= 1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.Y >= 1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.Y >= 1) { if (!pressedKeys.down) { menuSessionState++; menuSpriteLocation = TextLocations[5] - new Vector2(10, 0); pressedKeys.down = true; menuSelectSound.Play(); break; } } else { pressedKeys.down = false; } if (Keyboard.GetState().IsKeyDown(Keys.Left) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.X <= -1) { if (!pressedKeys.left) { if (availableSessions.Count > 0) { selectedSession--; selectedSession %= availableSessions.Count; currentSessionName = string.Format("Session %s : %d / %d", availableSessions[selectedSession].HostGamertag, availableSessions[selectedSession].CurrentGamerCount, availableSessions[selectedSession].CurrentGamerCount + availableSessions[selectedSession].OpenPublicGamerSlots); menuSelectSound.Play(); } pressedKeys.left = true; break; } } else { pressedKeys.left = false; } if (Keyboard.GetState().IsKeyDown(Keys.Right) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.X >= 1) { if (!pressedKeys.right) { if (availableSessions.Count > 0) { selectedSession++; selectedSession %= availableSessions.Count; currentSessionName = string.Format("Session %s : %d / %d", availableSessions[selectedSession].HostGamertag, availableSessions[selectedSession].CurrentGamerCount, availableSessions[selectedSession].CurrentGamerCount + availableSessions[selectedSession].OpenPublicGamerSlots); menuSelectSound.Play(); } pressedKeys.right = true; break; } } else { pressedKeys.right = false; } } else if (menuSessionState == 1) //go { if (Keyboard.GetState().IsKeyDown(Keys.Up) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y <= -1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.Y <= -1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.Y <= -1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.Y <= -1) { if (!pressedKeys.up) { menuSessionState--; menuSpriteLocation = TextLocations[4] - new Vector2(10, 0); pressedKeys.up = true; menuSelectSound.Play(); break; } } else { pressedKeys.up = false; } if (Keyboard.GetState().IsKeyDown(Keys.Space) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.A)) { if (!pressedKeys.space) { menuSelectSound.Play(); JoinSession(); break; } } else { pressedKeys.space = false; } } if (Keyboard.GetState().IsKeyDown(Keys.B) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.B)) { if (!pressedKeys.b) { menuSessionState = 1; menuState = NetworkMenuState.SelectMode; menuSpriteLocation = TextLocations[2] - new Vector2(10, 0); pressedKeys.b = true; menuSelectSound.Play(); break; } } else { pressedKeys.b = false; } break; case NetworkMenuState.CreateSession: if (Keyboard.GetState().IsKeyDown(Keys.Space) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.A)) { if (!pressedKeys.space) { menuSelectSound.Play(); CreateSession(); break; } } else { pressedKeys.space = false; } if (Keyboard.GetState().IsKeyDown(Keys.B) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.B)) { if (!pressedKeys.b) { menuSessionState = 0; menuState = NetworkMenuState.SelectMode; menuSpriteLocation = TextLocations[1] - new Vector2(10, 0); pressedKeys.b = true; menuSelectSound.Play(); break; } } else { pressedKeys.b = false; } break; } } else { // send local object updates SendLocalEntityUpdates(); // Pump the session session.Update(); // Check for session ending if (session == null) game.GameState = GameState.NetworkSetup; // Read packets for remote updates RecieveRemoteEntityUpdates(); } }
/// <summary> /// Updates the NetworkSystem to /// 1) Create and maintain the network session /// 2) update all networked element's components and /// 3) send local element's component's updates to remote systems /// </summary> /// <param name="elapsedTime"> /// The time elapsed between this and the previous frame /// </param> public void Update(float elapsedTime) { if (searchingForAvailableSessions) { searchingtimer += elapsedTime; searchingtimer %= 1; return; } if (game.GameState == GameState.NetworkSetup) { switch (menuState) { case NetworkMenuState.SelectMode: if (menuSessionState == 1) { if (Keyboard.GetState().IsKeyDown(Keys.Left) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.X <= -1) { if (!pressedKeys.left) { menuSessionState--; menuSpriteLocation = TextLocations[1] - new Vector2(10, 0); pressedKeys.left = true; menuSelectSound.Play(); break; } } else { pressedKeys.left = false; } if (Keyboard.GetState().IsKeyDown(Keys.Space) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.A)) { if (!pressedKeys.space) { try { menuSelectSound.Play(); backgroundWorker = new BackgroundWorker(); backgroundWorker.DoWork += new DoWorkEventHandler(findAvailableSessions); backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(foundAvailableSessions); backgroundWorker.RunWorkerAsync(); searchingForAvailableSessions = true; pressedKeys.space = true; } catch (Exception e) { //Throw error } break; } } else { pressedKeys.space = false; } } if (menuSessionState == 0) { if (Keyboard.GetState().IsKeyDown(Keys.Right) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.X >= 1) { if (!pressedKeys.right) { menuSessionState++; menuSpriteLocation = TextLocations[2] - new Vector2(10, 0); pressedKeys.right = true; menuSelectSound.Play(); break; } } else { pressedKeys.right = false; } if (Keyboard.GetState().IsKeyDown(Keys.Space) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.A)) { if (!pressedKeys.space) { menuSelectSound.Play(); menuState = NetworkMenuState.CreateSession; menuSpriteLocation = TextLocations[3] - new Vector2(10, 0); pressedKeys.space = true; break; } } else { pressedKeys.space = false; } } break; case NetworkMenuState.JoinSession: if (menuSessionState == 0) //select game { if (Keyboard.GetState().IsKeyDown(Keys.Down) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y >= 1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.Y >= 1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.Y >= 1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.Y >= 1) { if (!pressedKeys.down) { menuSessionState++; menuSpriteLocation = TextLocations[5] - new Vector2(10, 0); pressedKeys.down = true; menuSelectSound.Play(); break; } } else { pressedKeys.down = false; } if (Keyboard.GetState().IsKeyDown(Keys.Left) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.X <= -1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.X <= -1) { if (!pressedKeys.left) { if (availableSessions.Count > 0) { selectedSession--; selectedSession %= availableSessions.Count; currentSessionName = string.Format("Session %s : %d / %d", availableSessions[selectedSession].HostGamertag, availableSessions[selectedSession].CurrentGamerCount, availableSessions[selectedSession].CurrentGamerCount + availableSessions[selectedSession].OpenPublicGamerSlots); menuSelectSound.Play(); } pressedKeys.left = true; break; } } else { pressedKeys.left = false; } if (Keyboard.GetState().IsKeyDown(Keys.Right) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.X >= 1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.X >= 1) { if (!pressedKeys.right) { if (availableSessions.Count > 0) { selectedSession++; selectedSession %= availableSessions.Count; currentSessionName = string.Format("Session %s : %d / %d", availableSessions[selectedSession].HostGamertag, availableSessions[selectedSession].CurrentGamerCount, availableSessions[selectedSession].CurrentGamerCount + availableSessions[selectedSession].OpenPublicGamerSlots); menuSelectSound.Play(); } pressedKeys.right = true; break; } } else { pressedKeys.right = false; } } else if (menuSessionState == 1) //go { if (Keyboard.GetState().IsKeyDown(Keys.Up) || GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.Y <= -1 || GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.Y <= -1 || GamePad.GetState(PlayerIndex.Three).ThumbSticks.Left.Y <= -1 || GamePad.GetState(PlayerIndex.Four).ThumbSticks.Left.Y <= -1) { if (!pressedKeys.up) { menuSessionState--; menuSpriteLocation = TextLocations[4] - new Vector2(10, 0); pressedKeys.up = true; menuSelectSound.Play(); break; } } else { pressedKeys.up = false; } if (Keyboard.GetState().IsKeyDown(Keys.Space) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.A)) { if (!pressedKeys.space) { menuSelectSound.Play(); JoinSession(); break; } } else { pressedKeys.space = false; } } if (Keyboard.GetState().IsKeyDown(Keys.B) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.B)) { if (!pressedKeys.b) { menuSessionState = 1; menuState = NetworkMenuState.SelectMode; menuSpriteLocation = TextLocations[2] - new Vector2(10, 0); pressedKeys.b = true; menuSelectSound.Play(); break; } } else { pressedKeys.b = false; } break; case NetworkMenuState.CreateSession: if (Keyboard.GetState().IsKeyDown(Keys.Space) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.A) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.A)) { if (!pressedKeys.space) { menuSelectSound.Play(); CreateSession(); break; } } else { pressedKeys.space = false; } if (Keyboard.GetState().IsKeyDown(Keys.B) || GamePad.GetState(PlayerIndex.One).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Two).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Three).IsButtonDown(Buttons.B) || GamePad.GetState(PlayerIndex.Four).IsButtonDown(Buttons.B)) { if (!pressedKeys.b) { menuSessionState = 0; menuState = NetworkMenuState.SelectMode; menuSpriteLocation = TextLocations[1] - new Vector2(10, 0); pressedKeys.b = true; menuSelectSound.Play(); break; } } else { pressedKeys.b = false; } break; } } else { // send local object updates SendLocalEntityUpdates(); // Pump the session session.Update(); // Check for session ending if (session == null) { game.GameState = GameState.NetworkSetup; } // Read packets for remote updates RecieveRemoteEntityUpdates(); } }