Example #1
0
        public void Update(double TimeElapsed, Player p)
        {
            time = time - TimeElapsed;
            if (IsAlive)
            {
                if (time <= 0 && loc.Y > 0)
                {
                    Fireball f = new Fireball(this, p, _fball);
                    _enemyFireballs.Add(f);
                    time = Constants.INT_BETWEEN_SHOTS;
                }
            }
            foreach (Fireball f in _enemyFireballs)
            {
                f.Update();
            }

            // check collision against player
            p.CollideWithFireball(_enemyFireballs, this);
        }
        /// <summary>
        /// Called during update in game class
        /// </summary>
        public void Update(double timeElapsed, MouseState mState, MouseState prevMstate, KeyboardState kbState, KeyboardState prevKbState)
        {
            //Checks for a double tap for a dash and also the fireball command
            if (pState != PlayerState.CrouchingLeft && pState != PlayerState.CrouchingRight)
            {
                if (gM.KeyReleased(Keys.D))
                {
                    dashPrimedRight = true;
                    dashPrimedLeft  = false;
                    timeSinceTap    = 0;
                }
                if (gM.KeyReleased(Keys.A))
                {
                    dashPrimedLeft  = true;
                    dashPrimedRight = false;
                    timeSinceTap    = 0;
                }

                timeSinceTap += timeElapsed;
                if (timeSinceTap >= Constants.DOUBLETAP_TIME)
                {
                    dashPrimedRight = false;
                    dashPrimedLeft  = false;
                }
                if (gM.SingleMouseClickLeft())
                {
                    _shoot.Play();
                    Fireball f = new Fireball(this, mState);
                    f.FireBallT2D = _fball;
                    projectiles.Add(f);
                }
            }

            #region pState FSM
            switch (pState)
            {
                #region IdleRight
            case PlayerState.IdleRight:
            {
                canDoubleJump = true;
                if (impact == false)
                {
                    XVelocity = 0;
                }

                if (attacheWall != null)
                {
                    yVelocity = 0;
                }
                if (YVelocity != 0)
                {
                    pState = PlayerState.JumpingRight;
                }
                if (gM.SingleKeyPress(Keys.D) && dashPrimedRight)
                {
                    attacheWall = null;
                    YLoc--;
                    pState = PlayerState.DashingRight;
                }
                else if (kbState.IsKeyDown(Keys.D))
                {
                    pState = PlayerState.WalkingRight;
                }
                if (kbState.IsKeyDown(Keys.A))
                {
                    pState = PlayerState.WalkingLeft;
                }
                if (kbState.IsKeyDown(Keys.S))
                {
                    pState = PlayerState.CrouchingRight;
                }
                if (kbState.IsKeyDown(Keys.W) && onPlatform)
                {
                    pState    = PlayerState.JumpingRight;
                    yVelocity = Constants.PLAYER_JUMP_SPEED;
                }
                else
                {
                    if (attacheWall != null)
                    {
                        YLoc = attacheWall.top - Constants.PLAYER_HEIGHT + 1;
                    }
                }
                break;
            }

                #endregion
                #region IdleLeft
            case PlayerState.IdleLeft:
            {
                canDoubleJump = true;
                if (impact == false)
                {
                    XVelocity = 0;
                }

                if (attacheWall != null)
                {
                    yVelocity = 0;
                }

                if (YVelocity != 0)
                {
                    pState = PlayerState.JumpingLeft;
                }
                if (kbState.IsKeyDown(Keys.D) && !collidingRight)
                {
                    pState = PlayerState.WalkingRight;
                }
                if (gM.SingleKeyPress(Keys.A) && dashPrimedLeft)
                {
                    attacheWall = null;
                    YLoc--;
                    pState = PlayerState.DashingLeft;
                }
                else if (kbState.IsKeyDown(Keys.A) && !collidingLeft)
                {
                    pState = PlayerState.WalkingLeft;
                }
                if (kbState.IsKeyDown(Keys.S))
                {
                    pState = PlayerState.CrouchingLeft;
                }
                if (kbState.IsKeyDown(Keys.W) && onPlatform)
                {
                    pState    = PlayerState.JumpingLeft;
                    yVelocity = Constants.PLAYER_JUMP_SPEED;
                }
                else
                {
                    if (attacheWall != null)
                    {
                        YLoc = attacheWall.top - Constants.PLAYER_HEIGHT + 1;
                    }
                }
                break;
            }

                #endregion
                #region WalkingRight
            case PlayerState.WalkingRight:
            {
                canDoubleJump = true;
                xVelocity     = Constants.WALK_SPEED;
                Step();
                if (attacheWall != null)
                {
                    yVelocity = 0;
                }
                if (YVelocity != 0)
                {
                    pState = PlayerState.JumpingRight;
                }
                if (gM.SingleKeyPress(Keys.D) && dashPrimedRight)
                {
                    attacheWall = null;
                    YLoc--;
                    pState = PlayerState.DashingRight;
                }
                if (kbState.IsKeyUp(Keys.D))
                {
                    pState = PlayerState.IdleRight;
                }
                if (kbState.IsKeyDown(Keys.S))
                {
                    pState = PlayerState.CrouchingRight;
                }
                if (kbState.IsKeyDown(Keys.W) && onPlatform)
                {
                    pState    = PlayerState.JumpingRight;
                    yVelocity = Constants.PLAYER_JUMP_SPEED;
                }
                else
                {
                    if (attacheWall != null)
                    {
                        YLoc = attacheWall.top - Constants.PLAYER_HEIGHT + 1;
                    }
                }
                if (collidingRight)
                {
                    pState = PlayerState.IdleRight;
                }
                break;
            }

                #endregion
                #region WalkingLeft
            case PlayerState.WalkingLeft:
            {
                canDoubleJump = true;
                xVelocity     = -Constants.WALK_SPEED;
                Step();
                if (attacheWall != null)
                {
                    yVelocity = 0;
                }
                if (YVelocity != 0)
                {
                    pState = PlayerState.JumpingLeft;
                }
                if (YVelocity != 0)
                {
                    pState = PlayerState.JumpingRight;
                }
                if (gM.SingleKeyPress(Keys.A) && dashPrimedLeft)
                {
                    attacheWall = null;
                    YLoc--;
                    pState = PlayerState.DashingLeft;
                }
                if (kbState.IsKeyUp(Keys.A))
                {
                    pState = PlayerState.IdleLeft;
                }
                if (kbState.IsKeyDown(Keys.S))
                {
                    pState = PlayerState.CrouchingLeft;
                }
                if (kbState.IsKeyDown(Keys.W) && onPlatform)
                {
                    pState    = PlayerState.JumpingRight;
                    yVelocity = Constants.PLAYER_JUMP_SPEED;
                }
                else
                {
                    if (attacheWall != null)
                    {
                        YLoc = attacheWall.top - Constants.PLAYER_HEIGHT + 1;
                    }
                }
                if (collidingLeft)
                {
                    pState = PlayerState.IdleLeft;
                }
                break;
            }

                #endregion
                #region JumpingRight
            case PlayerState.JumpingRight:
            {
                if (YVelocity == 0)
                {
                    pState = PlayerState.IdleRight;
                }
                else
                {
                    attacheWall = null;
                }
                // The double jump
                if (gM.SingleKeyPress(Keys.W) && canDoubleJump)
                {
                    _doubleJump.Play(.3f, 0, 0);
                    YVelocity     = Constants.PLAYER_JUMP_SPEED;
                    canDoubleJump = false;
                    dJNow         = true;
                }
                if (kbState.IsKeyDown(Keys.S))
                {
                    pState = PlayerState.CrouchingRight;
                }
                if (kbState.IsKeyDown(Keys.A) && kbState.IsKeyUp(Keys.D))
                {
                    pState    = PlayerState.JumpingLeft;
                    xVelocity = -Constants.WALK_SPEED;
                }
                if (gM.SingleKeyPress(Keys.D) && dashPrimedRight && canDoubleJump)
                {
                    YVelocity = 0;
                    pState    = PlayerState.DashingRight;
                }
                else if (kbState.IsKeyDown(Keys.D))
                {
                    xVelocity = Constants.WALK_SPEED;
                }
                if (kbState.IsKeyUp(Keys.W) && yVelocity < 0)
                {
                    yVelocity /= 1.5;
                }
                break;
            }

                #endregion
                #region JumpingLeft
            case PlayerState.JumpingLeft:
            {
                if (YVelocity == 0)
                {
                    pState = PlayerState.IdleLeft;
                }
                else
                {
                    attacheWall = null;
                }
                // The double jump;
                if (gM.SingleKeyPress(Keys.W) && canDoubleJump)
                {
                    _doubleJump.Play(.3f, 0, 0);
                    YVelocity     = Constants.PLAYER_JUMP_SPEED;
                    canDoubleJump = false;
                    dJNow         = true;
                }
                if (kbState.IsKeyDown(Keys.S))
                {
                    pState = PlayerState.CrouchingLeft;
                }
                if (kbState.IsKeyDown(Keys.D))
                {
                    pState    = PlayerState.JumpingRight;
                    xVelocity = Constants.WALK_SPEED;
                }
                if (gM.SingleKeyPress(Keys.A) && dashPrimedLeft && canDoubleJump)
                {
                    YVelocity = 0;
                    pState    = PlayerState.DashingLeft;
                }
                else if (kbState.IsKeyDown(Keys.A) && !collidingLeft)
                {
                    xVelocity = -Constants.WALK_SPEED;
                }
                if (kbState.IsKeyUp(Keys.W) && yVelocity < 0)
                {
                    yVelocity /= 1.5;
                }
                break;
            }

                #endregion
                #region CrouchingRight
            case PlayerState.CrouchingRight:
            {
                if (attacheWall != null)
                {
                    yVelocity = 0;
                }
                if (YVelocity == 0)
                {
                    xVelocity = 0;
                }
                if (kbState.IsKeyUp(Keys.S))
                {
                    if (YVelocity == 0)
                    {
                        pState = PlayerState.IdleRight;
                    }
                    else
                    {
                        pState = PlayerState.JumpingRight;
                    }
                }
                break;
            }

                #endregion
                #region CrouchingLeft
            case PlayerState.CrouchingLeft:
            {
                if (attacheWall != null)
                {
                    yVelocity = 0;
                }
                if (YVelocity == 0)
                {
                    xVelocity = 0;
                }
                if (kbState.IsKeyUp(Keys.S))
                {
                    if (YVelocity == 0)
                    {
                        pState = PlayerState.IdleLeft;
                    }
                    else
                    {
                        pState = PlayerState.JumpingLeft;
                    }
                }
                break;
            }

                #endregion
                #region DashingRight
            case PlayerState.DashingRight:
            {
                dashPrimedRight = false;
                attacheWall     = null;
                XVelocity       = Constants.DASH_SPEED;
                timeDashing    += timeElapsed;
                if (timeDashing >= Constants.DASH_TIME)
                {
                    timeDashing   = 0;
                    XVelocity     = Constants.WALK_SPEED;
                    canDoubleJump = false;
                    dashed        = false;
                    pState        = PlayerState.JumpingRight;
                }
                if (!dashed)
                {
                    _dashSound.Play();
                    dashed = true;
                }
                break;
            }

                #endregion
                #region DashingLeft
            case PlayerState.DashingLeft:
            {
                dashPrimedLeft = false;
                attacheWall    = null;
                XVelocity      = -Constants.DASH_SPEED;
                timeDashing   += timeElapsed;
                if (timeDashing >= Constants.DASH_TIME)
                {
                    timeDashing   = 0;
                    XVelocity     = -Constants.WALK_SPEED;
                    canDoubleJump = false;
                    dashed        = false;
                    pState        = PlayerState.JumpingLeft;
                }
                if (!dashed)
                {
                    _dashSound.Play();
                    dashed = true;
                }
                break;
            }
                #endregion
            }
            #endregion


            foreach (Fireball f in projectiles)
            {
                f.Update();
            }


            rect = new Rectangle((int)loc.X, (int)loc.Y, Constants.PLAYER_WIDTH, Constants.PLAYER_HEIGHT);
            base.Update();
            impact = false;
        }