public void Handle(IEntity entity, IEntity target, Side side)
        {
            IPlayer player = (IPlayer)entity;

            // Player is colliding into a block
            if (target is IBlock)
            {
                IBlock block = (IBlock)target;
                // Blocks can only be hit from the bottom
                if (side == Side.BOTTOM)
                {
                    // Player hits the block
                    block.Hit(player);
                }
                else if (target is Pipe && side == ((Pipe)target).Side)
                {
                    target.Collision.Handle(player, target, side);
                    block.Hit(player);
                }
            }
            // Player is colliding into an item
            else if (target is IItem)
            {
                // Player consumes the item
                ((IItem)target).Consume(player);
            }
            // Player is colliding into an enemy
            else if (target is IEnemy)
            {
                IEnemy enemy = (IEnemy)target;
                if (enemy.IsAlive())
                {
                    if (side == Side.TOP)
                    {
                        if (player.State.Name != "Dead")
                        {
                            // Player stomps the enemy from the top
                            SoundEffects.Instance.PlayStompSound();
                            enemy.BeStomped();

                            player.ApplyForce(stomp);
                        }
                    }
                    else
                    {
                        // Player got damaged progressively
                        player.CauseDamage();
                    }
                }
                else if (enemy is Koopa && ((Koopa)enemy).State is KoopaStompedState)
                {
                    SoundEffects.Instance.PlayStompSound();
                    enemy.BeStomped();
                    if (side == Side.TOP)
                    {
                        player.ApplyForce(stomp);
                    }
                }
            }
        }
Exemple #2
0
        public void HandleCollision(IEnemy enemy)
        {
            if (mario.IsStarMario() && result != Direction.None)
            {
                enemy.Beflipped();
                SoundManager.Instance.PlaySoundEffect(SoundString.flip);
                ScoringSystem.Instance.AddPointsForStompingEnemy(enemy);
            }


            if (result == Direction.Up)
            {
                enemy.BeStomped();
                SoundManager.Instance.PlaySoundEffect(SoundString.stomp);
            }

            if (enemy.EnemyState is StompedKoopaState && result == Direction.Right)
            {
                enemy.TurnLeft();
                SoundManager.Instance.PlaySoundEffect(SoundString.flip);
            }
            else if (enemy.EnemyState is StompedKoopaState && result == Direction.Left)
            {
                enemy.TurnRight();
                SoundManager.Instance.PlaySoundEffect(SoundString.flip);
            }
        }
        public static void MarioCollideWithEnemy(IMario mario, IGameObject enemy)
        {
            Rectangle  marioRectangle = mario.RetrieveMarioRectangle();
            Rectangle  enemyRectangle = enemy.GetHitBox();
            ICollision collisionSide  = CollisionSideDetector.DetectCollisionSide(marioRectangle, enemyRectangle);
            IEnemy     collidedEnemy  = (IEnemy)enemy;

            if (collisionSide is RightCollision)
            {
                mario.Position = new Vector2(mario.Position.X - collisionSide.CollisionIntersection.Width, mario.Position.Y);
                if (!(mario is StarMario) && !(collidedEnemy.State is StompedKoopaState))
                {
                    new ShrunkenMario(mario, Game1.Instance);
                }
                if (!(collidedEnemy.State is StompedKoopaState))
                {
                    mario.GetHit();
                }
                collidedEnemy.HitFromLeft((IGameObject)mario);
            }
            else if (collisionSide is LeftCollision)
            {
                mario.Position = new Vector2(mario.Position.X + collisionSide.CollisionIntersection.Width, mario.Position.Y);
                if (!(mario is StarMario) && !(collidedEnemy.State is StompedKoopaState))
                {
                    new ShrunkenMario(mario, Game1.Instance);
                }
                if (!(collidedEnemy.State is StompedKoopaState))
                {
                    mario.GetHit();
                }
                collidedEnemy.HitFromRight((IGameObject)mario);
            }
            else if (collisionSide is TopCollision)
            {
                mario.Position = new Vector2(mario.Position.X, mario.Position.Y - collisionSide.CollisionIntersection.Height - 5);
                mario.physicsObject.aerialSpeed = 10;
                mario.physicsObject.dy          = 0;
                mario.physicsObject.Jump();
                IEnemy hitEnemy = (IEnemy)enemy;
                hitEnemy.BeStomped();
            }
            else if (collisionSide is BottomCollision)
            {
                mario.Position = new Vector2(mario.Position.X, mario.Position.Y + collisionSide.CollisionIntersection.Height);
                if (!(collidedEnemy.State is StompedKoopaState))
                {
                    mario.GetHit();
                }
            }
        }
Exemple #4
0
 public static void HandleFireballEnemyCollision(Fireball fireball, IEnemy enemy)
 {
     if (enemy.Collidable)
     {
         if (enemy is Jellyfish || enemy is Fish)
         {
             fireball.Dead = true;
             enemy.BeFlipped();
             TreeNewBee.SuperMarioBros.Instance.ScManager.Score += Constant.Constant.Instance.FireballKillEnemyReward;
         }
         else
         {
             fireball.Dead = true;
             enemy.BeStomped();
             TreeNewBee.SuperMarioBros.Instance.ScManager.Score += Constant.Constant.Instance.FireballKillEnemyReward;
         }
     }
 }
        public void Update(GameTime gameTime)
        {
            // Check if entity is under map kill threshold
            if (Position.Y >= 12.5)
            {
                if (this is IPlayer)
                {
                    IPlayer player = (IPlayer)this;
                    player.CauseDamage();
                }
                else if (this is IEnemy)
                {
                    IEnemy enemy = (IEnemy)this;
                    if (enemy.IsAlive())
                    {
                        enemy.BeStomped();
                    }
                }
            }

            // Calculate the delta time
            float delta = gameTime.ElapsedGameTime.Milliseconds / 1000f;

            // Apply pending forces to acceleration
            foreach (Force force in forces)
            {
                Acceleration.Add(force.Calculate(this, delta));
            }
            forces.Clear();

            // Limit acceleration
            Acceleration.Limit();

            // Increase velocity by acceleration and set to 0
            Velocity.Add(Acceleration.Vector);
            Acceleration.Set(Vector2.Zero);

            // Limit new velocity
            Velocity.Limit();

            // Handle moving entity's position based on velocity
            Movement.MoveEntity(this, Velocity.X * delta, -Velocity.Y * delta);
        }
 private static void MarioGoombaCollision(IMario mario, IEnemy enemy, CollisionSide side)
 {
     if (mario.MarioPowerUpState is MarioSmallInvincibleState || mario.MarioPowerUpState is MarioSuperInvincibleState)
     {
         enemy.BeFlipped();
     }
     else
     {
         if (side == CollisionSide.Top && enemy.Collidable)
         {
             enemy.BeStomped();
             SoundFactory.Instance.CreateStompSound();
             TreeNewBee.SuperMarioBros.Instance.ScManager.Score += Constant.Instance.HitGoombaReward;
         }
         else
         {
             if (!(enemy.State is GoombaFlippedState || enemy.State is GoombaStompedState))
             {
                 mario.TakeDamage();
             }
         }
     }
 }
        public static void EnemyHandler(Mario mario, IEnemy enemy, int CollisionSide)
        {
            int     THREE = 3;
            int     SIX   = 6;
            Vector2 newPosition;

            if (!(enemy.enemySprite is GoombaFlippedSprite) && !(enemy.enemySprite is GoombaStompedSprite) && !(enemy.enemySprite is KoopaFlippedSprite))
            {
                switch (CollisionSide)
                {
                case 1:
                    if (mario.HasStarPower)
                    {
                        enemy.BeFlipped();
                        //score
                        mario.isScored    = true;
                        mario.score       = GameConstants.Score1;
                        mario.totalScore += mario.score;
                        Vector2 newP;
                        newP.X             = mario.position.X;
                        newP.Y             = mario.position.Y - THREE;
                        mario.textPosition = newP;
                    }
                    else
                    {
                        if (mario.state.marioSprite.desRectangle.Bottom > enemy.enemySprite.desRectangle.Top)
                        {
                            mario.bounce = true;
                            MarioSoundManager.instance.playSound(MarioSoundManager.STOMP);
                            enemy.BeStomped();
                            if (!(enemy.shellDirection))
                            {
                                mario.isScored    = true;
                                mario.score       = GameConstants.Score1;
                                mario.totalScore += mario.score;
                                Vector2 newP;
                                newP.X             = mario.position.X;
                                newP.Y             = mario.position.Y - THREE;
                                mario.textPosition = newP;
                            }
                        }
                        else if (enemy.enemySprite is KoopaStompedSprite)
                        {
                            if (enemy.shellDirection)
                            {
                                mario.MarioGetHit();
                            }
                            else
                            {
                                if (mario.marioDirection)
                                {
                                    enemy.shellDirection = true;
                                    enemy.shellMoving    = GameConstants.Left;
                                }
                                else
                                {
                                    enemy.shellDirection = true;
                                    enemy.shellMoving    = GameConstants.Right;
                                }
                            }
                        }
                    }
                    break;

                case 2:
                    if (mario.HasStarPower)
                    {
                        enemy.BeFlipped();
                        //score
                        mario.isScored = true;
                        mario.score    = GameConstants.Score1;
                        Vector2 newP;
                        newP.X             = mario.position.X;
                        newP.Y             = mario.position.Y - THREE;
                        mario.textPosition = newP;
                    }
                    else
                    {
                        if (enemy.enemySprite is KoopaStompedSprite)
                        {
                            if (enemy.shellDirection)
                            {
                                mario.MarioGetHit();
                            }
                            else
                            {
                                if (mario.position.X < enemy.position.X)
                                {
                                    newPosition.X  = mario.position.X - SIX;
                                    newPosition.Y  = mario.position.Y;
                                    mario.position = newPosition;
                                }
                                else
                                {
                                    newPosition.X  = mario.position.X + SIX;
                                    newPosition.Y  = mario.position.Y;
                                    mario.position = newPosition;
                                }

                                enemy.shellDirection = true;
                                enemy.shellMoving    = GameConstants.Right;
                            }
                        }
                        else
                        {
                            mario.MarioGetHit();
                        }
                    }
                    break;

                case 3:
                    if (mario.HasStarPower)
                    {
                        enemy.BeFlipped();
                        //score
                        mario.isScored = true;
                        mario.score    = GameConstants.Score1;
                        Vector2 newP;
                        newP.X             = mario.position.X;
                        newP.Y             = mario.position.Y - THREE;
                        mario.textPosition = newP;
                    }
                    else
                    {
                        mario.MarioGetHit();
                    }

                    break;

                case 4:
                    if (mario.HasStarPower)
                    {
                        enemy.BeFlipped();
                        //score
                        mario.isScored = true;
                        mario.score    = GameConstants.Score1;
                        Vector2 newP;
                        newP.X             = mario.position.X;
                        newP.Y             = mario.position.Y - THREE;
                        mario.textPosition = newP;
                    }
                    else
                    {
                        if (enemy.enemySprite is KoopaStompedSprite)
                        {
                            if (enemy.shellDirection)
                            {
                                mario.MarioGetHit();
                            }
                            else
                            {
                                MarioSoundManager.instance.playSound(MarioSoundManager.KICK);
                                if (mario.position.X < enemy.position.X)
                                {
                                    newPosition.X  = mario.position.X - SIX;
                                    newPosition.Y  = mario.position.Y;
                                    mario.position = newPosition;
                                }
                                else
                                {
                                    newPosition.X  = mario.position.X + SIX;
                                    newPosition.Y  = mario.position.Y;
                                    mario.position = newPosition;
                                }
                                enemy.shellDirection = true;
                                enemy.shellMoving    = GameConstants.Left;
                            }
                        }
                        else
                        {
                            mario.MarioGetHit();
                        }
                    }
                    break;
                }
            }
        }