예제 #1
0
        public override void Update(GameTime gameTime, World world, Player player)
        {
            if (frame_cooldown > 0)
            {
                frame_cooldown -= 1;
            }

            if (player.Hurtbox.Intersects(Hurtbox))
            {
                if (player.Velocity.Y > 1) // player rifght above ruche falling
                {
                    if (frame_cooldown == 0)
                    {
                        frame_cooldown = 10;
                    }
                    if (frame_cooldown > 6)
                    {
                        player.Velocity.Y *= 0.7f;
                    }
                    if (frame_cooldown == 6)
                    {
                        player.Velocity.Y   = -12;
                        player.CurrentState = Player.PlayerState.jump;
                        SoundEffectPlayer.Play(jump);
                    }
                }
            }
            else if (player.Hurtbox.Intersects(Hurtbox))
            {
                player.Bump(this);
            }

            if (frame_cooldown > 6)
            {
                CurrentSprite = ruche2;
            }
            else if (frame_cooldown > 0)
            {
                CurrentSprite = ruche3;
                if (bee_count > 0)
                {
                    bee_count -= 1;
                    Bee b = new Bee(FeetPosition + new Vector2(-direction * 50, 0),
                                    new Vector2(-direction * r.Next(14, 25), r.Next(-2, 3))); // use (r.Next(0,2) * 2 - 1) to random -1 or 1
                    // b.Velocity = new Vector2(-10, 0);
                    world.NewStuff.Add(b);
                }
            }
            else
            {
                CurrentSprite = ruche1;
            }
            base.Update(gameTime, world, player);
        }
예제 #2
0
파일: Player.cs 프로젝트: efflam-s/Bikip
        public override void Update(GameTime gameTime, World world, Player player)
        {
            this.world = world;
            KeyboardState KbState = Keyboard.GetState();

            if (PreviousState == CurrentState)
            {
                state_frames += 1;
            }
            else
            {
                state_frames = 0;
            }
            PreviousState = CurrentState;

            switch (CurrentState)
            {
            case PlayerState.idle:
                /*if (KbState.IsKeyDown(Input.Left) && KbState.IsKeyUp(Input.Right))
                 * {
                 *  if (Velocity.X > -MaxSpeed)
                 *      ApplyForce(new Vector2(-2f, 0));
                 *  CurrentState = PlayerState.walk;
                 * } else if (KbState.IsKeyDown(Input.Right))
                 * {
                 *  if (Velocity.X < MaxSpeed)
                 *      ApplyForce(new Vector2(2f, 0));
                 *  CurrentState = PlayerState.walk;
                 * }*/
                if (!IsOnGround(world))
                {
                    CurrentState = PlayerState.jump;
                }
                else if (Input.direction != 0)
                {
                    CurrentState = PlayerState.walk;
                }
                else if (KbState.IsKeyDown(Input.Jump))
                {
                    ApplyForce(new Vector2(0, -15f));
                    SoundEffectPlayer.Play(jump, 1f, 0f);
                    if (Input.direction != 0)
                    {
                        PlayerDirection = Input.direction;
                    }
                    CurrentState = PlayerState.jump;
                }
                break;

            case PlayerState.walk:
                if (KbState.IsKeyDown(Input.Jump))
                {
                    // Velocity = 0;
                    if (Input.direction != 0)
                    {
                        PlayerDirection = Input.direction;
                    }
                    ApplyForce(new Vector2(0, -15f));
                    SoundEffectPlayer.Play(jump, 1f, 0f);
                    CurrentState = PlayerState.jump;
                }
                else if (!IsOnGround(world))
                {
                    CurrentState = PlayerState.jump;
                }
                else if (Input.direction != 0)     // player is inputing a direction (either left or right)
                {
                    PlayerDirection = Input.direction;
                    if (Math.Sign(Velocity.X) * Math.Sign(Input.direction) >= 0) // if inputed direction is the same as current movement direction
                    {
                        if (Velocity.X * Velocity.X < MaxSpeed * MaxSpeed)       // if norm of velocity below max speed
                        {
                            ApplyForce(new Vector2(Input.direction * 5f, 0));
                        }
                    }
                    else     // if player is inputing the direction against the current movement (brake)
                    {
                        ApplyForce(new Vector2(Input.direction * 5f, 0));
                    }
                }
                else
                {
                    CurrentState = PlayerState.idle;
                }
                break;

            case PlayerState.jump:
                if (Velocity.Y < 0 && Velocity.X != 0)
                {
                    PlayerDirection = Math.Sign(Velocity.X);                                        // raising
                }
                if (KbState.IsKeyDown(Input.Jump) && !prevKbState.IsKeyDown(Input.Jump))
                {
                    Velocity.Y = 0;
                    //if (Input.direction == 0)
                    SoundEffectPlayer.Play(jump, 1f, 0.5f);
                    ApplyForce(new Vector2(0, -15));
                    //else
                    //{
                    //  PlayerDirection = Input.direction;
                    //  ApplyForce(new Vector2(Input.direction * 50, -8));
                    //  }
                    CurrentState = PlayerState.doublejump;
                }
                else if (Input.direction != 0)     // player is inputing a direction (either left or right)
                {
                    if ((Velocity.X + Input.direction) * Math.Sign(Velocity.X) <= MaxSpeed)
                    {
                        ApplyForce(new Vector2(Input.direction * 5f, 0));
                    }
                }
                if (IsOnGround(world))
                {
                    CurrentState = PlayerState.idle;
                }
                break;

            case (PlayerState.doublejump):
            {
                if (Velocity.Y < 0 && Velocity.X != 0)
                {
                    PlayerDirection = Math.Sign(Velocity.X);                                            // raising
                }
                if (IsOnGround(world))
                {
                    CurrentState = PlayerState.idle;
                }
                else if (Input.direction != 0)         // player is inputing a direction (either left or right)
                {
                    if ((Velocity.X + Input.direction) * Math.Sign(Velocity.X) <= MaxSpeed)
                    {
                        ApplyForce(new Vector2(Input.direction * 5f, 0));
                    }
                }
                break;
            }

            case (PlayerState.flip):
            {
                if (CurrentSprite.isOver && state_frames > 200)
                {
                    world.NextLevel(this);        //do something like next level;
                    CurrentState = PlayerState.idle;
                }
                break;
            }
            }
            //
            // SPRITE DETERMINATION
            //
            PreviousSprite = CurrentSprite;
            switch (CurrentState)
            {
            case (PlayerState.idle):
            {
                if (Velocity.X * Velocity.X < 1)
                {
                    CurrentSprite = idle;
                }
                else
                {
                    CurrentSprite = brake;
                }
                break;
            }

            case (PlayerState.walk):
            {
                CurrentSprite = run;
                break;
            }

            case (PlayerState.jump):
            {
                if (Velocity.Y < -1)
                {
                    CurrentSprite = rise;
                }
                else if (Velocity.Y > 1)
                {
                    CurrentSprite = fall;
                }
                else
                {
                    CurrentSprite = top;
                }
                break;
            }

            case (PlayerState.doublejump):
            {
                if (Velocity.Y < 3)
                {
                    CurrentSprite = roll;
                }
                else
                {
                    CurrentSprite = fall;
                }
                break;
            }

            case (PlayerState.flip):
            {
                CurrentSprite = flip;
                break;
            }
            }

            /*if (Input.double_tap_waiting && IsOnGround(world) && CurrentSprite != slug_walk)
             * {
             *  CurrentSprite = slug_charge_attack;
             *  PlayerDirection = Input.direction;
             * }*/

            if (CurrentSprite != PreviousSprite)
            {
                CurrentSprite.ResetAnimation();
                //Console.WriteLine("Switched to sprite " + CurrentSprite.Texture.Name);
            }
            CurrentSprite.direction = PlayerDirection;
            CurrentSprite.UpdateFrame(gameTime);

            foreach (PhysicalObject o in world.Stuff)
            {
                if (o is Bee b)
                {
                    b.AttractFromPlayer(this);
                    if (Hurtbox.Intersects(b.Hurtbox))
                    {
                        //Console.WriteLine("collision between bee and player");
                        world.RemovedStuff.Add(b);
                        SoundEffectPlayer.Play(bee_collected, 0.08f, (float)(r.NextDouble() * 0.2));
                    }
                }
                // do something;
            }

            prevKbState = KbState;
            base.Update(gameTime, world, this);
        }