Ejemplo n.º 1
0
        public static void HandleCollision(Game1 game, Mario mario, IEnemy collidingEnemy)
        {
            if (mario.state.isDead())
            {
                return;
            }
            Rectangle marioRectangle = mario.GetSizeRectangle();

            if (collidingEnemy is IEnemy enemy)
            {
                if (enemy is Koopa koopa && koopa.state is FlipedKoopaState)
                {
                    return;
                }
                if (enemy is Goomba goomba && goomba.state is FlipedGoombaState)
                {
                    return;
                }
                if (mario.state.isStar() && !mario.isStarDoingDamage)
                {
                    return;
                }
                Rectangle          enemyRectangle = enemy.GetSizeRectangle();
                CollisionDirection direction      = CollisionDetection.DetectCollisionDirection(marioRectangle, enemyRectangle);
                if (!(direction is CollisionDirection.NoCollision))
                {
                    if (mario.state.isStar() && mario.isStarDoingDamage)
                    {
                        game.HUD.GetScore(ConstantNumber.SCORE_100);
                        enemy.Flip();
                        SoundFactory.Instance.playStompEnemySoundEffect();
                    }
                    else if (direction is CollisionDirection.Top)
                    {
                        game.HUD.GetScore(ConstantNumber.SCORE_100);
                        enemy.BeStomped();
                        SoundFactory.Instance.playStompEnemySoundEffect();
                        mario.Bounce();
                    }
                    else if (enemy is Koopa stompedKoopa && stompedKoopa.isStompedIdle)
                    {
                        if (direction is CollisionDirection.Left)
                        {
                            stompedKoopa.TurnRight();
                        }
                        else if (direction is CollisionDirection.Right)
                        {
                            stompedKoopa.TurnLeft();
                        }
                    }
                    else
                    {
                        mario.TakeDamage();
                        SoundFactory.Instance.playTakeDamageSoundEffect();
                    }
                }
            }
        public static void HandleCollision(Mario player1, Mario player2)
        {
            Rectangle          player1Rectangle = player1.GetSizeRectangle();
            Rectangle          player2Rectangle = player2.GetSizeRectangle();
            CollisionDirection direction        = DetectCollisionDirection(player1Rectangle, player2Rectangle);

            if (!(direction is CollisionDirection.NoCollision))
            {
                if (direction is CollisionDirection.Top)
                {
                    player2.GetInjured();
                    player1.Bounce();
                }
                else if (direction is CollisionDirection.Left)
                {
                    player1.Position = new Vector2(player2.GetSizeRectangle().X - player1.GetSizeRectangle().Width, player1.Position.Y);
                }
                else if (direction is CollisionDirection.Right)
                {
                    player1.Position = new Vector2(player2.GetSizeRectangle().X + player1.GetSizeRectangle().Width, player1.Position.Y);
                }
            }
        }