void ConnectIfPossible(CellGrid.Cell nextCell) { if (nextCell.IsOccupied()) { TileConnector nextCellConnector = nextCell.tile.GetOppositeConnector(direction); if (nextCellConnector != null) { Connect(this, nextCellConnector); } else { _isInvalid = true; } } }
Tile FindConnectionTileToFill(CellGrid.Cell cell) { // Find total neighbors on this cell int r = cell.row; int c = cell.col; // Top bool isTopNeeded = false; if (r + 1 < gridRows - 1) { CellGrid.Cell topCell = _cellGrid.GetCell(r + 1, c); if (topCell.IsOccupied()) { if (topCell.tile.GetOppositeConnector(TileConnector.DIRECTION.TOP) != null) { isTopNeeded = true; } } } // Bot bool isBotNeeded = false; if (r > 0) { CellGrid.Cell botCell = _cellGrid.GetCell(r - 1, c); if (botCell.IsOccupied()) { if (botCell.tile.GetOppositeConnector(TileConnector.DIRECTION.BOT) != null) { isBotNeeded = true; } } } // Right bool isRightNeeded = false; if (c + 1 < gridCols - 1) { CellGrid.Cell rightCell = _cellGrid.GetCell(r, c + 1); if (rightCell.IsOccupied()) { if (rightCell.tile.GetOppositeConnector(TileConnector.DIRECTION.RIGHT) != null) { isRightNeeded = true; } } } // Left bool isLeftNeeded = false; if (c > 0) { CellGrid.Cell leftCell = _cellGrid.GetCell(r, c - 1); if (leftCell.IsOccupied()) { if (leftCell.tile.GetOppositeConnector(TileConnector.DIRECTION.LEFT) != null) { isLeftNeeded = true; } } } int totalNeeded = 0; if (isTopNeeded) { totalNeeded++; } if (isBotNeeded) { totalNeeded++; } if (isRightNeeded) { totalNeeded++; } if (isLeftNeeded) { totalNeeded++; } if (totalNeeded == 1) { return(null); } for (int i = 0; i < _totalTilesInBaseTileSet; ++i) { Tile baseTile = _baseTileSet[i]; int numConnections = baseTile.connections.Length; if (numConnections == 1) { continue; } bool hasTop = false; bool hasBot = false; bool hasRight = false; bool hasLeft = false; for (int j = 0; j < numConnections; ++j) { if (baseTile.connections[j].direction == TileConnector.DIRECTION.TOP) { hasTop = true; } else if (baseTile.connections[j].direction == TileConnector.DIRECTION.BOT) { hasBot = true; } else if (baseTile.connections[j].direction == TileConnector.DIRECTION.RIGHT) { hasRight = true; } else if (baseTile.connections[j].direction == TileConnector.DIRECTION.LEFT) { hasLeft = true; } } int totalValid = 0; if (isTopNeeded && hasTop) { totalValid++; } else if (!isTopNeeded && !hasTop) { totalValid++; } if (isBotNeeded && hasBot) { totalValid++; } else if (!isBotNeeded && !hasBot) { totalValid++; } if (isRightNeeded && hasRight) { totalValid++; } else if (!isRightNeeded && !hasRight) { totalValid++; } if (isLeftNeeded && hasLeft) { totalValid++; } else if (!isLeftNeeded && !hasLeft) { totalValid++; } if (totalValid == 4) { return(baseTile); } } Debug.Log("Error: Unable to find a connection tile to fill so skipping"); return(null); }