Example #1
0
        /// <summary>
        /// Handles the Keyboard and GamePad inputs
        /// </summary>
        private void HandleInput()
        {
            // get all of our input states
            keyboardState      = Keyboard.GetState();
            gamePadState       = GamePad.GetState(PlayerIndex.One);
            touchState         = TouchPanel.GetState();
            accelerometerState = Accelerometer.GetState();

            // Exit the game when back is pressed.
            if (gamePadState.Buttons.Back == ButtonState.Pressed || keyboardState.IsKeyDown(Keys.Escape))
            {
                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)
                {
                    this.life--;
                    if (this.life >= 1)
                    {
                        level.StartNewLife();
                        level.HealthPlayer = 150;
                    }
                    else
                    {
                        pause.ExitClicked = true;
                    }
                }
                else if (level.TimeRemaining == TimeSpan.Zero)
                {
                    if (level.ReachedExit)
                    {
                        LoadNextLevel(this);
                        timerSwitchGame = TimeSpan.FromSeconds(0);
                    }
                    else if (life > 1)
                    {
                        ReloadCurrentLevel();
                    }
                    else if (life == 1)
                    {
                        Exit();
                    }
                }
            }

            wasContinuePressed = continuePressed;
        }
Example #2
0
        /// <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))
            {
                movement = -1.0f;
            }
            else if (gamePadState.IsButtonDown(Buttons.DPadRight) ||
                     keyboardState.IsKeyDown(Keys.Right))
            {
                movement = 1.0f;
            }

            // Check if the player wants to jump.
            isJumping =
                gamePadState.IsButtonDown(JumpButton) ||
                keyboardState.IsKeyDown(Keys.Space) ||
                keyboardState.IsKeyDown(Keys.Up) ||
                touchState.AnyTouch();
        }
Example #3
0
        /// <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);

            if (previousKeyboardState.IsKeyUp(Keys.E) && keyboardState.IsKeyDown(Keys.E))
            {
                if (AttackTime != MaxAttackTime && timerAttack <= TimeSpan.FromSeconds(0.02))
                {
                    timerAttack += TimeSpan.FromSeconds(MaxAttackTime / 10);
                    isAttacking  = true;
                    AttackTime   = MaxAttackTime;
                }
            }
            // Here we fix the infinite attacking bug
            if (keyboardState.IsKeyUp(Keys.E))
            {
                timerAttack = TimeSpan.FromSeconds(0);
            }
            DoAttack(gameTime);


            ApplyPhysics(gameTime);

            if (IsAlive && IsOnGround)
            {
                if (isAttacking)
                {
                    sprite.PlayAnimation(attackAnimation);
                }
                else
                {
                    if (Math.Abs(Velocity.X) - 0.02f > 0)
                    {
                        sprite.PlayAnimation(runAnimation);
                    }
                    else
                    {
                        sprite.PlayAnimation(idleAnimation);
                    }
                }
            }

            //Basically saying if the player is alive, and is on the ground
            //then you can play the animation. and if he is in the air, then it applies velocity


            // Clear input.
            movement = 0.0f;

            isJumping = false;
            if (isOnGround)
            {
                numberOfJumps = 0;
            }
            if (timerAttack >= TimeSpan.FromSeconds(0.2))
            {
                timerAttack = TimeSpan.FromSeconds(0);
            }
        }