//create a passage private void CreatePassage(TreeMazeCell curCell, TreeMazeCell neighbor, TreeDir dir) { TreeMazePassage passage = Instantiate(passagePrefab) as TreeMazePassage; passage.Init(curCell, neighbor, dir); passage = Instantiate(passagePrefab) as TreeMazePassage; passage.Init(neighbor, curCell, dir.GetOppositeDir()); }
public void Init(TreeMazeCell cell, TreeMazeCell otherCell, TreeDir dir) { this.cell = cell; this.otherCell = otherCell; this.dir = dir; cell.SetEdge(dir, this); transform.parent = cell.transform; transform.localPosition = Vector3.zero; transform.localRotation = dir.ToRotation(); }
//creating a wall private void CreateWall(TreeMazeCell curCell, TreeMazeCell neighbor, TreeDir dir) { TreeMazeWall wall = Instantiate(wallPrefab) as TreeMazeWall; wall.Init(curCell, neighbor, dir); if (neighbor != null) { wall = Instantiate(wallPrefab) as TreeMazeWall; wall.Init(neighbor, curCell, dir.GetOppositeDir()); } }
//create a cell public TreeMazeCell CreateCell(IntVector2 coordinates) { TreeMazeCell newCell = Instantiate(cellPrefab) as TreeMazeCell; cells[coordinates.r, coordinates.c] = newCell; newCell.coordinates = coordinates; newCell.name = "Tree Maze Cell " + coordinates.r + ", " + coordinates.c; newCell.transform.parent = transform; newCell.transform.localPosition = new Vector3(coordinates.r * wallSize, -(wallSize / 2f), coordinates.c * wallSize); //newCell.transform.Rotate(Vector3.right, 90f); return(newCell); }
//getting the current cell, checks if its all edges are initialized and if so removing it from the list //a cell will be fully initialized only when all its neighbors have been visited. //in order to prevent incorrect walls, we should pick a random direction that is not yet initialized for current cell private void DoNextStep(List <TreeMazeCell> activeCells) { int curIndex = activeCells.Count - 1; TreeMazeCell curCell = activeCells[curIndex]; if (curCell.IsFullyInit) { activeCells.RemoveAt(curIndex); return; } TreeDir direction = curCell.RandomNoDir; IntVector2 coor = curCell.coordinates + direction.ToIntVector2(); if (ContainsCoor(coor)) { // checking if the neighbor doesnt exists, if so we are creating it and adding a passage between the //neighbor and the current cell. If the neighbor already exists, separate them with wall. TreeMazeCell neighbor = GetNewCell(coor); if (neighbor == null) { neighbor = CreateCell(coor); CreatePassage(curCell, neighbor, direction); activeCells.Add(neighbor); } else { CreateWall(curCell, neighbor, direction); } } else { CreateWall(curCell, null, direction); } }