예제 #1
0
    /// <summary>
    /// Gets a chain of tiles that fullfill the matching condition along a given direction.
    /// </summary>
    /// <param name="startCoordinates">coordinates of the startTile</param>
    /// <param name="direction">search direction(s)</param>
    /// <param name="matchingCondition">matching condition</param>
    /// <param name="excludeStartTile">whether the tile at start coordinates should be included or excluded</param>
    /// <returns>number of consecutive tiles that fullfill the matching condition</returns>
    public List <BoardTile> GetMatchingChainInDirection(Vector2Int startCoordinates, Direction direction, System.Predicate <BoardTile> matchingCondition, bool excludeStartTile = false)
    {
        List <BoardTile> matchingTiles = new List <BoardTile>();

        if (excludeStartTile || matchingCondition(GetTileAt(startCoordinates)))
        {
            if (!excludeStartTile)
            {
                matchingTiles.Add(GetTileAt(startCoordinates));
            }

            foreach (Vector2Int directionVector in DirectionHelper.DirectionToVectors(direction))
            {
                Vector2Int searchCoordinates = startCoordinates + directionVector;

                while (searchCoordinates.IsBetween(Vector2Int.zero, Dimensions) && matchingCondition(GetTileAt(searchCoordinates)))
                {
                    matchingTiles.Add(GetTileAt(searchCoordinates));
                    searchCoordinates += directionVector;
                }
            }
        }

        return(matchingTiles);
    }