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[columns,rows,5];
- //initialize me a 3D bool array.
- for ( int i = 0; i < columns; i++ )
- {
- for ( int j = 0; j < rows; j++ )
- {
- for ( int w = 0; w < 4; w++ )
- {
- // set all the walls to true.
- grid [i, j, w] = true;
- }
- }
- }
- //we now have a grid of "cells"
- // [i,x,x] tracks column number of cells
- // [x,j,x] tracks row number of cells
- // [x,x,0] is true if a cells top wall exists // [x,x,1] is true if a cells right wall exists
- // [x,x,2] is true if a cells bot wall exists // [x,x,3] is true if a cells left wall exists
- // [x,x,4] is true if a cell has been visited
- //to start, pick a random cell mark it as the current cell and mark it as visited
- //grid [current_i = Random.Range ( 1, columns -1), current_j = Random.Range ( 1, rows -1 ), 4] = true;
- //to start debugging, let's set the initial cell to 0,0 and hope we get an out of range error
- grid [current_i = 1, current_j = 1, 4] = true;
- //this, because i'm sick of moving my camera to where the maze starts each time
- // -current_j because reasons
- cameraStart.transform.position = new Vector3 ( current_i * spacing, -current_j * spacing, -15 );
- //runs this loop an arbitrary number of times... for now
- for ( int TODO = 0; TODO < 30; TODO++ ) // : replace this with a "while (there are still unvisisted cells) loop"
- {
- //make an empty list called unvisitedNeighbours
- List<int> unvisitedNeighbours = new List<int>();
- //if we're not on the topmost row, check if the cell above us is unvisited
- if ( current_j !=0)
- {
- //otherwise, check to see if the cell above us is unvisited
- if ( grid [current_i, current_j - 1, 4] == false )
- {
- unvisitedNeighbours.Add ( 0 );
- }
- }
- //only check the cell above us if we aren't on the top row
- if ( current_i != columns - 1 )
- {
- //if there is, etc.
- if ( grid [current_i + 1, current_j, 4] == false )
- {
- unvisitedNeighbours.Add ( 1 );
- }
- }
- if ( current_j != rows - 1 )
- {
- if ( grid [current_i, current_j + 1, 4] == false )
- {
- unvisitedNeighbours.Add ( 2 );
- }
- }
- if ( current_i != 0 )
- {
- if ( grid [current_i - 1, current_j, 4] == false )
- {
- unvisitedNeighbours.Add ( 3 );
- }
- }
- //if there was any unvisisted neighbours
- if ( unvisitedNeighbours.Count > 0 )
- {
- //then randomly choose one element from the unvisisted neighbours list
- int chosen_unvisisted = Random.Range ( 0, unvisitedNeighbours.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 visited
- 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 )
- {
- //-j because we want to instantiate a grid 'left down' rather than 'left up'
- //this is why '// -current_j because reasons' is because reasons...
- //:^)
- 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