private MazeUnit createNewUnitInMaze(char dir, int xstart, int zstart) { MazeUnit oldUnit = mazeFloor[xstart, zstart]; MazeUnit newUnit = Instantiate(mazeUnitPrefab); newUnit.setDirection(dir); mazeFloor[XafterDirection(xstart, dir), ZafterDirection(zstart, dir)] = newUnit; newUnit.setPos(XafterDirection(xstart, dir), ZafterDirection(zstart, dir)); placeNewMazeUnit(newUnit, oldUnit, dir); lastMazeUnit = newUnit; return(newUnit); }
private void createEmptyFloor(int level) { for (int i = 0; i < sizeX; i++) { int j = 0; int j2 = sizeZ - 1; MazeUnit edgeUnit = Instantiate(mazeUnitPrefab); edgeUnit.setPos(i, j); edgeUnit.transform.SetParent(transform); edgeUnit.transform.localPosition = new Vector3(i, level, j); MazeUnit edgeUnit2 = Instantiate(mazeUnitPrefab); edgeUnit2.setPos(i, j2); edgeUnit2.transform.SetParent(transform); edgeUnit2.transform.localPosition = new Vector3(i, level, j2); MazeUnit edgeUnit3 = Instantiate(mazeUnitPrefab); edgeUnit3.setPos(j, i); edgeUnit3.transform.SetParent(transform); edgeUnit3.transform.localPosition = new Vector3(j, level, i); MazeUnit edgeUnit4 = Instantiate(mazeUnitPrefab); edgeUnit4.setPos(j2, i); edgeUnit4.transform.SetParent(transform); edgeUnit4.transform.localPosition = new Vector3(j2, level, i); } }
public void generateMazeFloor(int xStart, int zStart, char direction, int level, MazeUnit[,] floor, int size) { sizeX = size; sizeZ = size; mazeFloor = floor; mazeUnitList = new List <MazeUnit>(); MazeUnit firstUnit = Instantiate(mazeUnitPrefab); mazeFloor[xStart, zStart] = firstUnit; mazeFloor[xStart, zStart].setPos(xStart, zStart); mazeFloor[xStart, zStart].transform.SetParent(transform); mazeFloor[xStart, zStart].transform.localPosition = new Vector3(xStart, level, zStart); if (isOnEdge(xStart, zStart)) { mazeFloor[xStart, zStart].deleteWall('E'); mazeFloor[xStart, zStart].deleteWall('W'); mazeFloor[xStart, zStart].deleteWall('S'); mazeFloor[xStart, zStart].deleteWall('N'); if (level == startingLevel) { for (int i = xStart - 2; i < xStart + 3; i++) { for (int j = zStart - 2; j < zStart + 3; j++) { if (i != xStart || j != zStart) { MazeUnit edgeUnit = Instantiate(mazeUnitPrefab); edgeUnit.setPos(i, j); edgeUnit.transform.SetParent(transform); edgeUnit.transform.localPosition = new Vector3(i, level, j); edgeUnit.onlyLeaveFloor(); } } } } } if (level != startingLevel) { mazeFloor[xStart, zStart].deleteFloor(); } int xcurr = XafterDirection(xStart, direction); int zcurr = ZafterDirection(zStart, direction); if (!withinBounds(xStart, zStart, direction)) { mazeFloor[xStart, zStart].deleteWall(direction); MazeUnit[,] f = new MazeUnit[sizeX, sizeZ]; f[xStart, zStart] = firstUnit; for (int i = xStart - 1; i < xStart + 2; i++) { for (int j = zStart - 1; j < zStart + 2; j++) { if (withinBounds(i, j, 'C') && isOnEdge(i, j)) { generateMazeFloor(i, j, getOppositeDirection(direction), level, f, size); return; } } } } char randDir = generateRandomDirection(); mazeUnitList.Add(createNewUnitInMaze(direction, xStart, zStart)); int superCount = 0; while (mazeUnitList.Count > 0 && superCount < 200) { superCount++; xcurr = mazeUnitList[0].x; zcurr = mazeUnitList[0].z; int count = 0; while (!isEmptyMazeSpot(xcurr, zcurr, randDir, mazeFloor) && count < 4) { count++; randDir = generateRandomDirection(); } if (!withinBounds(xcurr, zcurr, randDir)) { mazeUnitList.Remove(mazeUnitList[0]); } else if (!isEmptyMazeSpot(xcurr, zcurr, randDir, mazeFloor)) { mazeFloor[xcurr, zcurr].deleteWall(randDir); mazeFloor[XafterDirection(xcurr, randDir), ZafterDirection(zcurr, randDir)].deleteWall(getOppositeDirection(randDir)); mazeUnitList.Remove(mazeUnitList[0]); } else { mazeUnitList.Add(createNewUnitInMaze(randDir, xcurr, zcurr)); } } for (int i = 0; i < sizeX; i++) { for (int j = 0; j < sizeZ; j++) { if (mazeFloor[i, j] != null) { mazeFloor[i, j].deleteAllWallCheck(); } } } //placing stairs! int xStair = lastMazeUnit.x; int zStair = lastMazeUnit.z; char dirStair = lastMazeUnit.dir; mazeFloor[xStair, zStair].deleteCeiling(); Stairs s = Instantiate(stairsPrefab); s.transform.SetParent(transform); s.transform.localPosition = new Vector3(xStair, level, zStair); s.setDirOfStairs(dirStair); for (int i = xStair - 1; i < xStair + 2; i++) { for (int j = zStair - 1; j < zStair + 2; j++) { if (i != xStair || j != zStair) { MazeUnit edgeUnit = Instantiate(mazeUnitPrefab); edgeUnit.setPos(i, j); edgeUnit.transform.SetParent(transform); edgeUnit.transform.localPosition = new Vector3(i, level, j); edgeUnit.onlyLeaveCeiling(); } } } //run traceback getTraceBackDistance(xStart, zStart, xStair, zStair); if (level < height) { generateMazeFloor(xStair, zStair, dirStair, level + 1, new MazeUnit[sizeX, sizeZ], sizeX); } else { lastStairX = xStair; lastStairZ = zStair; } }