Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Maze : MonoBehaviour
- {
- static int columns = 40;
- static int rows = 40;
- static int spacing = 3; //adjust this to fit with the size of your prefabs (code will break if prefabs are not spacing x spacing ie.if they are not square)
- public Transform cameraStart;
- public GameObject topwall;
- public GameObject rightwall;
- public GameObject botwall;
- public GameObject leftwall;
- public GameObject floor;
- int current_i;
- int current_j;
- int chosen_i;
- int chosen_j;
- void Start ( )
- {
- bool[,,] grid = new bool[rows,columns,5];
- //Instantiate a 3D bool array to keep track of the algorithms state
- for ( int i = 0; i < columns; i++ )
- {
- for ( int j = 0; j < rows; j++ )
- {
- for ( int w = 0; w < 4; w++ )
- {
- grid [i, j, w] = true;
- }
- }
- }
- // [i,x,x] tracks column number while [x,j,x] tracks row number
- // [x,x,0] = top wall exists // [x,x,1] = right wall exists // [x,x,2] = bot wall exists// [x,x,3] = left wall exists
- // [x,x,4] = cell has been visisted //
- //to start, pick a random cell mark it as the current cell and mark it as visisted
- grid [current_i = Random.Range ( 0, columns ),current_j = Random.Range ( 0, rows ), 4] = true;
- //this, because i'm sick of moving my camera to where the maze starts each time
- cameraStart.transform.position = new Vector3 ( current_i * spacing, -current_j * spacing, -15 );
- for ( int TODO = 0; TODO < 30; TODO++ ) // : After i get the algorithm working, change this to a " WHILE (THERE ARE UNVISITED CELLS) " condition
- {
- //check for unvisisted cells adjacent to the current cell
- //if they exist, add them to a list called unvisisted neighbours
- List<int> unvisistedNeighbours = new List<int>();
- //if there is no cell above us, don't check there (ie. if we are on the top row)
- if ( current_j !=0)
- {
- //otherwise, check to see if the cell above us was visited yet
- if ( grid [current_i, current_j - 1, 4] == false )
- {
- unvisistedNeighbours.Add ( 0 );
- }
- }
- //if there's no cell to our right, don't try to check it
- if ( current_i != columns - 1 )
- {
- //if there is, etc.
- if ( grid [current_i + 1, current_j, 4] == false )
- {
- unvisistedNeighbours.Add ( 1 );
- }
- }
- if ( current_j != rows - 1 )
- {
- if ( grid [current_i, current_j + 1, 4] == false )
- {
- unvisistedNeighbours.Add ( 2 );
- }
- }
- if ( current_i != 0 )
- {
- if ( grid [current_i - 1, current_j, 4] == false )
- {
- unvisistedNeighbours.Add ( 3 );
- }
- }
- //if there was any unvisisted neighbours
- if ( unvisistedNeighbours.Count > 0 )
- {
- //then randomly choose one element from the unvisisted neighbours list
- int chosen_unvisisted = Random.Range ( 0, unvisistedNeighbours.Count );
- //if we choose the 0th element of that list, it's the cell above us
- if ( chosen_unvisisted == 0 )
- {
- //grab its i&j and remember them
- chosen_i = current_i;
- chosen_j = current_j - 1;
- //and remove the walls between the chosen cell and the current cell
- grid [current_i, current_j, 0] = false;
- grid [chosen_i, chosen_j, 2] = false;
- }
- //if we choose element 1 it's the cell to our right..
- if ( chosen_unvisisted == 1 )
- {
- //etc
- chosen_i = current_i + 1;
- chosen_j = current_j;
- grid [current_i, current_j, 1] = false;
- grid [chosen_i, chosen_j, 3] = false;
- }
- if ( chosen_unvisisted == 2 )
- {
- chosen_i = current_i;
- chosen_j = current_j + 1;
- grid [current_i, current_j, 2] = false;
- grid [chosen_i, chosen_j, 0] = false;
- }
- if ( chosen_unvisisted == 3 )
- {
- chosen_i = current_i - 1;
- chosen_j = current_j;
- grid [current_i, current_j, 3] = false;
- grid [chosen_i, chosen_j, 1] = false;
- }
- //now make the chosen cell the current cell and mark it as visisted
- current_i = chosen_i;
- current_j = chosen_j;
- grid [current_i, current_j, 4] = true;
- }
- }
- //draw it
- for ( int i = 0; i < columns; i++ )
- {
- for ( int j = 0; j < rows; j++ )
- {
- if ( grid [i, j, 0] == true )
- {
- Instantiate ( topwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
- }
- if ( grid [i, j, 1] == true )
- {
- Instantiate ( rightwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
- }
- if ( grid [i, j, 2] == true )
- {
- Instantiate ( botwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
- }
- if ( grid [i, j, 3] == true )
- {
- Instantiate ( leftwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
- }
- Instantiate ( floor, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement