Esempio n. 1
0
    /// <summary>
    /// Checks in every direction around the centerPos for atleast
    /// 4 animal tiles of the same type in a row. Animal rows that are found
    /// are marked matched at the tile level.
    /// </summary>
    /// <param name="centerPos"></param>
    public void CheckForMatches(IntVector2 centerPos)
    {
        if (!IsValidIndex(centerPos))
        {
            return;
        }
        AnimalTile centerAnimal = GetAnimal(centerPos);

        if (AnimalTile.IsNullOrMoving(centerAnimal))
        {
            return;
        }

        //Match count for each direction.
        int[] matchCounts = { 1, 1, 1, 1, 0, 0, 0, 0 };

        //Count matches found extending out from (x,y) in each direction.
        foreach (SwipeDirection direction in Enum.GetValues(typeof(SwipeDirection)))
        {
            IntVector2 currPos    = GetNextNeighbor(centerPos, direction);
            AnimalTile currAnimal = GetAnimal(currPos);

            //While neighbor in direction is the same as center. Add to count and move to next
            while (!AnimalTile.IsNullOrMoving(currAnimal) && AnimalTile.IsSameType(centerAnimal, currAnimal))
            {
                matchCounts[(int)direction]++;

                currPos    = GetNextNeighbor(currPos, direction);
                currAnimal = GetAnimal(currPos);
            }
        }

        //Only iterate: North, NorthEast, East, SouthEast. The rest can be considered part of these.
        for (int i = 0; i < 4; i++)
        {
            IntVector2     currStartPos  = new IntVector2(centerPos.x, centerPos.y);
            SwipeDirection currDirection = (SwipeDirection)i;
            int            currLength    = 0;

            //Center is not start of match
            SwipeDirection invertedDirection = InputController.InvertDirection(currDirection);

            //Find correct start, and how long it really is.
            currStartPos = GetNextNeighbor(currStartPos, invertedDirection, matchCounts[i + 4]);
            currLength  += matchCounts[i + 4] + matchCounts[i];

            //If the row is 4 or longer and has an unmatched animal in it. Remove it.
            if (currLength >= MinMatch && DoesRowHaveNonMatched(currStartPos, currLength, currDirection))
            {
                IsReady = false;
                //Set them marked in the board. And give player points.
                MarkRowMatched(currStartPos, currLength, currDirection);
                playerScore.AddPoints(currStartPos, currLength, currDirection);
            }
        }
    }