Beispiel #1
0
    //Exchange the positions of two sweets.
    private void ExchangeSweets(SweetsController sweet1, SweetsController sweet2)
    {
        if (sweet1.Movable() && sweet2.Movable())
        {
            sweets[sweet1.X, sweet1.Y] = sweet2;
            sweets[sweet2.X, sweet2.Y] = sweet1;

            if (MatchSweets(sweet1, sweet2.X, sweet2.Y) != null || MatchSweets(sweet2, sweet1.X, sweet1.Y) != null ||
                sweet1.Type == SweetsType.RAINBOW_CANDY || sweet2.Type == SweetsType.RAINBOW_CANDY)
            {
                int tempX = sweet1.X, tempY = sweet1.Y;
                sweet1.MovedComponent.Move(sweet2.X, sweet2.Y, fillTime);
                sweet2.MovedComponent.Move(tempX, tempY, fillTime);

                if (sweet1.Type == SweetsType.RAINBOW_CANDY && sweet1.ColorAble() && sweet2.ColorAble())
                {
                    EliminateAnySweets clearColor = sweet1.GetComponent <EliminateAnySweets>();

                    if (clearColor != null)
                    {
                        clearColor.ClearColor = sweet2.ColoredComponent.ThisType;
                    }
                    EliminateSweet(sweet1.X, sweet1.Y);
                }

                if (sweet2.Type == SweetsType.RAINBOW_CANDY && sweet1.ColorAble() && sweet2.ColorAble())
                {
                    EliminateAnySweets clearColor = sweet2.GetComponent <EliminateAnySweets>();

                    if (clearColor != null)
                    {
                        clearColor.ClearColor = sweet1.ColoredComponent.ThisType;
                    }
                    EliminateSweet(sweet2.X, sweet2.Y);
                }

                EliminateAllMatchedSweets();
                StartCoroutine(FillAll());
            }
            else
            {
                sweets[sweet1.X, sweet1.Y] = sweet1;
                sweets[sweet2.X, sweet2.Y] = sweet2;
            }
        }
    }
Beispiel #2
0
    //Fill the grids one by one.
    public bool Fill()
    {
        bool filledNotFinished = false;

        //From bottom to top.
        for (int y = rows - 2; y >= 0; y--)
        {
            for (int x = 0; x < columns; x++)
            {
                SweetsController sweet = sweets[x, y];

                if (sweet.Movable())                                //无法移动则无法向下填充(垂直填充).
                {
                    SweetsController sweetBelow = sweets[x, y + 1]; //得到当前位置下方的实体.

                    if (sweetBelow.Type == SweetsType.EMPTY)        //下方当前为空物体,允许垂直填充.
                    {
                        Destroy(sweetBelow.gameObject);
                        sweet.MovedComponent.Move(x, y + 1, fillTime);
                        sweets[x, y + 1] = sweet;
                        CreateNewSweet(x, y, SweetsType.EMPTY);
                        filledNotFinished = true;
                    }
                    else //向左右偏移填充
                    {
                        for (int d = -1; d <= 1; d++)
                        {
                            if (d != 0)  //排除正下方.
                            {
                                int down = x + d;

                                if (down >= 0 && down < columns)
                                {
                                    SweetsController downSweet = sweets[down, y + 1];
                                    if (downSweet.Type == SweetsType.EMPTY)
                                    {
                                        bool vertical_fillable = true;  //用于判断垂直填充是否能满足向下填充.
                                        for (int above = y; above >= 0; above--)
                                        {
                                            SweetsController aboveSweet = sweets[down, above];
                                            if (aboveSweet.Movable())
                                            {
                                                //左下方空白的正上方有可向下填充的物体,则不用向侧下填充.
                                                break;
                                            }
                                            else if (!aboveSweet.Movable() && aboveSweet.Type != SweetsType.EMPTY)
                                            {
                                                vertical_fillable = false;
                                                break;
                                            }
                                        }

                                        if (!vertical_fillable)
                                        {
                                            Destroy(downSweet.gameObject);
                                            sweet.MovedComponent.Move(down, y + 1, fillTime);
                                            sweets[down, y + 1] = sweet;
                                            CreateNewSweet(x, y, SweetsType.EMPTY);
                                            filledNotFinished = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        //Exceptional case: the top row
        for (int x = 0; x < columns; x++)
        {
            SweetsController sweet = sweets[x, 0];
            if (sweet.Type == SweetsType.EMPTY)
            {
                GameObject newSweet =
                    Instantiate(sweetsPrefabDic[SweetsType.NORMAL], CalibratePosition(x, -1), Quaternion.identity);
                newSweet.transform.parent = transform;

                sweets[x, 0] = newSweet.GetComponent <SweetsController>();
                sweets[x, 0].Init(x, -1, this, SweetsType.NORMAL);
                sweets[x, 0].MovedComponent.Move(x, 0, fillTime);
                sweets[x, 0].ColoredComponent.SetThisType(  //随机生成一种类型的甜品 **枚举为特殊的int**
                    (SweetsColorType.ColorType)Random.Range(0, sweets[x, 0].ColoredComponent.MaxColorsNum));
                filledNotFinished = true;
            }
        }
        //Filling finished or not.
        return(filledNotFinished);
    }