// Switch - the player has to be near and respond to a key press public override void ChangeState(Player p, KeyboardState keyState, InputManager inputManager) { RectangleF other = p.BoundingRectangle; Boolean touching = other.Intersects(this.BoundingRectangle); if (inputManager.IsNewPress(Keys.E) && touching) p.GotoLevel(levelIndex); }
public PlatformerGame() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; inputManager = new InputManager(); #if WINDOWS_PHONE graphics.IsFullScreen = true; TargetElapsedTime = TimeSpan.FromTicks(333333); #endif Accelerometer.Initialize(); }
// Switch - the player has to be near and respond to a key press public override void ChangeState(Player p, KeyboardState keyState, InputManager inputManager) { RectangleF other = p.BoundingRectangle; Boolean touching = other.Intersects(this.BoundingRectangle); if (inputManager.IsNewPress(Keys.E) && touching) { on = !on; if (on) { buttonDown.Play(); } else { buttonUp.Play(); } foreach (IActivatable responder in list) { responder.ChangeState(); } } }
/// <summary> /// Gets player horizontal movement and jump commands from input. /// </summary> private void GetInput( KeyboardState keyboardState, GamePadState gamePadState, TouchCollection touchState, AccelerometerState accelState, DisplayOrientation orientation, InputManager inputManager) { // Get analog horizontal movement. movement.X = gamePadState.ThumbSticks.Left.X * MoveStickScale; // Get analog vertical movement. movement.Y = gamePadState.ThumbSticks.Left.Y * MoveStickScale; // Ignore small movements to prevent running in place. if (Math.Abs(movement.X) < 0.5f) { movement.X = 0.0f; } if (Math.Abs(movement.Y) < 0.5f) { movement.Y = 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.X = -1.0f; } else if (gamePadState.IsButtonDown(Buttons.DPadRight) || keyboardState.IsKeyDown(Keys.Right) || keyboardState.IsKeyDown(Keys.D)) { movement.X = 1.0f; } // Handle ladder up input if (gamePadState.IsButtonDown(Buttons.DPadUp) || keyboardState.IsKeyDown(Keys.Up) || keyboardState.IsKeyDown(Keys.W)) { isClimbing = false; //makes sure the players position is aligned to the center of the ladder if (IsAlignedToLadder()) { //need to check the tile behind the player, not what he is standing on if (level.GetTileCollisionBehindPlayer(position) == TileCollision.Ladder) { isClimbing = true; isJumping = false; isOnGround = false; movement.Y = -1.0f; } } } // Handle ladder down input else if (gamePadState.IsButtonDown(Buttons.DPadDown) || keyboardState.IsKeyDown(Keys.Down) || keyboardState.IsKeyDown(Keys.S)) { isClimbing = false; //makes sure the players position is aligned to the center of the ladder if (IsAlignedToLadder()) { //need to check the tile that the player is standing on if (level.GetTileCollisionBelowPlayer(this.Position) == TileCollision.Ladder) { isClimbing = true; isJumping = false; isOnGround = false; movement.Y = 2.0f; } } } // Check if the player wants to jump. // Change this so that we only want to jump if it is a new press - i.e. KeyPressDown() // //isJumping = gamePadState.IsButtonDown(JumpButton) || keyboardState.IsKeyDown(Keys.Space) || keyboardState.IsKeyDown(Keys.Up) || // keyboardState.IsKeyDown(Keys.W) || touchState.AnyTouch(); isJumping = inputManager.IsNewPress(JumpButton) || inputManager.IsNewPress(Keys.Space) || inputManager.IsNewPress(Keys.Up) || inputManager.IsNewPress(Keys.W); }
/// <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, InputManager inputManager) { // Hook for InputManager inputManager.Update(); if (IsAlive) { GetInput(keyboardState, gamePadState, touchState, accelState, orientation, inputManager); } ApplyPhysics(gameTime); if (IsAlive) { if (isOnGround) { if (Math.Abs(Velocity.X) - 0.02f > 0) { if (Velocity.X > 0) { sprite.PlayAnimation(runRightAnimation); isRight = true; } else { sprite.PlayAnimation(runLeftAnimation); isRight = false; } } else { idle(); } } else if (isClimbing) { if (Velocity.Y > 0.02f) { sprite.PlayAnimation(ladderDownAnimation); } else if (Velocity.Y < 0.02f) { sprite.PlayAnimation(ladderUpAnimation); } if (Math.Abs(Velocity.Y) <= 0.02f) { sprite.Pause(); } } else if (!isJumping) { if (Velocity.X > 0) { sprite.PlayAnimation(fallRightAnimation); } else if (Velocity.X < 0) { sprite.PlayAnimation(fallLeftAnimation); } else { sprite.PlayAnimation(isRight ? fallRightAnimation : fallLeftAnimation); } } } // Check for fall damage checkFallDamage(); // Clear input. movement = Vector2.Zero; wasClimbing = isClimbing; isClimbing = false; isJumping = false; }
//change state sets the activator to on and off depending on players position and keystrokes public abstract void ChangeState(Player c, KeyboardState keyState, InputManager inputManager);