Пример #1
0
 public void Update(Player player)
 {
     if (!collected)
     {
         // see if the player collected the item
         HitBox overlap = this.HitBox.CheckCollision(player.HitBox);
         if (overlap.IsNotNull())
         {
             player.Collect(this);
             collected = true;
         }
     }
 }
Пример #2
0
        public void MoveHorzNew(Controls controls, int lean)
        {
            ///////////////////////////////////////////////////////////////////////////////////////////
            // this is the new method for moving the player horizontally
            // it features a very quick acceleration to normal running speed,
            // with a timer that immediately activates Sprinting speed after a few seconds
            ///////////////////////////////////////////////////////////////////////////////////////////
            UpdateSprintTimer(controls, lean);

            float maxSpeed = 0;
            if (lean != 0)
            {
                maxSpeed = MaxJogSpeed;
                if (controls.Run)
                {
                    maxSpeed = MaxRunSpeed;
                    if (SprintModeOn)
                        maxSpeed = MaxSprintSpeed;
                }
            }

            // is the player speeding up?
            if (lean != 0 && mov.X * lean < maxSpeed)
            {
                // speed the player up
                mov.X += lean * Accel;
                if (mov.X > maxSpeed * lean)
                    mov.X = maxSpeed * lean;
            }
            // is the player slowing down?
            else
            {
                // slow the player down
                int curDir = mov.X < 0 ? -1 : 1;
                mov.X -= Decel * curDir;
                if (mov.X < maxSpeed * curDir)
                    mov.X = maxSpeed * curDir;
            }
            pos.X += mov.X;
            HitBox overlap = level.CheckCollisionSolid(this.HitBox);
            if (mov.X < 0 && overlap.Left != 0)
            {
                pos.X -= overlap.Left;
                mov.X = 0;
            }
            else if (mov.X > 0 && overlap.Right != 0)
            {
                pos.X -= overlap.Right;
                mov.X = 0;
            }
        }
Пример #3
0
 public HitBox CheckCollision(HitBox hitbox2)
 {
     if (Left < hitbox2.Right && Right > hitbox2.Left && Top < hitbox2.Bottom && Bottom > hitbox2.Top)
     {
         HitBox overlap = new HitBox();
         overlap.Left   = Left - hitbox2.Right;
         overlap.Right  = Right - hitbox2.Left;
         overlap.Top    = Top - hitbox2.Bottom;
         overlap.Bottom = Bottom - hitbox2.Top;
         return(overlap);
     }
     else
     {
         return(Zero);
     }
 }
Пример #4
0
        public virtual void Update()
        {
            if (HitBox.Left > level.Bounds.Right || HitBox.Top > level.Bounds.Bottom)
            {
                finished = true;
            }

            pos.X += mov.X;
            HitBox overlap = level.CheckCollisionSolid(this.HitBox);

            if (mov.X < 0 && overlap.Left != 0)
            {
                frame  = 0;
                pos.X -= overlap.Left + Width / 4;
                if (overlap.Bottom < Height - HitBorder)
                {
                    pos.Y += Height - HitBorder - overlap.Bottom;
                }
                burstFrame = 0;
                splatTime  = MaxSplatTime;
                mov        = Vector2.Zero;
            }
            else if (mov.X > 0 && overlap.Right != 0)
            {
                frame  = 3;
                pos.X -= overlap.Right - Width / 4;
                if (overlap.Bottom < Height - HitBorder)
                {
                    pos.Y += Height - HitBorder - overlap.Bottom;
                }
                burstFrame = 0;
                splatTime  = MaxSplatTime;
                mov        = Vector2.Zero;
            }
            mov.Y  += Gravity;
            pos.Y  += mov.Y;
            overlap = level.CheckCollisionSolid(this.HitBox);
            if (overlap.Bottom != 0 && splatTime != MaxSplatTime)
            {
                if (mov.X < 0)
                {
                    frame = 1;
                }
                else
                {
                    frame = 2;
                }
                pos.Y -= overlap.Bottom - Height / 4;
                if (-overlap.Left < Width - HitBorder)
                {
                    pos.X -= Width - HitBorder + overlap.Left;
                }
                if (overlap.Right < Width - HitBorder)
                {
                    pos.X += Width - HitBorder - overlap.Right;
                }
                burstFrame = 0;
                splatTime  = MaxSplatTime;
                mov        = Vector2.Zero;
            }

            if (mov.X < 0)
            {
                --frame;
                if (frame < 0)
                {
                    frame = FinalFrame;
                }
            }
            else
            {
                ++frame;
                if (frame > FinalFrame)
                {
                    frame = 0;
                }
            }
        }
Пример #5
0
        /*
        public void MoveHorzOld(Controls controls, int lean)
        {
            ///////////////////////////////////////////////////////////////////////////////////////////
            // this is the old method for moving the player horizontally
            // it features a very gradual acceleration method that provides much more limited control
            ///////////////////////////////////////////////////////////////////////////////////////////
            if (lean == 0 || (!controls.Run && ((lean < 0 && mov.X < -MaxRunSpeed) || (lean > 0 && mov.X > MaxRunSpeed))))
            {
                // slow down
                if (mov.X < 0)
                {
                    mov.X += Decel;
                    if (mov.X > 0)
                        mov.X = 0;
                }
                else if (mov.X > 0)
                {
                    mov.X -= Decel;
                    if (mov.X < 0)
                        mov.X = 0;
                }
            }
            else
            {
                // speed up
                float maxSpeed = MaxWalkSpeed;
                float accel = WalkAccel;
                if (controls.Run && ((lean < 0 && mov.X < 0) || (lean > 0 && mov.X > 0)))
                {
                    if (mov.X <= -MaxRunSpeed || mov.X >= MaxRunSpeed)
                    {
                        maxSpeed = MaxSprintSpeed;
                        accel = SprintAccel;
                    }
                    else if (mov.X <= -MaxWalkSpeed || mov.X >= MaxWalkSpeed)
                    {
                        maxSpeed = MaxRunSpeed;
                        accel = RunAccel;
                    }
                }

                mov.X += lean * accel;
                if (mov.X < -maxSpeed && lean < 0)
                    mov.X = -maxSpeed;
                if (mov.X > maxSpeed && lean > 0)
                    mov.X = maxSpeed;
            }
            pos.X += mov.X;
            HitBox overlap = level.CheckCollisionSolid(this.HitBox);
            if (mov.X < 0 && overlap.Left != 0)
            {
                pos.X -= overlap.Left;
                mov.X = 0;
            }
            else if (mov.X > 0 && overlap.Right != 0)
            {
                pos.X -= overlap.Right;
                mov.X = 0;
            }
        }
        */
        public void Update(Controls controls)
        {
            // track the state of the previous frame
            State prevState = state;

            // update tomatoes already on screen
            numTomatoes = 0;
            List<Tomato> completed = new List<Tomato>();
            foreach (Tomato tomato in tomatoes)
            {
                tomato.Update();
                if (tomato.Active)
                    ++numTomatoes;
                if (tomato.Finished)
                    completed.Add(tomato);
            }
            foreach (Tomato tomato in completed)
                tomatoes.Remove(tomato);

            if (state == State.Dead)
            {
                ++timeInState;
                if (timeInState >= DeathTime)
                    level.CompletionCode = Level.CompleteCode.PlayerDied;
                return;
            }
            if (state == State.Dying)
            {
                ++timeInState;
                if (timeInState < 2)
                {
                    if (mov.Y > 0)
                        mov.Y = -mov.Y;
                }
                mov.Y += Gravity / 2;
                if (mov.Y > FallSpeed)
                    mov.Y = FallSpeed;
                pos += mov;
                if (pos.Y > level.Bounds.Bottom)
                {
                    state = State.Dead;
                    timeInState = 0;
                }

                // if the player is dead, don't do any other updates
                return;
            }

            // move the player horizontally
            int lean = 0;
            if (controls.Left)
                lean -= 1;
            if (controls.Right)
                lean += 1;
            MoveHorz(controls, lean);

            // move the player vertically
            if (state == State.Standing || state == State.Walking || state == State.Running || state == State.Sprinting)
            {
                if (!jumpPressedLastFrame && controls.Jump)
                    state = State.Jumping;
                else
                {
                    // simulate the player falling
                    pos.Y += FallSpeed;
                    HitBox overlap = level.CheckCollisionSolid(this.HitBox);
                    if (overlap.Bottom != 0)
                        // correct based on the overlap
                        // this will correct for slopes and moving platforms too
                        pos.Y -= overlap.Bottom;
                    else
                    {
                        pos.Y -= FallSpeed;
                        // if the falling simulation did not hit a solid object,
                        // then the player should fall for real
                        state = State.Falling;
                        lateJumpTimer = LateJumpFrames;
                    }
                }
            }
            else if (state == State.Jumping)
            {
                mov.Y = -JumpSpeed;
                pos.Y += mov.Y;
                HitBox overlap = level.CheckCollisionSolid(this.HitBox);
                if (overlap.Top != 0)
                {
                    pos.Y -= overlap.Top;
                    mov.Y = 0;
                    state = State.Falling;
                }
            }
            else if (state == State.Falling)
            {
                if (!jumpPressedLastFrame && controls.Jump && lateJumpTimer > 0)
                {
                    mov.Y = 0;
                    state = State.Jumping;
                    lateJumpTimer = 0;
                }
                else
                {
                    if (!jumpPressedLastFrame && controls.Jump)
                        earlyJumpTimer = EarlyJumpFrames;

                    mov.Y += Gravity;
                    if (mov.Y > FallSpeed)
                        mov.Y = FallSpeed;
                    pos.Y += mov.Y;
                    HitBox overlap = level.CheckCollisionSolid(this.HitBox);
                    if (overlap.Bottom != 0)
                    {
                        pos.Y -= overlap.Bottom;
                        mov.Y = 0;
                        if (earlyJumpTimer > 0)
                        {
                            state = State.Jumping;
                            earlyJumpTimer = 0;
                        }
                        else
                            state = State.Standing;
                    }
                }
            }
            if (earlyJumpTimer > 0)
                --earlyJumpTimer;
            if (lateJumpTimer > 0)
                --lateJumpTimer;

            // see if the player completed the level
            if (HitBox.Left >= level.Bounds.Right)
            {
                level.CompletionCode = Level.CompleteCode.Victory;
                return;
            }
            // see if the player fell in the hole
            if (pos.Y > level.Bounds.Bottom)
            {
                KillQuickly();
                return;
            }
            // see if the player died of hunger
            --health;
            if (health < 0)
            {
                KillSlowly();
                return;
            }

            // make the player attack if needed
            if (controls.Attack && !attackPressedLastFrame && CanAttack)
                attacking = true;

            // update the Y (attacking) frame
            if (attacking)
            {
                if (frameY < AttackFrameFinal)
                    ++frameY;
                else
                {
                    attacking = false;
                    frameY = 0;
                }
            }

            // throw a tomato if needed
            if (attacking && frameY == AttackFrameToStartThrow && numTomatoes < MaxTomatoes)
            {
                float startPosX = pos.X + Width / 2 - 8;
                float startPosY = pos.Y + Height / 2 - 8 - TomatoOffsetY;
                if (TomatoSpeed < 0)
                    startPosX -= TomatoOffsetX;
                else
                    startPosX += TomatoOffsetX;

                Tomato tomato = new Tomato(level, tomatoTexture, startPosX, startPosY, TomatoSpeed, 0);
                tomatoes.Add(tomato);
            }

            // update the state
            if (state == State.Standing || state == State.Walking || state == State.Running || state == State.Sprinting)
            {
                if (mov.X == 0)
                    state = State.Standing;
                else if (mov.X <= MaxWalkSpeed && mov.X >= -MaxWalkSpeed)
                    state = State.Walking;
                else if (mov.X <= MaxRunSpeed && mov.X >= -MaxRunSpeed)
                    state = State.Running;
                else
                    state = State.Sprinting;
            }
            if (state == prevState)
                ++timeInState;
            else
                timeInState = 0;
            if (state == State.Jumping && (!controls.Jump || timeInState > JumpHeight))
                state = State.Falling;

            // update the X frame
            if (lean < 0)
                frameReversed = true;
            if (lean > 0)
                frameReversed = false;
            switch (state)
            {
                case State.Standing:
                    frameX = 0;
                    break;
                case State.Walking:
                    frameX = WalkingFrameOrder[(timeInState / WalkingFrameNum) % WalkingFrameOrder.Length];
                    break;
                case State.Running:
                    frameX = RunningFrameOrder[(timeInState / RunningFrameNum) % RunningFrameOrder.Length];
                    break;
                case State.Sprinting:
                    frameX = SprintingFrameOrder[(timeInState / SprintingFrameNum) % SprintingFrameOrder.Length];
                    break;
                case State.Jumping:
                    if (timeInState > 1)
                        frameX = JumpingFrame;
                    else if (timeInState == 1)
                        frameX = JumpingFrame - 1;
                    else if (timeInState == 0)
                        frameX = JumpingFrame - 2;
                    break;
                case State.Falling:
                    frameX = FallingFrame;
                    break;
            }

            // update button flags
            jumpPressedLastFrame = controls.Jump;
            attackPressedLastFrame = controls.Attack;
        }