Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.84 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Maze : MonoBehaviour
  6. {
  7.     static int columns = 40;
  8.     static int rows = 40;
  9.     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)
  10.  
  11.     public Transform cameraStart;
  12.     public GameObject topwall;
  13.     public GameObject rightwall;
  14.     public GameObject botwall;
  15.     public GameObject leftwall;
  16.     public GameObject floor;
  17.  
  18.     int current_i;
  19.     int current_j;
  20.     int chosen_i;
  21.     int chosen_j;
  22.  
  23.     void Start ( )
  24.     {
  25.         bool[,,] grid = new bool[columns,rows,5];
  26.  
  27.         //initialize me a 3D bool array.
  28.         for ( int i = 0; i < columns; i++ )
  29.         {
  30.             for ( int j = 0; j < rows; j++ )
  31.             {
  32.                 for ( int w = 0; w < 4; w++ )
  33.                 {
  34.                     // set all the walls to true.
  35.                     grid [i, j, w] = true;
  36.                 }
  37.             }
  38.         }
  39.  
  40.         //we now have a grid of "cells"
  41.  
  42.         // [i,x,x] tracks column number of cells
  43.         // [x,j,x] tracks row number of cells
  44.  
  45.         // [x,x,0] is true if a cells top wall exists // [x,x,1] is true if a cells right wall exists
  46.         // [x,x,2] is true if a cells bot wall exists // [x,x,3] is true if a cells left wall exists
  47.         // [x,x,4] is true if a cell has been visited  
  48.  
  49.  
  50.         //to start, pick a random cell mark it as the current cell and mark it as visited
  51.  
  52.         //grid [current_i = Random.Range ( 1, columns -1), current_j =  Random.Range ( 1, rows -1 ), 4] = true;
  53.  
  54.         //to start debugging, let's set the initial cell to 0,0 and hope we get an out of range error
  55.  
  56.         grid [current_i = 1, current_j = 1, 4] = true;
  57.  
  58.         //this, because i'm sick of moving my camera to where the maze starts each time
  59.         // -current_j because reasons
  60.         cameraStart.transform.position = new Vector3 ( current_i * spacing, -current_j * spacing, -15 );
  61.  
  62.         //runs this loop an arbitrary number of times... for now
  63.         for ( int TODO = 0; TODO < 30; TODO++ ) // : replace this with a "while (there are still unvisisted cells) loop"
  64.         {
  65.             //make an empty list called unvisitedNeighbours
  66.             List<int> unvisitedNeighbours = new List<int>();
  67.  
  68.            
  69.  
  70.             //if we're not on the topmost row, check if the cell above us is unvisited
  71.             if ( current_j !=0)
  72.             {
  73.                 //otherwise, check to see if the cell above us is unvisited
  74.                 if ( grid [current_i, current_j - 1, 4] == false )
  75.                 {
  76.                     unvisitedNeighbours.Add ( 0 );
  77.                 }
  78.             }
  79.  
  80.             //only check the cell above us if we aren't on the top row
  81.             if ( current_i != columns - 1 )
  82.             {
  83.                 //if there is, etc.
  84.                 if ( grid [current_i + 1, current_j, 4] == false )
  85.                 {
  86.                     unvisitedNeighbours.Add ( 1 );
  87.                 }
  88.             }
  89.  
  90.             if ( current_j != rows - 1 )
  91.             {
  92.                 if ( grid [current_i, current_j + 1, 4] == false )
  93.                 {
  94.                     unvisitedNeighbours.Add ( 2 );
  95.                 }
  96.             }
  97.  
  98.             if ( current_i != 0 )
  99.             {
  100.                 if ( grid [current_i - 1, current_j, 4] == false )
  101.                 {
  102.                     unvisitedNeighbours.Add ( 3 );
  103.                 }
  104.             }
  105.  
  106.             //if there was any unvisisted neighbours
  107.             if ( unvisitedNeighbours.Count > 0 )
  108.             {
  109.  
  110.                 //then randomly choose one element from the unvisisted neighbours list
  111.                 int chosen_unvisisted = Random.Range ( 0, unvisitedNeighbours.Count );
  112.  
  113.                 //if we choose the 0th element of that list, it's the cell above us
  114.  
  115.                 if ( chosen_unvisisted == 0 )
  116.                 {  
  117.                     //grab its i&j and remember them
  118.                     chosen_i = current_i;
  119.                     chosen_j = current_j - 1;
  120.                     //and remove the walls between the chosen cell and the current cell
  121.                     grid [current_i, current_j, 0] = false;
  122.                     grid [chosen_i, chosen_j, 2] = false;
  123.                 }
  124.                 //if we choose element 1 it's the cell to our right..
  125.  
  126.                 if ( chosen_unvisisted == 1 )
  127.                 {
  128.                     //etc
  129.                     chosen_i = current_i + 1;
  130.                     chosen_j = current_j;
  131.  
  132.                     grid [current_i, current_j, 1] = false;
  133.                     grid [chosen_i, chosen_j, 3] = false;
  134.                 }
  135.  
  136.                 if ( chosen_unvisisted == 2 )
  137.                 {
  138.                     chosen_i = current_i;
  139.                     chosen_j = current_j + 1;
  140.  
  141.                     grid [current_i, current_j, 2] = false;
  142.                     grid [chosen_i, chosen_j, 0] = false;
  143.                 }
  144.  
  145.                 if ( chosen_unvisisted == 3 )
  146.                 {
  147.                     chosen_i = current_i - 1;
  148.                     chosen_j = current_j;
  149.  
  150.                     grid [current_i, current_j, 3] = false;
  151.                     grid [chosen_i, chosen_j, 1] = false;
  152.                 }
  153.  
  154.  
  155.                 //now make the chosen cell the current cell and mark it as visited
  156.                 current_i = chosen_i;
  157.                 current_j = chosen_j;
  158.                 grid [current_i, current_j, 4] = true;
  159.             }
  160.  
  161.  
  162.  
  163.         }
  164.  
  165.         //draw it
  166.         for ( int i = 0; i < columns; i++ )
  167.         {
  168.             for ( int j = 0; j < rows; j++ )
  169.             {
  170.                 if ( grid [i, j, 0] == true )
  171.                 {
  172.                     //-j because we want to instantiate a grid 'left down' rather than 'left up'
  173.                     //this is why '// -current_j because reasons' is because reasons...
  174.                     //:^)
  175.  
  176.                     Instantiate ( topwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
  177.                 }
  178.                 if ( grid [i, j, 1] == true )
  179.                 {
  180.                     Instantiate ( rightwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
  181.                 }
  182.                 if ( grid [i, j, 2] == true )
  183.                 {
  184.                     Instantiate ( botwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
  185.                 }
  186.                 if ( grid [i, j, 3] == true )
  187.                 {
  188.                     Instantiate ( leftwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
  189.                 }
  190.                 Instantiate ( floor, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
  191.                
  192.             }
  193.         }
  194.  
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement