private void randomExpand(Floor cur_floor) { if (visitedCells.Count < cur_floor.cells.Count) { Cell rndCell = visitedCells[Random.Range(0, visitedCells.Count)]; Edge rndEdge = rndCell.edges[Random.Range(0, rndCell.edges.Count)]; rndCell = rndEdge.getRoomA(); Cell otherSide = rndEdge.getRoomB(); CellTuple t1 = new CellTuple(otherSide, rndCell); if (!blockedEdges.Contains(t1)) { rndEdge.setSides(Random.Range(0, 2)); blockedEdges.Add(t1); visitedEdges.Add(rndEdge); if (!visitedCells.Contains(rndCell)) { visitedCells.Add(rndCell); } if (!visitedCells.Contains(otherSide)) { visitedCells.Add(otherSide); } } randomExpand(cur_floor); } else { // Debug.Log("pathFound"); } }
public void Initialize(GridData gridData, GameController gameController) { this.gridData = gridData; this.gameController = gameController; if (gridData.mapSize.x % 2 == 0) { transform.Translate(Vector3.left * HexMetrics.OuterRadius); } if (gridData.mapSize.y % 2 == 0) { transform.Translate(Vector3.down * HexMetrics.OuterRadius); } cells = new List <Cell>(); roads = new List <Road>(); bridgeCells = new List <Cell>(); residentialTiles = new List <Residential>(); industrialTiles = new List <Industrial>(); pathCache = new Dictionary <Cell, List <Path> >(); tileDict = new Dictionary <Vector2, Cell>(); float offsetX = (-(gridData.mapSize.x / 2f) + 1 + (gridData.mapSize.x % 2 == 0 ? .5f : 0)) * HexMetrics.InnerRadius * 2f; float offsetY = (-(gridData.mapSize.y / 2f) + .5f + (gridData.mapSize.y % 2 == 0 ? .25f : 0)) * HexMetrics.InnerRadius * 2f; transform.position = new Vector2(offsetX, offsetY); foreach (CellData cellData in gridData.cellData) { if (!cellData.enabled) { continue; } Cell cell = Instantiate(cellObj, this.transform).GetComponent <Cell>(); cell.Initialize(cellData); Tile tile = Instantiate(emptyTileObj, cell.transform).GetComponent <Tile>(); tile.Initialize(cell); emptyTiles.Add((Empty)tile); cell.residentialCapacity = cell.cellData.baseResidentialCapacity; cell.industrialCapacity = cell.cellData.baseIndustrialCapacity; cell.imaginaryResidentialCapacity = 1; cell.roadConnections = new List <Cell>(); cells.Add(cell); tileDict.Add(cellData.coords, cell); } List <CellTuple> visited = new List <CellTuple>(); foreach (Cell cell in cells) { cell.CalculateAdjacent(); foreach (Cell adj in cell.adjacent) { CellTuple tuple = new CellTuple(cell, adj); if (!visited.Contains(tuple)) { Road road = Instantiate(roadObj, transform).GetComponent <Road>(); road.SetPositions(cell.transform.position + Vector3.back * 3, adj.transform.position + Vector3.back * 3); road.Initialize(cell, adj); roads.Add(road); visited.Add(tuple); visited.Add(tuple.Complement()); } } } foreach (Bridge bridge in gridData.bridges) { Cell cell = Instantiate(cellObj, this.transform).GetComponent <Cell>(); cell.Initialize(bridge.coords); bridgeCells.Add(cell); } visited.Clear(); foreach (Cell cell in bridgeCells) { cell.CalculateAdjacent(); foreach (Cell adj in cell.adjacent) { CellTuple tuple = new CellTuple(cell, adj); if (!visited.Contains(tuple)) { Road road = Instantiate(roadObj, transform).GetComponent <Road>(); road.SetPositions(cell.transform.position + Vector3.back * 3, adj.transform.position + Vector3.back * 3); road.Initialize(cell, adj); roads.Add(road); visited.Add(tuple); visited.Add(tuple.Complement()); } } cell.adjacent.Clear(); } roadsAvailable = gridData.roadCount; StartCoroutine(ContinousCalculateCommutes()); }