/// <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> /// Loads the player sprite sheet and sounds. /// </summary> public void LoadContent() { // Load animated textures. idleAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/cop_idle"), 0.1f, true); runAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/cop_running"), 0.1f, true); jumpAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/cop_jump"), 0.1f, false); celebrateAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/Celebrate"), 0.1f, false); dieAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/cop_die"), 0.1f, false); rollAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/cop_roll2"), 0.1f, false); grenadeAnimation = new Animation(Level.Content.Load<Texture2D>("Sprites/Player/cop_grenade"), 0.1f, false); killedSound = Level.Content.Load<SoundEffect>("Sounds/killedSound"); jumpSound = Level.Content.Load<SoundEffect>("Sounds/jumpSound"); rollSound = Level.Content.Load<SoundEffect>("Sounds/rollSound"); hitSound = Level.Content.Load<SoundEffect>("Sounds/hitSound"); // Calculate bounds within texture size. int width = (int)(idleAnimation.FrameWidth * 0.4); int left = (idleAnimation.FrameWidth - width) / 2; int height = (int)(idleAnimation.FrameWidth * 0.8); int top = idleAnimation.FrameHeight - height; localBounds = new Rectangle(left, top, width, height); m_handgun = new HandGun(level, position, this); m_shotgun = new Shotgun(level, position, this); m_knife = new Knife(level, position, this); m_bomb = new Grenade(level, position, this); weapon = m_handgun; font = level.Content.Load<SpriteFont>("Fonts/Hud"); }