/// <summary> /// Handles input, performs physics, and animates the player sprite. /// </summary> /// <remarks> /// We pass in all of the input states so that our game is only polling the hardware /// once per frame. We also pass the game's orientation because when using the accelerometer, /// we need to reverse our motion when the orientation is in the LandscapeRight orientation. /// </remarks> public void Update( GameTime gameTime, KeyboardState keyboardState, GamePadState gamePadState, TouchCollection touchState, AccelerometerState accelState, DisplayOrientation orientation) { GetInput(keyboardState, gamePadState, touchState, accelState, orientation); ApplyPhysics(gameTime); if (IsAlive && IsOnGround) { if (Math.Abs(Velocity.X) - 0.02f > 0) { sprite.PlayAnimation(runAnimation); } else { sprite.PlayAnimation(idleAnimation); } } // Clear input. movement = 0.0f; isJumping = false; }
private void HandleInput() { accelState = Accelerometer.GetState(); touchState = TouchPanel.GetState(); bool buttonTouched = false; KeyboardState keyboardState = Keyboard.GetState(); GamePadState gamepadState = GamePad.GetState(PlayerIndex.One); // Exit the game when back is pressed. if (gamepadState.Buttons.Back == ButtonState.Pressed) { Exit(); } //interpert touch screen presses foreach (TouchLocation location in touchState) { switch (location.State) { case TouchLocationState.Pressed: buttonTouched = true; break; case TouchLocationState.Moved: break; case TouchLocationState.Released: break; } } bool continuePressed = gamepadState.IsButtonDown(ContinueButton) || keyboardState.IsKeyDown(Keys.Space) || buttonTouched; // Perform the appropriate action to advance the game and // to get the player back to playing. if (!wasContinuePressed && continuePressed) { if (!level.Player.IsAlive) { level.StartNewLife(); } else if (level.TimeRemaining == TimeSpan.Zero) { if (level.ReachedExit) { LoadNextLevel(); } else { ReloadCurrentLevel(); } } } wasContinuePressed = continuePressed; }
/// <summary> /// Updates all objects in the world, performs collision between them, /// and handles the time limit with scoring. /// </summary> public void Update( GameTime gameTime, KeyboardState keyboardState, GamePadState gamePadState, TouchCollection touchState, AccelerometerState accelState, DisplayOrientation orientation) { // Pause while the player is dead or time is expired. // Pause while the player is dead or time is expired. if (!Player.IsAlive || TimeRemaining == TimeSpan.Zero) { // Still want to perform physics on the player. Player.ApplyPhysics(gameTime); } else if (ReachedExit) { // Animate the time being converted into points. int seconds = (int)Math.Round(gameTime.ElapsedGameTime.TotalSeconds * 100.0f); seconds = Math.Min(seconds, (int)Math.Ceiling(TimeRemaining.TotalSeconds)); timeRemaining -= TimeSpan.FromSeconds(seconds); score += seconds * PointsPerSecond; } else { timeRemaining -= gameTime.ElapsedGameTime; Player.Update(gameTime, keyboardState, gamePadState, touchState, accelState, orientation); UpdateMovableTiles(gameTime); UpdateGems(gameTime); // Falling off the bottom of the level kills the player. if (Player.BoundingRectangle.Top >= Height * Tile.Height) { OnPlayerKilled(null); } UpdateEnemies(gameTime); // The player has reached the exit if they are standing on the ground and // his bounding rectangle contains the center of the exit tile. They can only // exit when they have collected all of the gems. if (Player.IsAlive && Player.IsOnGround && Player.BoundingRectangle.Contains(exit)) { OnExitReached(); } } // Clamp the time remaining at zero. if (timeRemaining < TimeSpan.Zero) { timeRemaining = TimeSpan.Zero; } }
/// <summary> /// Handles input, performs physics, and animates the player sprite. /// </summary> /// <remarks> /// We pass in all of the input states so that our game is only polling the hardware /// once per frame. We also pass the game's orientation because when using the accelerometer, /// we need to reverse our motion when the orientation is in the LandscapeRight orientation. /// </remarks> public void Update( GameTime gameTime, KeyboardState keyboardState, GamePadState gamePadState, TouchCollection touchState, AccelerometerState accelState, DisplayOrientation orientation, Viewport viewport) { if (!IsRespawnable) { return; } GetInput(keyboardState, gamePadState, touchState, accelState, orientation); if (isThrowGrenade) { sprite.PlayAnimation(grenadeAnimation); m_bomb.Update(gameTime, Position, flip); } else { ApplyPhysics(gameTime); if (IsAlive && IsOnGround && !isRolling) { if (Math.Abs(Velocity.X) - 0.02f > 0) { sprite.PlayAnimation(runAnimation); } else { sprite.PlayAnimation(idleAnimation); } } //weapon.Update(gameTime, Position, flip); m_handgun.Update(gameTime, Position, flip); m_shotgun.Update(gameTime, Position, flip); m_knife.Update(gameTime, Position, flip); m_bomb.Update(gameTime, Position, flip); //Sprite effects if (pulseRed) { pulseRedTime += (float)gameTime.ElapsedGameTime.TotalSeconds; if (pulseRedTime > MAX_PULSE_TIME) { pulseRed = false; pulseRedTime = 0.0f; } } } // Clear input. movement = 0.0f; isJumping = false; }
/// <summary> /// Handles input, performs physics, and animates the player sprite. /// </summary> /// <remarks> /// We pass in all of the input states so that our game is only polling the hardware /// once per frame. We also pass the game's orientation because when using the accelerometer, /// we need to reverse our motion when the orientation is in the LandscapeRight orientation. /// </remarks> public void Update( GameTime gameTime, KeyboardState keyboardState, GamePadState gamePadState, TouchCollection touchState, AccelerometerState accelState, DisplayOrientation orientation) { GetInput(keyboardState, gamePadState, touchState, accelState, orientation); ApplyPhysics(gameTime); //Find nearest monster and highlight HighlightEnemy(); if (IsAlive && IsOnGround) { if (Math.Abs(Velocity.X) - 0.02f > 0) { sprite.PlayAnimation(runAnimation); } else { sprite.PlayAnimation(idleAnimation); } } //If the player is attacking if (isAttacking) { this.Attack(); } //If the player is depossessing if (dePossess) { target.isDead = true; possession = PossessionStatus.None; } // Clear input. movement = 0.0f; isJumping = false; isAttacking = false; dePossess = false; //Update attackRange this.attackRange.Y = BoundingRectangle.Y; if (direction == -1) { this.attackRange.X = BoundingRectangle.X - attackWidth; } else { this.attackRange.X = BoundingRectangle.X; } }
/// <summary> /// Gets player horizontal movement and jump commands from input. /// </summary> private void GetInput( KeyboardState keyboardState, GamePadState gamePadState, TouchCollection touchState, #if WINDOWS_PHONE AccelerometerState accelState, #endif DisplayOrientation orientation) { // Get analog horizontal movement. movement = gamePadState.ThumbSticks.Left.X * MoveStickScale; // Ignore small movements to prevent running in place. if (Math.Abs(movement) < 0.5f) { movement = 0.0f; } #if WINDOWS_PHONE // Move the player with accelerometer if (Math.Abs(accelState.Acceleration.Y) > 0.10f) { // set our movement speed movement = MathHelper.Clamp(-accelState.Acceleration.Y * AccelerometerScale, -1f, 1f); // if we're in the LandscapeLeft orientation, we must reverse our movement if (orientation == DisplayOrientation.LandscapeRight) { movement = -movement; } } #endif // If any digital horizontal movement input is found, override the analog movement. if (gamePadState.IsButtonDown(Buttons.DPadLeft) || keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A)) { movement = -1.0f; } else if (gamePadState.IsButtonDown(Buttons.DPadRight) || keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D)) { movement = 1.0f; } // Check if the player wants to jump. isJumping = gamePadState.IsButtonDown(JumpButton) || keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Up) || keyboardState.IsKeyDown(Keys.W) || touchState.AnyTouch(); }
public override void HandleInput(InputState input) { if (input == null) { throw new ArgumentNullException("input"); } // Look up inputs for the active player profile. int playerIndex = (int)ControllingPlayer.Value; // get all of our input states keyboardState = Keyboard.GetState(); gamePadState_1 = GamePad.GetState(PlayerIndex.One); gamePadState_2 = GamePad.GetState(PlayerIndex.Two); gamePadStates[0] = gamePadState_1; gamePadStates[1] = gamePadState_2; touchState = TouchPanel.GetState(); accelerometerState = Accelerometer.GetState(); // Exit the game when back is pressed. //if (gamePadState_1.Buttons.Back == ButtonState.Pressed) // Exit(); if (input.IsPauseGame(null)) { ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer); } else { foreach (Player player in PlatformerGame.Players) { bool continuePressed = keyboardState.IsKeyDown(Keys.Space) || gamePadStates[PlatformerGame.Players.IndexOf(player)].IsButtonDown(Buttons.A) || touchState.AnyTouch(); if (!player.IsAlive) { level.StartNewLife(player); } if (level.ReachedExit) { LoadNextLevel(); } } //check if both player is in the "no-man lands" if (players[0].Position.X == -999.9f && players[1].Position.X == -999.9f) { //players[attacker_id].Reset(Vector2.Zero); level.StartNewLife(players[attacker_id]); } } }
private void HandleInput() { // get all of our input states #if !IPHONE keyboardState = Keyboard.GetState(); gamePadState = GamePad.GetState(PlayerIndex.One); #endif touchState = TouchPanel.GetState(); #if WINDOWS_PHONE accelerometerState = Accelerometer.GetState(); #endif #if !IPHONE // Exit the game when back is pressed. if ((gamePadState.Buttons.Back == ButtonState.Pressed) || (keyboardState.IsKeyDown(Keys.Escape))) { Exit(); } #endif bool continuePressed = #if !IPHONE keyboardState.IsKeyDown(Keys.Space) || gamePadState.IsButtonDown(Buttons.A) || #endif touchState.AnyTouch(); // Perform the appropriate action to advance the game and // to get the player back to playing. if (!wasContinuePressed && continuePressed) { if (!level.Player.IsAlive) { level.StartNewLife(); } else if (level.TimeRemaining == TimeSpan.Zero) { if (level.ReachedExit) { LoadNextLevel(); } else { ReloadCurrentLevel(); } } } wasContinuePressed = continuePressed; }
/// <summary> /// Gets player horizontal movement and jump commands from input. /// </summary> private void GetInput( KeyboardState keyboardState, GamePadState gamePadState, TouchCollection touchState, AccelerometerState accelState, DisplayOrientation orientation) { bool touchJump = false; movement = 0.0f; // Process touch locations pressed = false; //esquerda = false; foreach (TouchLocation location in touchState) { switch (location.State) { case TouchLocationState.Pressed: { //fingerXOld = fingerX; //fingerYOld = fingerY; fingerX = (float)Math.Round(location.Position.Y);; fingerY = (float)Math.Round(location.Position.X); if (fingerX > 0) { pressed = true; } if (fingerY < (BoundingRectangle.X)) { touchJump = true; } break; } case TouchLocationState.Released: //Don't care about released state in this demo break; case TouchLocationState.Moved: { //fingerXOld = fingerX; //fingerYOld = fingerY; fingerX = (float)Math.Round(location.Position.X);; fingerY = (float)Math.Round(location.Position.Y); if (fingerX > 0) { pressed = true; } //if (!(fingerY > (BoundingRectangle.X - localBounds.Width))) // if (fingerY < (BoundingRectangle.TopY)) float pY = Position.Y; float pX = Position.X; float lY = localBounds.Y; float lX = localBounds.X; if ((fingerY + 30) < (BoundingRectangle.Y)) { // (int)Math.Round(Position.Y - sprite.Origin.Y) + localBounds.Y; touchJump = true; } break; } } } // Ignore small movements to prevent running in place. // if (Math.Abs(movement) < 0.5f) movement = 0.0f; if (fingerX != 0) { if (fingerX >= Position.X) { movement = 1.0f; } else { movement = -1.0f; } } // Check if the player wants to jump. isJumping = touchJump; }
/// <summary> /// Handles input, performs physics, and animates the player sprite. /// </summary> /// <remarks> /// We pass in all of the input states so that our game is only polling the hardware /// once per frame. We also pass the game's orientation because when using the accelerometer, /// we need to reverse our motion when the orientation is in the LandscapeRight orientation. /// </remarks> public void Update( GameTime gameTime, KeyboardState keyboardState, GamePadState gamePadState, TouchCollection touchState, AccelerometerState accelState, DisplayOrientation orientation) { velocity.X = 0; movement = 0; if (!IsPoweredDown) { MoveAcceleration = 700f; MaxMoveSpeed = 1750.0f; MaxJumpTime = 0.35f; } else { MoveAcceleration = 350f; MaxMoveSpeed = 1750.0f / 2; MaxJumpTime = 0.20f; } GetInput(keyboardState, gamePadState, touchState, accelState, orientation); if (IsPoweredUp) { powerUpTime = Math.Max(0.0f, powerUpTime - (float)gameTime.ElapsedGameTime.TotalSeconds); } if (IsPoweredDown) { powerDownTime = Math.Max(0.0f, powerDownTime - (float)gameTime.ElapsedGameTime.TotalSeconds); } if (!BoundingRectangle.Intersects(FingerRectangle) && pressed) { ApplyPhysics(gameTime); if (IsAlive && IsOnGround) { if (Math.Abs(Velocity.X) - 0.2f > 0) { sprite.PlayAnimation(runAnimation); } else { sprite.PlayAnimation(idleAnimation); } } } else if (isOnGround) { sprite.PlayAnimation(idleAnimation); pressed = false; } else { ApplyPhysics(gameTime); pressed = false; } // Clear input. movement = 0.0f; isJumping = false; if (isOnGround) { numberOfJumps = 0; } }
/// <summary> /// Updates all objects in the world, performs collision between them, /// and handles the time limit with scoring. /// </summary> public void Update( GameTime gameTime, KeyboardState keyboardState, GamePadState[] gamePadStates, TouchCollection touchState, AccelerometerState accelState, DisplayOrientation orientation, Viewport viewport) { // Play the bridge withdrawing animation if (isBridgeWithdrawing) { WithdrawBridge(gameTime); } else { // Pause while the player is dead or time is expired. foreach (Player player in PlatformerGame.Players) { if (!player.IsAlive)// || TimeRemaining == TimeSpan.Zero) { // Still want to perform physics on the player. player.ApplyPhysics(gameTime); } else if (ReachedExit) { // Animate the time being converted into points. int seconds = (int)Math.Round(gameTime.ElapsedGameTime.TotalSeconds * 100.0f); seconds = Math.Min(seconds, (int)Math.Ceiling(TimeRemaining.TotalSeconds)); timeRemaining -= TimeSpan.FromSeconds(seconds); score += seconds * PointsPerSecond; } else { timeRemaining -= gameTime.ElapsedGameTime; player.Update(gameTime, keyboardState, gamePadStates[player.PlayerId], touchState, accelState, orientation, viewport); UpdateGems(gameTime); // Falling off the bottom of the level kills the player. if (player.BoundingRectangle.Top >= Height * Tile.Height) { PlatformerGame.attacker_id = (player.PlayerId == 0) ? 1 : 0; fallingSound.Play(); if (!PlatformerGame.bossFight) { player.OnKilled(); player.Reset(); } else { game.displayEpicFail(); } } UpdateEnemies(gameTime); UpdateSpikes(gameTime); UpdateWinnerTile(gameTime); // The player has reached the exit if they are standing on the ground and // his bounding rectangle contains the center of the exit tile. They can only // exit when they have collected all of the gems. if (game.firstKill && player.PlayerId == PlatformerGame.attacker_id) { if (player.IsAlive && player.IsOnGround) { if (player.BoundingRectangle.Contains(exit[PlatformerGame.attacker_id])) { OnExitReached(); } if (player.BoundingRectangle.Contains(bridgeSwitch)) { WithdrawBridge(gameTime); } } } } } } // Clamp the time remaining at zero. if (timeRemaining < TimeSpan.Zero) { //timeRemaining = TimeSpan.Zero; timeRemaining = TimeSpan.FromMinutes(2.0); } }
/// <summary> /// Gets player horizontal movement and jump commands from input. /// </summary> private void GetInput(AccelerometerState accelState, TouchCollection touchState) { // Get input state. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One); KeyboardState keyboardState = Keyboard.GetState(); // Get analog horizontal movement. movement = gamePadState.ThumbSticks.Left.X * MoveStickScale; // Ignore small movements to prevent running in place. if (Math.Abs(movement) < 0.5f) { movement = 0.0f; } if (Math.Abs(accelState.Acceleration.X) > 0.10f) { if (accelState.Acceleration.X > 0.0f) { movement = 1.0f; } else { movement = -1.0f; } } //override digital if touch input is found // Process touch locations. bool touchJump = false; foreach (TouchLocation location in touchState) { switch (location.State) { case TouchLocationState.Pressed: touchJump = true; break; case TouchLocationState.Moved: break; case TouchLocationState.Released: break; } } // If any digital horizontal movement input is found, override the analog movement. if (gamePadState.IsButtonDown(Buttons.DPadLeft) || keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A)) { movement = -1.0f; } else if (gamePadState.IsButtonDown(Buttons.DPadRight) || keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D)) { movement = 1.0f; } // Check if the player wants to jump. isJumping = gamePadState.IsButtonDown(JumpButton) || keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Up) || keyboardState.IsKeyDown(Keys.W) || touchJump; }
private void HandleInput() { // get all of our input states keyboardState = Keyboard.GetState(); gamePadState = GamePad.GetState(PlayerIndex.One); touchState = TouchPanel.GetState(); accelerometerState = Accelerometer.GetState(); //Pause game when p is pressed if (keyboardState.IsKeyDown(Keys.P) && state == Gamestate.Game) { state = Gamestate.Pause; } //Reset Level if paused and r is pressed if (keyboardState.IsKeyDown(Keys.R) && state == Gamestate.Pause) { ReloadCurrentLevel(); state = Gamestate.Game; } //Resume Game upon Enter if (keyboardState.IsKeyDown(Keys.Space) && state != Gamestate.Game) { state = Gamestate.Game; } // Exit the game when back is pressed. if (gamePadState.Buttons.Back == ButtonState.Pressed) { Exit(); } bool continuePressed = keyboardState.IsKeyDown(Keys.Space) || gamePadState.IsButtonDown(Buttons.A) || touchState.AnyTouch(); // Perform the appropriate action to advance the game and // to get the player back to playing. if (!wasContinuePressed && continuePressed) { if (!level.Player.IsAlive) { level.StartNewLife(); ReloadCurrentLevel(); } else if (level.TimeRemaining == TimeSpan.Zero) { if (level.ReachedExit) { LoadNextLevel(); } else { ReloadCurrentLevel(); } } } wasContinuePressed = continuePressed; }
/// <summary> /// Gets player horizontal movement and jump commands from input. /// </summary> private void GetInput( KeyboardState keyboardState, GamePadState gamePadState, TouchCollection touchState, AccelerometerState accelState, DisplayOrientation orientation) { // Get analog horizontal movement. movement = gamePadState.ThumbSticks.Left.X * MoveStickScale; // Ignore small movements to prevent running in place. if (Math.Abs(movement) < 0.5f) { movement = 0.0f; } // Move the player with accelerometer if (Math.Abs(accelState.Acceleration.Y) > 0.10f) { // set our movement speed movement = MathHelper.Clamp(-accelState.Acceleration.Y * AccelerometerScale, -1f, 1f); // if we're in the LandscapeLeft orientation, we must reverse our movement if (orientation == DisplayOrientation.LandscapeRight) { movement = -movement; } } // If any digital horizontal movement input is found, override the analog movement. if (keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A)) { movement = -1.0f; } else if (keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D)) { movement = 1.0f; } // Weapon switching if (gamePadState.IsButtonDown(Buttons.DPadLeft)) { weapon.Reset(); weapon = m_handgun; } else if (gamePadState.IsButtonDown(Buttons.DPadUp)) { weapon.Reset(); weapon = m_shotgun; } else if (gamePadState.IsButtonDown(Buttons.DPadRight)) { weapon.Reset(); weapon = m_knife; } if (gamePadState.IsButtonDown(SwitchButton) && !old_gamePadState.IsButtonDown(SwitchButton)) { weapon.Reset(); if (weapon == m_handgun) { weapon = m_shotgun; } else if (weapon == m_shotgun) { weapon = m_knife; } else { weapon = m_handgun; } } // Check if the player wants to jump. isJumping = gamePadState.IsButtonDown(JumpButton) || keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Up) || keyboardState.IsKeyDown(Keys.W) || touchState.AnyTouch(); if (gamePadState.IsButtonDown(RollButton) && !old_gamePadState.IsButtonDown(RollButton)) { if (canRollAgain && isOnGround) { isRolling = true; canRollAgain = false; } } isThrowGrenade = false; if (!isRolling) { // release the charged bomb if (old_gamePadState.IsButtonDown(GrenadeButton) && !gamePadState.IsButtonDown(GrenadeButton)) { m_bomb.Shoot(); } // charge up the bomb throw if button held if (gamePadState.IsButtonDown(GrenadeButton) && isOnGround && m_bomb.CanAttack) { isThrowGrenade = true; m_bomb.Charging(this.position); } // other attacks else if ((gamePadState.IsButtonDown(FireButton) && !old_gamePadState.IsButtonDown(FireButton)) || (keyboardState.IsKeyDown(Keys.Z)) || (gamePadState.IsButtonDown(FireButton2) && !old_gamePadState.IsButtonDown(FireButton2))) { weapon.Shoot(); } } old_gamePadState = gamePadState; }
/// <summary> /// Gets player horizontal movement and jump commands from input. /// </summary> private void GetInput( KeyboardState keyboardState, GamePadState gamePadState, TouchCollection touchState, AccelerometerState accelState, DisplayOrientation orientation) { // Get analog horizontal movement. movement = gamePadState.ThumbSticks.Left.X * MoveStickScale; // Ignore small movements to prevent running in place. if (Math.Abs(movement) < 0.5f) { movement = 0.0f; } // Move the player with accelerometer if (Math.Abs(accelState.Acceleration.Y) > 0.10f) { // set our movement speed movement = MathHelper.Clamp(-accelState.Acceleration.Y * AccelerometerScale, -1f, 1f); // if we're in the LandscapeLeft orientation, we must reverse our movement if (orientation == DisplayOrientation.LandscapeRight) { movement = -movement; } } // If any digital horizontal movement input is found, override the analog movement. if (gamePadState.IsButtonDown(Buttons.DPadLeft) || keyboardState.IsKeyDown(Keys.Left) || keyboardState.IsKeyDown(Keys.A)) { movement = -1.0f; } else if (gamePadState.IsButtonDown(Buttons.DPadRight) || keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D)) { movement = 1.0f; } //Set direction and pubmove to movement if (movement != 0) { this.direction = movement; } this.pubMove = movement; // Check if the player wants to jump. isJumping = gamePadState.IsButtonDown(JumpButton) || /*keyboardState.IsKeyDown(Keys.Space) ||*/ keyboardState.IsKeyDown(Keys.Up) || keyboardState.IsKeyDown(Keys.W) || touchState.AnyTouch(); //Check if the player is attacking isAttacking = keyboardState.IsKeyDown(Keys.Space); //Check to see if player wants to release possession dePossess = possession != PossessionStatus.None && keyboardState.IsKeyDown(Keys.V); }