private bool CanCreateStairs( MazeCell cell, MazeCell otherCell, MazeDirection direction) { // To create stairs, the strairs must have a altitude 0 low landing cell in the same // room as the stairs, and there must not be an existing cell at the location of the // future high landing cell. bool result = false; if (null != stairsPrefab && 0 == cell.altitude) { IntVector2 lowLandingCoordinates = cell.coordinates + direction.GetOpposite().ToIntVector2(); IntVector2 highLandingCoordinates = otherCell.coordinates + direction.ToIntVector2(); if (ContainsCoordinates(lowLandingCoordinates) && ContainsCoordinates(highLandingCoordinates)) { MazeCell lowLandingCell = GetCell(lowLandingCoordinates); if (null != lowLandingCell && cell.GetRoomNumber() == lowLandingCell.GetRoomNumber() && null == GetCell(highLandingCoordinates)) { result = true; } } } return(result); }
private void DoNextGenerationStep( List <MazeCell> activeCells) { int currentIndex = activeCells.Count - 1; Debug.Assert(currentIndex >= 0); MazeCell currentCell = activeCells [currentIndex]; if (currentCell.IsFullyInitialized) { activeCells.RemoveAt(currentIndex); return; } MazeDirection direction = currentCell.RandomUninitializedDirection; IntVector2 coordinates = currentCell.coordinates + direction.ToIntVector2(); if (ContainsCoordinates(coordinates)) { MazeCell neighbor = GetCell(coordinates); if (neighbor == null) { if (null != cellTreePrefab && 0 == Random.Range(0, 77)) { neighbor = CreateCell(coordinates, 0, cellTreePrefab); activeCells.Add(neighbor); } else { neighbor = CreateCell(coordinates, 0, cellPrefab); activeCells.Add(neighbor); CreatePassage(activeCells, currentCell, neighbor, direction); } } else if (currentCell.GetRoomNumber() == neighbor.GetRoomNumber() && doorProbability > 0 && currentCell.altitude == neighbor.altitude) { CreatePassageInSameRoom(currentCell, neighbor, direction); } else { CreateWall(currentCell, neighbor, direction); } } else { CreateWall(currentCell, null, direction); } }
private void CreatePassage( List <MazeCell> activeCells, MazeCell cell, MazeCell otherCell, MazeDirection direction) { // Create one of the possible passable edges from one a cell in one // room to a cell in another. MazePassage prefab = passagePrefab; // generic passage if (cell.altitude == 0 && Random.value < doorProbability) { // Possibly make a door from one room to the next prefab = doorPrefab; if (Random.value < doorProbability && CanCreateStairs(cell, otherCell, direction)) { // Instead of a door, let's try to make stairs. Stiars occupy // both cell and other cell and enforce constraints on // a low landing cell at the base of the stairs and a // high landing cell at the top of the stairs. prefab = passagePrefab; // Not a door after all! cell.accessory = Instantiate(stairsPrefab) as MazeCellAccessory; cell.accessory.cell = cell; cell.accessory.transform.parent = cell.transform; cell.accessory.transform.localPosition = Vector3.zero; cell.accessory.transform.localRotation = direction.ToRotation(); otherCell.Initialize(cell.GetRoomNumber()); IntVector2 highLandingCoordinates = otherCell.coordinates + direction.ToIntVector2(); MazeCell highLandingCell = CreateCell( highLandingCoordinates, 1, cellPrefab); activeCells.Add(highLandingCell); highLandingCell.Initialize(otherCell.GetRoomNumber() + 1); MazePassage landingPassage = Instantiate(prefab) as MazePassage; landingPassage.Initialize(otherCell, highLandingCell, direction); GameObject newColumn = Instantiate(columnPrefab) as GameObject; newColumn.transform.parent = otherCell.transform; newColumn.transform.localPosition = Vector3.zero; newColumn.transform.localRotation = direction.ToRotation(); if (null != railingColumnPrefab) { GameObject newRailingColumn = Instantiate(railingColumnPrefab) as GameObject; newRailingColumn.transform.parent = highLandingCell.transform; newRailingColumn.transform.localPosition = Vector3.zero; newRailingColumn.transform.localRotation = direction.ToRotation(); } } else { // Doors alsways change room numbers otherCell.Initialize(cell.GetRoomNumber() + 1); } } else { // This passage is neither door nor stair otherCell.Initialize(cell.GetRoomNumber()); otherCell.altitude = cell.altitude; otherCell.transform.localPosition = new Vector3(otherCell.transform.localPosition.x, otherCell.transform.localPosition.y + otherCell.altitude * 1.078f, otherCell.transform.localPosition.z); } MazePassage passage = Instantiate(prefab) as MazePassage; passage.Initialize(cell, otherCell, direction); }