Exemplo n.º 1
0
    //Eliminate sweets in all range.
    private bool EliminateAllMatchedSweets()
    {
        bool needFill = false;

        for (int y = 0; y < rows; y++)
        {
            for (int x = 0; x < columns; x++)
            {
                if (sweets[x, y].Eliminable())
                {
                    List <SweetsController> matchList = MatchSweets(sweets[x, y], x, y);

                    if (matchList != null)
                    {
                        SweetsType       superSweets    = SweetsType.COUNT;
                        SweetsController superSweetsPos = matchList[Random.Range(0, matchList.Count)];

                        if (matchList.Count == 4)
                        {
                            superSweets = (SweetsType)Random.Range(3, 5);
                        }

                        //matchList.Count >= 5 ... Rainbow Sweets
                        if (matchList.Count >= 5)
                        {
                            superSweets = SweetsType.RAINBOW_CANDY;
                        }

                        for (int i = 0; i < matchList.Count; i++)
                        {
                            if (EliminateSweet(matchList[i].X, matchList[i].Y))
                            {
                                needFill = true;
                            }
                        }

                        //Generate super sweets.
                        if (superSweets != SweetsType.COUNT)
                        {
                            Destroy(sweets[superSweetsPos.X, superSweetsPos.Y]);
                            SweetsController newSweets = CreateNewSweet(superSweetsPos.X, superSweetsPos.Y, superSweets);

                            if (matchList.Count == 4 && newSweets.ColorAble() && matchList[0].ColorAble())
                            {
                                newSweets.ColoredComponent.SetThisType(matchList[0].ColoredComponent.ThisType);
                            }
                            else if (superSweets == SweetsType.RAINBOW_CANDY && newSweets.ColorAble())
                            {
                                //Generate Rainbow Sweets
                                newSweets.ColoredComponent.SetThisType(SweetsColorType.ColorType.UNIVERSAL);
                            }
                        }
                    }
                }
            }
        }
        return(needFill);
    }
Exemplo n.º 2
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;
            }
        }
    }
Exemplo n.º 3
0
    public List <SweetsController> MatchSweets(SweetsController sweet, int newX, int newY)
    {
        if (sweet.ColorAble())
        {
            SweetsColorType.ColorType color = sweet.ColoredComponent.ThisType;
            List <SweetsController>   matchSweetsInSameRow = new List <SweetsController>();
            List <SweetsController>   matchSweetsInSameCol = new List <SweetsController>();
            List <SweetsController>   matchingResult       = new List <SweetsController>();

            //row maching
            matchSweetsInSameRow.Add(sweet);
            //match the current row.(0->left, 1->right)
            for (int i = 0; i <= 1; i++)
            {
                for (int xDis = 1; xDis < columns; xDis++)
                {
                    int x = newX;
                    if (i == 0)  //traverse the left side.
                    {
                        x -= xDis;
                    }
                    if (i == 1)  //traverse the right side.
                    {
                        x += xDis;
                    }
                    if (x < 0 || x >= columns)  //encounter the border.
                    {
                        break;
                    }

                    if (sweets[x, newY].ColorAble() && sweets[x, newY].ColoredComponent.ThisType == color)
                    {
                        matchSweetsInSameRow.Add(sweets[x, newY]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //"L","T" shape matching.
            if (matchSweetsInSameRow.Count >= 3)
            {
                for (int i = 0; i < matchSweetsInSameRow.Count; i++)
                {
                    matchingResult.Add(matchSweetsInSameRow[i]);
                }
                for (int j = 0; j <= 1; j++)  //只需扫描被调换元素的列而无需所有行匹配成功元素?
                {
                    //apply col-matching to the first element.
                    for (int yDis = 1; yDis <= rows; yDis++)
                    {
                        int y = newY;
                        if (j == 0)
                        {
                            y -= yDis;
                        }
                        if (j == 1)
                        {
                            y += yDis;
                        }
                        if (y < 0 || y >= rows)
                        {
                            break;
                        }

                        if (sweets[matchSweetsInSameRow[0].X, y].ColorAble() && sweets[matchSweetsInSameRow[0].X, y].ColoredComponent.ThisType == color)
                        {
                            matchSweetsInSameCol.Add(sweets[matchSweetsInSameRow[0].X, y]);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                if (matchSweetsInSameCol.Count < 2)
                {
                    matchSweetsInSameCol.Clear();
                }
                else
                {
                    for (int k = 0; k < matchSweetsInSameCol.Count; k++)
                    {
                        matchingResult.Add(matchSweetsInSameCol[k]);
                    }
                }
            }

            if (matchingResult.Count >= 3)
            {
                //If successed in matching sweets in one row, then return.
                return(matchingResult);
            }
            else
            {
                matchSweetsInSameRow.Clear();
                matchSweetsInSameCol.Clear();
            }

            //columns matching
            matchSweetsInSameCol.Add(sweet);  //basic element
            //match the current row to the left and to the right.
            for (int i = 0; i <= 1; i++)
            {
                for (int yDis = 1; yDis < rows; yDis++)
                {
                    int y = newY;
                    if (i == 0)  //traverse the up side.
                    {
                        y -= yDis;
                    }
                    if (i == 1)  //traverse the down side.
                    {
                        y += yDis;
                    }
                    if (y < 0 || y >= rows)  //encounter the border.
                    {
                        break;
                    }

                    if (sweets[newX, y].ColorAble() && sweets[newX, y].ColoredComponent.ThisType == color)
                    {
                        matchSweetsInSameCol.Add(sweets[newX, y]);
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (matchSweetsInSameCol.Count >= 3)
            {
                for (int i = 0; i < matchSweetsInSameCol.Count; i++)
                {
                    matchingResult.Add(matchSweetsInSameCol[i]);
                }
                for (int j = 0; j <= 1; j++)
                {
                    for (int xDis = 1; xDis < columns; xDis++)
                    {
                        int x = newX;
                        if (j == 0)
                        {
                            x -= xDis;
                        }
                        if (j == 1)
                        {
                            x += xDis;
                        }
                        if (x < 0 || x >= columns)
                        {
                            break;
                        }

                        if (sweets[x, matchSweetsInSameCol[0].Y].ColorAble() && sweets[x, matchSweetsInSameCol[0].Y].ColoredComponent.ThisType == color)
                        {
                            matchSweetsInSameRow.Add(sweets[x, matchSweetsInSameCol[0].Y]);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                if (matchSweetsInSameRow.Count < 2)
                {
                    matchSweetsInSameRow.Clear();
                }
                else
                {
                    for (int k = 0; k < matchSweetsInSameRow.Count; k++)
                    {
                        matchingResult.Add(matchSweetsInSameRow[k]);
                    }
                }
            }

            if (matchingResult.Count >= 3)
            {
                //If successed in matching sweets in one column.
                return(matchingResult);
            }
        }

        return(null);
    }