public void Init(CellGrid.Cell cell, CellGrid cellGrid) { _listOfFreeConnections = new List <TileConnector>(); for (int i = 0; i < connections.Length; ++i) { connections[i].ownerTile = this; if (connections[i].IsConnected) { continue; } if (connections[i].direction == TileConnector.DIRECTION.TOP) { if (cell.row == cellGrid.TotalRows - 1 || cellGrid.GetCell(cell.row + 1, cell.col).IsOccupied()) { connections[i].SetToInvalid(); } } else if (connections[i].direction == TileConnector.DIRECTION.BOT) { if (cell.row == 0 || cellGrid.GetCell(cell.row - 1, cell.col).IsOccupied()) { connections[i].SetToInvalid(); } } else if (connections[i].direction == TileConnector.DIRECTION.LEFT) { if (cell.col == 0 || cellGrid.GetCell(cell.row, cell.col - 1).IsOccupied()) { connections[i].SetToInvalid(); } } else if (connections[i].direction == TileConnector.DIRECTION.RIGHT) { if (cell.col == cellGrid.TotalCols - 1 || cellGrid.GetCell(cell.row, cell.col + 1).IsOccupied()) { connections[i].SetToInvalid(); } } if (!connections[i].IsInvalid) { _listOfFreeConnections.Add(connections[i]); } } cell.tile = this; _cell = cell; }
void ConnectIfPossible(CellGrid.Cell nextCell) { if (nextCell.IsOccupied()) { TileConnector nextCellConnector = nextCell.tile.GetOppositeConnector(direction); if (nextCellConnector != null) { Connect(this, nextCellConnector); } else { _isInvalid = true; } } }
public void UpdateBasedOnGrd(CellGrid cellGrid) { CellGrid.Cell myCell = ownerTile.Cell; int r = myCell.row; int c = myCell.col; if (direction == DIRECTION.TOP) { ConnectIfPossible(cellGrid.GetCell(r + 1, c)); } else if (direction == DIRECTION.BOT) { ConnectIfPossible(cellGrid.GetCell(r - 1, c)); } else if (direction == DIRECTION.LEFT) { ConnectIfPossible(cellGrid.GetCell(r, c - 1)); } else if (direction == DIRECTION.RIGHT) { ConnectIfPossible(cellGrid.GetCell(r, c + 1)); } }
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); }
void CreateTile(CellGrid.Cell cell, TILE_TYPE tileType, TileConnector parentConnector = null) { ZUtils.RandomizeList <Tile>(ref _baseTileSet); Tile newTile = null; if (tileType == TILE_TYPE.START) { newTile = FindStartTile(); } else if (tileType == TILE_TYPE.CONNECTION) { newTile = FindConnectionTile(cell); } else if (tileType == TILE_TYPE.CONNECTION_FILLER) { newTile = FindConnectionTileToFill(cell); } else if (tileType == TILE_TYPE.END) { newTile = FindEndTile(parentConnector); _endTile = _endTile; } else if (tileType == TILE_TYPE.DEAD_END) { newTile = FindDeadEndTile(parentConnector); } if (newTile == null) { // No valid option right now, move to next step return; } GameObject newTileGO = Instantiate(newTile.gameObject); Transform newTileT = newTileGO.transform; newTileT.SetParent(_dungeonT); newTileT.position = cell.Position; newTile = newTileGO.GetComponent <Tile>(); if (parentConnector != null) { TileConnector childConnector = newTile.GetOppositeConnector(parentConnector.direction); if (childConnector != null) { TileConnector.Connect(childConnector, parentConnector); } else { Debug.Log("Error: Unable to find a connector so skipping"); return; } } newTile.Init(cell, _cellGrid); if (AllowNewConnections(tileType)) { // Add free connections List <TileConnector> freeConnectionList = newTile.GetFreeConnections(); for (int i = 0; i < freeConnectionList.Count; ++i) { _listOfFreeConnections.Add(freeConnectionList[i]); } } // Reiterate free connection list for new blockers for (int i = 0; i < _listOfFreeConnections.Count; ++i) { _listOfFreeConnections[i].UpdateBasedOnGrd(_cellGrid); if (!_listOfFreeConnections[i].IsFree()) { _listOfFreeConnections[i].SetToInvalid(); _listOfFreeConnections.RemoveAt(i); --i; } } _dungeonTiles.Add(newTile); _cellObjectList.Add(newTileGO); _numTilesCreated++; if (tileType == TILE_TYPE.START) { _startTile = newTile; } else if (tileType == TILE_TYPE.END) { _endTile = newTile; } //Debug.Log("GenNextTile: " + _numCellsCreated.ToString() + " " + _listOfFreeConnections.Count.ToString()); }