public static void HandleCollision(Game1 game, Mario mario, IItem collidingItem, ref Collection <IGameObject> collidingGameObjects)
        {
            if (mario.state.isDead())
            {
                return;
            }
            Rectangle marioRectangle      = mario.GetSizeRectangle();
            Rectangle marioLargeRectangle = mario.GetLargeSizeRectangle();

            if (collidingItem is IItem item)
            {
                Rectangle          itemRectangle           = item.GetSizeRectangle();
                CollisionDirection direction               = CollisionDetection.DetectCollisionDirection(marioRectangle, itemRectangle);
                CollisionDirection directionLargeRectangle = CollisionDetection.DetectCollisionDirection(marioLargeRectangle, itemRectangle);

                if (!(direction is CollisionDirection.NoCollision) || !(directionLargeRectangle is CollisionDirection.NoCollision))
                {
                    if (item is FireFlowerItem)
                    {
                        FireFlowerItem temp = (FireFlowerItem)item;
                        if (temp.triggered)
                        {
                            game.HUD.GetScore(ConstantNumber.SCORE_1000);
                            mario.Evolve();
                            SoundFactory.Instance.playPowerUpSoundEffect();
                            item.Disappear();
                        }
                        else
                        {
                            if (direction is CollisionDirection.Bottom)
                            {
                                item.GetTrigger();
                                SoundFactory.Instance.playVineSoundEffect();
                            }
                        }
                    }
                    if (item is MushroomItem)
                    {
                        MushroomItem temp = (MushroomItem)item;
                        if (temp.triggered)
                        {
                            game.HUD.GetScore(ConstantNumber.SCORE_1000);
                            mario.Evolve();
                            SoundFactory.Instance.playPowerUpSoundEffect();
                            item.Disappear();
                        }
                        else
                        {
                            if (direction is CollisionDirection.Bottom)
                            {
                                item.GetTrigger();
                                SoundFactory.Instance.playVineSoundEffect();
                            }
                        }
                    }
                    if (item is PoisonMushroomItem)
                    {
                        PoisonMushroomItem temp = (PoisonMushroomItem)item;
                        if (temp.triggered)
                        {
                            mario.TakeDamage();
                            SoundFactory.Instance.playTakeDamageSoundEffect();
                            item.Disappear();
                        }
                        else
                        {
                            item.GetTrigger();
                            SoundFactory.Instance.playVineSoundEffect();
                        }
                    }
                    if (item is StarItem)
                    {
                        StarItem temp = (StarItem)item;
                        if (temp.triggered)
                        {
                            game.HUD.GetScore(ConstantNumber.SCORE_1000);
                            mario.ToStar();
                            SoundFactory.Instance.playStarMaioSong();
                            item.Disappear();
                        }
                        else
                        {
                            if (direction is CollisionDirection.Bottom)
                            {
                                item.GetTrigger();
                                SoundFactory.Instance.playVineSoundEffect();
                            }
                        }
                    }

                    if (item is CoinItem)
                    {
                        CoinItem temp = (CoinItem)item;
                        if (!temp.consumed)
                        {
                            if (temp.inBlock)
                            {
                                if (direction is CollisionDirection.Bottom)
                                {
                                    item.GetTrigger();
                                    SoundFactory.Instance.playCoinSoundEffect();
                                    game.HUD.GetCoin();
                                }
                            }
                            else
                            {
                                item.GetTrigger();
                                SoundFactory.Instance.playCoinSoundEffect();
                                game.HUD.GetCoin();
                            }
                        }
                    }

                    if (item is FlagPole flag)
                    {
                        flag.GetTrigger();
                        Game1.Instance.DisableUserControl();
                        Game1.Instance.LoadBasicCommand();
                        mario.Position = new Vector2(flag.GetSizeRectangle().X - mario.GetSizeRectangle().Width, mario.Position.Y);
                        mario.ToCreepDown();
                        SoundFactory.Instance.playFlagpoleSoundEffect();
                        if (flag.flagFallTime < 0 || collidingGameObjects.Count > 0)
                        {
                            mario.Position = new Vector2(flag.GetSizeRectangle().X - ConstantNumber.FlAG_POSITION_X_ADJUST + mario.GetSizeRectangle().Width, mario.Position.Y);
                            mario.ToLeft();
                            if (mario.standingTime < 0)
                            {
                                mario.ToRight();

                                mario.ReachDestination = true;
                            }
                            mario.standingTime--;
                        }
                    }
                }
            }
        }
Пример #2
0
        public static void ChangeLevel(Collection <IGameObject> gameObjects, string level)
        {
            using (TextFieldParser parser = new TextFieldParser(level))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                int      cellSize   = ConstantNumber.CELL_SIZE;
                int      csvY       = ConstantNumber.CELL_STARTING_ROW;
                Velocity enemySpeed = new Velocity(new Vector2(ConstantNumber.ENEMY_ITEM_SPEED_X, ConstantNumber.ENEMY_ITEM_SPEED_Y));
                Velocity itemSpeed  = new Velocity(new Vector2(ConstantNumber.ENEMY_ITEM_SPEED_X, ConstantNumber.ENEMY_ITEM_SPEED_Y));

                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();
                    for (int x = 0; x < fields.Length; x++)
                    {
                        switch (fields[x])
                        {
                        case "ff":    //FireFlower item
                            gameObjects.Add(new FireFlowerItem(new Vector2(cellSize * x, cellSize * csvY)));
                            break;

                        case "c":    //Coin item
                            gameObjects.Add(new CoinItem(new Vector2(cellSize * x, cellSize * csvY)));
                            break;

                        case "sm":    //Super Mushroom item
                            Game1.Instance.mushroom = new MushroomItem(new Vector2(cellSize * x, cellSize * csvY), itemSpeed);
                            gameObjects.Add(Game1.Instance.mushroom);
                            break;

                        case "pm":    //Poison Mushroom item
                            Game1.Instance.poisonMushroom = new PoisonMushroomItem(new Vector2(cellSize * x, cellSize * csvY), itemSpeed);
                            gameObjects.Add(Game1.Instance.poisonMushroom);
                            break;

                        case "s":    //Star item
                            Game1.Instance.star = new StarItem(new Vector2(cellSize * x, cellSize * csvY), itemSpeed);
                            gameObjects.Add(Game1.Instance.star);
                            break;

                        case "g":    //Goomba enemy
                            Game1.Instance.goomba = new Goomba(new Vector2(cellSize * x, cellSize * csvY), enemySpeed);
                            gameObjects.Add(Game1.Instance.goomba);
                            break;

                        case "k":    //Koopa enemy
                            Game1.Instance.koopa = new Koopa(new Vector2(cellSize * x, cellSize * csvY), enemySpeed);
                            gameObjects.Add(Game1.Instance.koopa);
                            break;

                        case "hb":    //Hidden Block
                            IBlock hiddenBlock = new Block(new Vector2(cellSize * x, cellSize * csvY));
                            hiddenBlock.ToHiddenBlock();
                            gameObjects.Add(hiddenBlock);
                            break;

                        case "ub":    //Used Block
                            IBlock usedBlock = new Block(new Vector2(cellSize * x, cellSize * csvY));
                            usedBlock.ToUsedBlock();
                            gameObjects.Add(usedBlock);
                            break;

                        case "bb":    //Brick Block
                            IBlock brickBlock = new Block(new Vector2(cellSize * x, cellSize * csvY));
                            gameObjects.Add(brickBlock);
                            break;

                        case "qb":    //Empty Question Block
                            IBlock questionBlock = new Block(new Vector2(cellSize * x, cellSize * csvY));
                            questionBlock.ToQuestionBlock();
                            gameObjects.Add(questionBlock);
                            break;

                        case "qbm":    //Question Block with Mushroom
                            IBlock questionBlockM = new Block(new Vector2(cellSize * x, cellSize * csvY));
                            questionBlockM.ToQuestionBlock();
                            gameObjects.Add(questionBlockM);
                            Game1.Instance.mushroom = new MushroomItem(new Vector2(cellSize * x, cellSize * csvY), itemSpeed);
                            gameObjects.Add(Game1.Instance.mushroom);
                            break;

                        case "qbf":    //Question Block with Fire Flower
                            IBlock questionBlockF = new Block(new Vector2(cellSize * x, cellSize * csvY));
                            questionBlockF.ToQuestionBlock();
                            gameObjects.Add(questionBlockF);
                            gameObjects.Add(new FireFlowerItem(new Vector2(cellSize * x, cellSize * csvY)));
                            break;

                        case "qbs":    //Question Block with Star
                            IBlock questionBlockS = new Block(new Vector2(cellSize * x, cellSize * csvY));
                            questionBlockS.ToQuestionBlock();
                            gameObjects.Add(questionBlockS);
                            Game1.Instance.star = new StarItem(new Vector2(cellSize * x, cellSize * csvY), itemSpeed);
                            gameObjects.Add(Game1.Instance.star);
                            break;

                        case "qbc":    //Question Block with Coin
                            IBlock questionBlockC = new Block(new Vector2(cellSize * x, cellSize * csvY));
                            questionBlockC.ToQuestionBlock();
                            gameObjects.Add(questionBlockC);
                            CoinItem coinItem = new CoinItem(new Vector2(cellSize * x, cellSize * csvY));
                            coinItem.inBlock = true;
                            gameObjects.Add(coinItem);
                            break;

                        case "spb":     //Smooth platform block
                            IBlock smoothPlatformBlock = new Block(new Vector2(cellSize * x, cellSize * csvY));
                            smoothPlatformBlock.ToSmoothPlatformBlock();
                            gameObjects.Add(smoothPlatformBlock);
                            break;

                        case "spbf":     //Smooth platform block with flag
                            IBlock smoothPlatformBlockFlag = new Block(new Vector2(cellSize * x + 8, cellSize * csvY));
                            smoothPlatformBlockFlag.ToSmoothPlatformBlock();
                            gameObjects.Add(smoothPlatformBlockFlag);
                            break;

                        case "rpb":    //Rough platform block
                            IBlock roughPlatformBlock = new Block(new Vector2(cellSize * x, cellSize * csvY));
                            roughPlatformBlock.ToRoughPlatformBlock();
                            gameObjects.Add(roughPlatformBlock);
                            break;

                        case "m":    //Mario
                            Game1.Instance.Mario.Position = new Vector2(cellSize * x, cellSize * csvY);
                            gameObjects.Add(Game1.Instance.Mario);
                            break;

                        case "p":    //Pipe
                            gameObjects.Add(new Pipe(new Vector2(cellSize * x, cellSize * csvY)));
                            break;

                        case "wp":    //Warp Pipe
                            var pipe = new Pipe(new Vector2(cellSize * x, cellSize * csvY));
                            gameObjects.Add(pipe);
                            Game1.Instance.warpPipe = pipe;
                            break;

                        case "fp":    //FlagPole
                            gameObjects.Add(new FlagPole(new Vector2(cellSize * x, cellSize * csvY)));
                            break;

                        default:
                            break;
                        }
                    }
                    csvY++;
                }
            }
        }