Beispiel #1
0
        private void FireKeyboardEvents()
        {
            // Check through each key in the key list
            foreach (Keys key in KeyList)
            {
                // Is the key currently down?
                if (CurrentKeyboardState.IsKeyDown(key))
                {
                    // Fire the OnKeyDown event
                    OnKeyDown?.Invoke(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                }

                // Has the key been released? (Was down and is now up)
                if (PrevKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
                {
                    OnKeyUp?.Invoke(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                }

                // Has the key been pressed? (Was up and is now down)
                if (PrevKeyboardState.IsKeyUp(key) && CurrentKeyboardState.IsKeyDown(key))
                {
                    OnKeyPressed?.Invoke(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                }
            }
        }
Beispiel #2
0
        private void FireKeyboardEvents()
        {
            foreach (var key in KeyList)
            {
                // Is the key currently down?
                if (CurrentKeyboardState.IsKeyDown(key))
                {
                    // Fire the OnKeyDown event
                    if (OnKeyDown != null)
                    {
                        OnKeyDown(this, new KeyboardEventArgs(key, CurrentKeyboardState,
                                                              PrevKeyboardState));
                    }
                }

                // Has the key been released? (Was down and is now up)
                if (PrevKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
                {
                    // Fire the OnKeyUp event
                    if (OnKeyUp != null)
                    {
                        OnKeyUp(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                    }
                }

                // Has the key been pressed? (Was up and is now down)
                if (PrevKeyboardState.IsKeyUp(key) && CurrentKeyboardState.IsKeyDown(key))
                {
                    if (OnKeyPressed != null)
                    {
                        OnKeyPressed(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                    }
                }
            }
        }
        private void FireKeyboardEvents()
        {
            foreach (Keys key in KeyList)
            {
                if (CurrentKeyboardState.IsKeyDown(key))
                {
                    if (OnKeyDown != null)
                    {
                        OnKeyDown(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                    }
                }

                if (PrevKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
                {
                    if (OnKeyUp != null)
                    {
                        OnKeyUp(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                    }
                }

                if (PrevKeyboardState.IsKeyUp(key) && CurrentKeyboardState.IsKeyDown(key))
                {
                    if (OnKeyPressed != null)
                    {
                        OnKeyPressed(this, new KeyboardEventArgs(key, CurrentKeyboardState, PrevKeyboardState));
                    }
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Used for determining if a keyType was just released.
        /// </summary>
        /// <param name="key">Key to check if released.</param>
        /// <returns>Returns true if keyType is up and last frame is down.</returns>
        public static bool KeyJustReleased(Keys key)
        {
            if (_active && OldKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
            {
                return(true);
            }

            return(false);
        }
        //
        // Events that should be fired based on the interaction/input received

        private void FireKeyboardEvents()
        {
            foreach (Keys key in KeyList)
            {
                // Is key currently down?
                if (CurrentKeyboardState.IsKeyDown(key))
                {
                    OnKeyDown?.Invoke(key);
                }

                // Has the key been released?
                if (PrevKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
                {
                    OnKeyUp?.Invoke(key);
                }

                // Key has been held
                if (PrevKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyDown(key))
                {
                    OnKeyPressed?.Invoke(key);
                }
            }
        }
Beispiel #6
0
 public static bool IsKeyReleased(Keys key)
 {
     return(CurrentKeyboardState.IsKeyUp(key) && PreviousKeyboardState.IsKeyDown(key));
 }
 public static bool KeyUp(Keys key)
 {
     return(CurrentKeyboardState.IsKeyUp(key));
 }
Beispiel #8
0
        /// <summary>
        /// Applies movement to the player character
        /// </summary>
        /// <param name="gameTime"></param>
        /// <returns>The new rectangle where the character needs to be drawn</returns>
        public override Vector2 ApplyMovement(GameTime gameTime)
        {
            Vector2 returnValue;

            // Apply movement based on keys pressed
            if ((CurrentKeyboardState.IsKeyDown(Keys.A)) && (PreviousKeyboardState.IsKeyUp(Keys.A)) ||
                (CurrentKeyboardState.IsKeyDown(Keys.Left)) && (PreviousKeyboardState.IsKeyUp(Keys.Left)))
            {
                base.MovementAppliedTo = MovementAppliedTo.Left;
                base.SpriteEffect      = SpriteEffects.FlipHorizontally;
            }
            else if ((CurrentKeyboardState.IsKeyDown(Keys.D)) && (PreviousKeyboardState.IsKeyUp(Keys.D)) ||
                     (CurrentKeyboardState.IsKeyDown(Keys.Right)) && (PreviousKeyboardState.IsKeyUp(Keys.Right)))
            {
                base.MovementAppliedTo = MovementAppliedTo.Right;
                base.SpriteEffect      = SpriteEffects.None;
            }
            else if ((CurrentKeyboardState.IsKeyUp(Keys.A)) && (PreviousKeyboardState.IsKeyDown(Keys.A)) ||
                     (CurrentKeyboardState.IsKeyUp(Keys.Left)) && (PreviousKeyboardState.IsKeyDown(Keys.Left)))
            {
                if ((!base.JumpInProgress) && (!base.HasJumped))
                {
                    base.MovementAppliedTo = MovementAppliedTo.None;
                }
            }
            else if ((CurrentKeyboardState.IsKeyUp(Keys.D)) && (PreviousKeyboardState.IsKeyDown(Keys.D)) ||
                     (CurrentKeyboardState.IsKeyUp(Keys.Right)) && (PreviousKeyboardState.IsKeyDown(Keys.Right)))
            {
                if ((!base.JumpInProgress) && (!base.HasJumped))
                {
                    base.MovementAppliedTo = MovementAppliedTo.None;
                    base.SpriteEffect      = SpriteEffects.None;
                }
            }
            // Implement the jump feature
            if (base.GravityDirection == GravityDirection.Down)
            {
                if (((CurrentKeyboardState.IsKeyDown(Keys.Space)) && (PreviousKeyboardState.IsKeyUp(Keys.Space)) ||
                     (CurrentKeyboardState.IsKeyDown(Keys.Up)) && (PreviousKeyboardState.IsKeyUp(Keys.Up))) &&
                    ((base.HitObstacle == HitObstacle.FromTop) || (this.JumpCount == 1)))
                {
                    base.TimeSinceJump = gameTime.ElapsedGameTime.Milliseconds;

                    base.MovementAppliedTo = MovementAppliedTo.Up;

                    base.HitObstacle = HitObstacle.None;

                    if (this.PlatformVerticalAcceleration > 0)
                    {
                        base.GravitationalVelocity -= this.PlatformVerticalAcceleration;
                    }

                    if (base.HasJumped == false)
                    {
                        base.HasJumped = true;
                        SoundEffectInstances["JumpSound"].Play();
                    }

                    if (base.JumpInProgress == false)
                    {
                        base.JumpInProgress = true;
                    }
                }
            }
            else if (base.GravityDirection == GravityDirection.Up)
            {
                if (((CurrentKeyboardState.IsKeyDown(Keys.Space)) && (PreviousKeyboardState.IsKeyUp(Keys.Space)) ||
                     (CurrentKeyboardState.IsKeyUp(Keys.Up)) && (PreviousKeyboardState.IsKeyDown(Keys.Up))) &&
                    ((base.HitObstacle == HitObstacle.FromTop) || (this.JumpCount == 1)))
                {
                    base.MovementAppliedTo = MovementAppliedTo.Down;

                    base.HitObstacle = HitObstacle.None;

                    if (base.HasJumped == false)
                    {
                        base.HasJumped = true;
                    }

                    if (base.JumpInProgress == false)
                    {
                        base.JumpInProgress = true;
                    }
                }
            }
            // Cancel movement if nothing is being pressed (because of velocity)
            if ((CurrentKeyboardState.IsKeyUp(Keys.A)) && (CurrentKeyboardState.IsKeyUp(Keys.D)) &&
                (CurrentKeyboardState.IsKeyUp(Keys.Left)) && (CurrentKeyboardState.IsKeyUp(Keys.Right)) &&
                (base.Falling == false) && (base.HasJumped == false) && (base.JumpInProgress == false))
            {
                base.MovementAppliedTo = MovementAppliedTo.None;
            }
            // Attempt at working with gametime
            if (base.TimeSinceLastUpdate > 100)
            {
                this.UpdateSprite();
                base.SelectSprite(base.CurrentSpriteIndex);

                base.TimeSinceLastUpdate = 0;
            }

            base.CalculateGravity(gameTime);
            base.CalculateMovement(gameTime);

            returnValue = new Vector2(
                this.DrawLocation.X + base.MovementVelocity,
                this.DrawLocation.Y + base.GravitationalVelocity
                );

            return(returnValue);
        }
Beispiel #9
0
 public static bool OneReleased(Keys key)
 {
     return(CurrentKeyboardState.IsKeyUp(key) && OldKeyboardState.IsKeyDown(key));
 }
 public bool IsKeyReleased(Keys key) => PreviousKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key);
Beispiel #11
0
 /// <summary>
 /// Checks if any of the keys specified are pressed/toggled
 /// </summary>
 public bool AnyKeysPressed(params Keys[] keys)
 {
     return(keys.Any(k => CurrentKeyboardState.IsKeyUp(k) && PreviousKeyboardState.IsKeyDown(k)));
 }
Beispiel #12
0
 /// <summary>
 /// Checks if a given key has been toggled (Was pressed last state, but now isn't)
 /// </summary>
 public bool WasKeyPressed(Keys key)
 {
     return(CurrentKeyboardState.IsKeyUp(key) && PreviousKeyboardState.IsKeyDown(key));
 }
Beispiel #13
0
 public static bool IsNewKeyRelease(Keys key)
 {
     return(LastKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key));
 }
Beispiel #14
0
 /// <summary>
 /// Gets whether the given key was just released.
 /// </summary>
 /// <param name="key">The given key.</param>
 /// <returns>True if it was just released, false otherwise.</returns>
 public static bool GetKeyUp(Keys key)
 {
     return(PreviousKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key));
 }
Beispiel #15
0
 public static bool WasJustReleased(Keys key)
 {
     return(OldKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key));
 }
Beispiel #16
0
 public static bool IsNewKeyPress(Keys key)
 {
     return(CurrentKeyboardState.IsKeyUp(key) && LastKeyboardState.IsKeyDown(key));
 }
Beispiel #17
0
 public bool IsNewKeyRelease(Keys key)
 {
     return(CurrentKeyboardState.IsKeyUp(key) && _previousKeyboardState.IsKeyDown(key));
 }