void TriggerAction(PlayerAction action) { Log.Debug("TriggerAction({0})", action); switch (action) { case PlayerAction.StartJump: if (State == PlayerState.OnGround) { SetState(PlayerState.InAir); JumpForceActive = true; JumpForceTime = 0.0f; } break; case PlayerAction.EndJump: JumpForceActive = false; break; case PlayerAction.Land: if (State == PlayerState.InAir) { SetState(PlayerState.OnGround); } break; case PlayerAction.Shoot: if (State != PlayerState.Dead) { Weapon.Shoot(); } break; case PlayerAction.Die: SetState(PlayerState.Dead); GameOver(); break; default: throw new Exception(string.Format("PlayerAction not handled: {0}", action)); } }
/// <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; }