Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class MazeGenRecursive : MonoBehaviour {
- List<Vector3> stack;
- List<Vector3> availableCells;
- public bool active;
- [RangeAttribute(5, 15)]
- public int width, height, depth;
- bool[,,,] maze;
- private Vector3 nullVector = new Vector3(-1, -1, -1);
- //lets make it: up - Y axis, forward - Z axis, and sideways - X axis
- float[,] cellScan = new float[6, 3] {{0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}};
- //up //forward //right //back //left //down
- public void Activate(int w, int h, int d) {
- width = w;
- height = h;
- depth = d;
- if (active) {
- Debug.Log ("Initializing...");
- InitializeMaze ();
- Debug.Log ("Generating...");
- GenerateMaze ();
- Debug.Log ("Displaying...");
- MazeSpawner mazeSpawner = GetComponent<MazeSpawner>();
- mazeSpawner.InstantiateMaze (maze);
- }
- }
- void InitializeMaze() {
- maze = new bool[width,height,depth,6];
- for (int x = 0; x < width; x++) {
- for (int z = 0; z < depth; z++) {
- for (int y = 0; y < height; y++) {
- for (int n = 0; n < 6; n++) {
- maze[x,y,z,n] = true; //[x][z][y][n]
- } //top-1, front-2, right-3, back-4, left-5, bottom-6
- }
- }
- }
- }
- //[x][z][y][1 - 6] those 6 correspond to the walls
- void GenerateMaze() {
- stack = new List<Vector3>();
- int remainingCells = width * height * depth - 1;
- Vector3 current = Vector3.zero;
- while (remainingCells > 0) {
- Vector3 choice = ScanAdjacentCells(current);
- //Debug.Log (choice);
- if (choice != nullVector) {
- RemoveConnectingWalls(current, choice);
- stack.Insert (stack.Count, current);
- current = choice;
- remainingCells --;
- } else {
- current = stack[stack.Count - 1];
- stack.Remove (stack[stack.Count - 1]);
- }
- }
- }
- Vector3 ScanAdjacentCells(Vector3 currentPos) {
- availableCells = new List<Vector3>();
- for (int i = 0; i < 6; i ++) {
- Vector3 cellToScan = new Vector3();
- cellToScan.x = currentPos.x + cellScan[i,0];
- cellToScan.y = currentPos.y + cellScan[i,1];
- cellToScan.z = currentPos.z + cellScan[i,2];
- if (cellToScan.x >= 0 && cellToScan.x < width && cellToScan.y >= 0 && cellToScan.y < height && cellToScan.z >= 0 && cellToScan.z < depth) {
- bool cellOpen = ScanCell (cellToScan);
- if (cellOpen) {
- availableCells.Add (cellToScan);
- }
- }
- }
- if (availableCells.Count > 0) {
- int selection = Random.Range (0, availableCells.Count - 1);
- return availableCells[selection];
- }
- return nullVector;
- }
- bool ScanCell(Vector3 cellPos) {
- bool flag = true;
- for (int i = 0; i < 6; i ++) {
- if (!(maze[(int)cellPos.x,(int)cellPos.y,(int)cellPos.z,i])) {
- flag = false;
- break;
- }
- }
- return flag;
- }
- void RemoveConnectingWalls(Vector3 cell1, Vector3 cell2) {
- Vector3 difference = cell2 - cell1;
- if (difference.y == 1) { //up
- maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,0] = false;
- maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,5] = false;
- } else if (difference.z == 1) { //forward
- maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,1] = false;
- maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,3] = false;
- } else if (difference.x == 1) { //right
- maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,2] = false;
- maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,4] = false;
- } else if (difference.z == -1) { //back
- maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,3] = false;
- maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,1] = false;
- } else if (difference.x == -1) { //left
- maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,4] = false;
- maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,2] = false;
- } else if (difference.y == -1) { //down
- maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,5] = false;
- maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,0] = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement