Beispiel #1
0
    IEnumerator UpdateBoardIndices(MatchInfo match)
    {
        int minX = match.GetMinX(); // Minimum match horizontal position
        int maxX = match.GetMaxX(); // Maximum match horizontal position
        int minY = match.GetMinY(); // Minimum match vertical position
        int maxY = match.GetMaxY(); // Maximum match vertical position

        List <Item> fallingItems = new List <Item> {
        };                                              // List to hold items that must fall

        if (minY == maxY)                               // Horizontal match, we have to update several columns (for the remaining upper items)
        {
            for (int i = minX; i <= maxX; i++)          //Loop through horizontal positions
            {
                for (int j = minY; j < height - 1; j++) //Loop through vertical positions above match
                {
                    Item upperIndex = _items[i, j + 1];
                    Item current    = _items[i, j];

                    // Do the swappingz
                    _items[i, j]     = upperIndex;
                    _items[i, j + 1] = current;
                    _items[i, j].SetPosition(_items[i, j].x, _items[i, j].y - 1);
                    fallingItems.Add(_items[i, j]); // Set the item to fall
                }

                // Fill empty space with new random item
                _items[i, height - 1] = InstantiateDoddle(i, height - 1, 0, 1);
                Item newItem = _items[i, height - 1];
                fallingItems.Add(newItem); // Fall this new item
            }
        }
        else if (minX == maxX) // Vertical match, we have to update just one column
        {
            int matchHeight = (maxY - minY) + 1;
            int currentX    = minX;
            for (int j = minY + matchHeight; j <= height - 1; j++) // Loop throught vertical positions above match
            {
                Item lowerIndex = _items[currentX, j - matchHeight];
                Item current    = _items[currentX, j];

                //Do the swappingz
                _items[currentX, j - matchHeight] = current;
                _items[currentX, j] = lowerIndex;
            }

            for (int y = 0; y < height - matchHeight; y++) // Update all items on column
            {
                _items[currentX, y].SetPosition(currentX, y);
                fallingItems.Add(_items[currentX, y]);
            }
            for (int i = 0; i < match.Count; i++)                                                 // Loop through match count
            {
                int newYPos = (height - 1) - i;                                                   // Vertical position for the new item
                _items[currentX, newYPos] = InstantiateDoddle(currentX, newYPos, 0, match.Count); // Instantiate new item
                fallingItems.Add(_items[currentX, newYPos]);                                      // Set new item to fall
            }
        }

        yield return(StartCoroutine(FallItems(fallingItems))); // Fall all items setted to fall and waits for finish

        CheckForMatches();                                     // Check for more matches after update

        bool hasPossibleMoves = CheckForPossibleMoves();

        if (!hasPossibleMoves)
        {
            Debug.Log("No more possible moves. Shuffling board.");
            ShuffleBoard(new System.Random());
        }
        ;

        yield return(null);
    }