/** * Writes the events of a single frame with the writer to the packet */ void writeFrame(frame f) { foreach (Keys k in f.pressedKeys) { writer.Write((byte)1); writer.Write(f.frameNumber + latency); writer.Write((int)k); } foreach (Keys k in f.releasedKeys) { writer.Write((byte)2); writer.Write(f.frameNumber + latency); writer.Write((int)k); } if (f.mouseChange) { if (f.mouseDown) { writer.Write((byte)3); writer.Write(f.frameNumber + latency); writer.Write((byte)1); } else { writer.Write((byte)4); writer.Write(f.frameNumber + latency); writer.Write((byte)1); } } writer.Write((byte)5); writer.Write(f.frameNumber + latency); writer.Write((short)f.mouseX); writer.Write((short)f.mouseY); }
/// <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) { //IN BEG. MENU if (inMenu) { // Get user's input state. KeyboardState keyState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); // Make a list of the keys pressed or released this frame. List<Keys> pressedKeys = new List<Keys>(); releasedKeys = new List<Keys>(); mouseChange = false; Keys[] pressedKeysArray = keyState.GetPressedKeys(); foreach (Keys k in pressedKeysArray) { if (!lastPressedKeys.Contains(k)) pressedKeys.Add(k); else lastPressedKeys.Remove(k); } releasedKeys = lastPressedKeys; lastPressedKeys = new List<Keys>(pressedKeysArray); //Act on the input depending on which screen we are on if (activeS == startS) { SSInput(); } else if (activeS == helpS) { HSInput(); } } else { //IN LOBBY if (networkSession == null) { //if we are not in game - in lobby UpdateLobby(); } else //update game { networkSession.Update(); //IN GAME if (gameStarted) { if (musicControl.gameplayStarted == false) { musicControl.changeToGameplayMusic(); musicControl.gameplayStarted = true; } musicControl.Update(); if (deterministicGame.duckAccuracy < 60) { deterministicGame.currentLevel.playerLose = true; } else { deterministicGame.currentLevel.playerLose = false; } if (deterministicGame.currentLevel.gameRestart == true) { restart(); return; } // Get user's input state. KeyboardState keyState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); // Make a list of the keys pressed or released this frame. List<Keys> pressedKeys = new List<Keys>(); releasedKeys = new List<Keys>(); mouseChange = false; Keys[] pressedKeysArray = keyState.GetPressedKeys(); foreach (Keys k in pressedKeysArray) { if (!lastPressedKeys.Contains(k)) pressedKeys.Add(k); else lastPressedKeys.Remove(k); } releasedKeys = lastPressedKeys; lastPressedKeys = new List<Keys>(pressedKeysArray); // Get mouse button state. if (buttonPressed != (mouseState.LeftButton == ButtonState.Pressed)) { buttonPressed = mouseState.LeftButton == ButtonState.Pressed; mouseChange = true; } /***** Begining of game logic. *****/ // Debug - allow user to exit. if (pressedKeys.Contains(Keys.Escape)) this.Exit(); // Debug - allow user on this machine to direct input to any player's state in the game. if (pressedKeys.Contains(Keys.F1)) activePlayer = playerIdentifiers[0]; if (pressedKeys.Contains(Keys.F2)) activePlayer = playerIdentifiers[1]; if (pressedKeys.Contains(Keys.F3)) activePlayer = playerIdentifiers[2]; if (pressedKeys.Contains(Keys.F4)) activePlayer = playerIdentifiers[3]; // Debug - allow user on this machine to pause/resume game state advances. // Debug - automatically pause every 1000 frames. // Game update // Direct inputs to the game engine - only report changes. lastButtonPressed = buttonPressed; if (!frames.ContainsKey(frameNumber)) { frame newFrame = new frame(frameNumber, pressedKeys, releasedKeys, mouseState.X, mouseState.Y, buttonPressed, mouseChange); frames.Add(frameNumber, newFrame); } if (!myPlayer.framesOfPlayer.ContainsKey(frameNumber + latency)) { myPlayer.framesOfPlayer.Add(frameNumber + latency, new frame()); myPlayer.lastFrameNumber = frameNumber + latency; } networkCommunications(); if (!paused && deterministicGame.CurrentFrameNumber <= frameNumber) { // Advance the game engine. deterministicGame.Update(targetTimeSpan); } /***** End of game logic. *****/ } //IN WAITING ROOM else UpdateWR(); // Allow the superclass to do any needed updates (unknown purpose). } } base.Update(gameTime); }
/// <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) { //IN BEG. MENU if (inMenu) { // Get user's input state. KeyboardState keyState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); // Make a list of the keys pressed or released this frame. List <Keys> pressedKeys = new List <Keys>(); releasedKeys = new List <Keys>(); mouseChange = false; Keys[] pressedKeysArray = keyState.GetPressedKeys(); foreach (Keys k in pressedKeysArray) { if (!lastPressedKeys.Contains(k)) { pressedKeys.Add(k); } else { lastPressedKeys.Remove(k); } } releasedKeys = lastPressedKeys; lastPressedKeys = new List <Keys>(pressedKeysArray); //Act on the input depending on which screen we are on if (activeS == startS) { SSInput(); } else if (activeS == helpS) { HSInput(); } } else { //IN LOBBY if (networkSession == null) { //if we are not in game - in lobby UpdateLobby(); } else //update game { networkSession.Update(); //IN GAME if (gameStarted) { if (musicControl.gameplayStarted == false) { musicControl.changeToGameplayMusic(); musicControl.gameplayStarted = true; } musicControl.Update(); if (deterministicGame.duckAccuracy < 60) { deterministicGame.currentLevel.playerLose = true; } else { deterministicGame.currentLevel.playerLose = false; } if (deterministicGame.currentLevel.gameRestart == true) { restart(); return; } // Get user's input state. KeyboardState keyState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); // Make a list of the keys pressed or released this frame. List <Keys> pressedKeys = new List <Keys>(); releasedKeys = new List <Keys>(); mouseChange = false; Keys[] pressedKeysArray = keyState.GetPressedKeys(); foreach (Keys k in pressedKeysArray) { if (!lastPressedKeys.Contains(k)) { pressedKeys.Add(k); } else { lastPressedKeys.Remove(k); } } releasedKeys = lastPressedKeys; lastPressedKeys = new List <Keys>(pressedKeysArray); // Get mouse button state. if (buttonPressed != (mouseState.LeftButton == ButtonState.Pressed)) { buttonPressed = mouseState.LeftButton == ButtonState.Pressed; mouseChange = true; } /***** Begining of game logic. *****/ // Debug - allow user to exit. if (pressedKeys.Contains(Keys.Escape)) { this.Exit(); } // Debug - allow user on this machine to direct input to any player's state in the game. if (pressedKeys.Contains(Keys.F1)) { activePlayer = playerIdentifiers[0]; } if (pressedKeys.Contains(Keys.F2)) { activePlayer = playerIdentifiers[1]; } if (pressedKeys.Contains(Keys.F3)) { activePlayer = playerIdentifiers[2]; } if (pressedKeys.Contains(Keys.F4)) { activePlayer = playerIdentifiers[3]; } // Debug - allow user on this machine to pause/resume game state advances. // Debug - automatically pause every 1000 frames. // Game update // Direct inputs to the game engine - only report changes. lastButtonPressed = buttonPressed; if (!frames.ContainsKey(frameNumber)) { frame newFrame = new frame(frameNumber, pressedKeys, releasedKeys, mouseState.X, mouseState.Y, buttonPressed, mouseChange); frames.Add(frameNumber, newFrame); } if (!myPlayer.framesOfPlayer.ContainsKey(frameNumber + latency)) { myPlayer.framesOfPlayer.Add(frameNumber + latency, new frame()); myPlayer.lastFrameNumber = frameNumber + latency; } networkCommunications(); if (!paused && deterministicGame.CurrentFrameNumber <= frameNumber) { // Advance the game engine. deterministicGame.Update(targetTimeSpan); } /***** End of game logic. *****/ } //IN WAITING ROOM else { UpdateWR(); } // Allow the superclass to do any needed updates (unknown purpose). } } base.Update(gameTime); }