Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 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[rows,columns,5];
  26.  
  27. //Instantiate a 3D bool array to keep track of the algorithms state
  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. grid [i, j, w] = true;
  35. }
  36. }
  37. }
  38.  
  39. // [i,x,x] tracks column number while [x,j,x] tracks row number
  40.  
  41. // [x,x,0] = top wall exists // [x,x,1] = right wall exists // [x,x,2] = bot wall exists// [x,x,3] = left wall exists
  42.  
  43. // [x,x,4] = cell has been visisted //
  44.  
  45.  
  46. //to start, pick a random cell mark it as the current cell and mark it as visisted
  47. grid [current_i = Random.Range ( 0, columns ),current_j = Random.Range ( 0, rows ), 4] = true;
  48.  
  49. //this, because i'm sick of moving my camera to where the maze starts each time
  50. cameraStart.transform.position = new Vector3 ( current_i * spacing, -current_j * spacing, -15 );
  51.  
  52. for ( int TODO = 0; TODO < 30; TODO++ ) // : After i get the algorithm working, change this to a " WHILE (THERE ARE UNVISITED CELLS) " condition
  53. {
  54. //check for unvisisted cells adjacent to the current cell
  55. //if they exist, add them to a list called unvisisted neighbours
  56.  
  57. List<int> unvisistedNeighbours = new List<int>();
  58.  
  59. //if there is no cell above us, don't check there (ie. if we are on the top row)
  60. if ( current_j !=0)
  61. {
  62. //otherwise, check to see if the cell above us was visited yet
  63. if ( grid [current_i, current_j - 1, 4] == false )
  64. {
  65. unvisistedNeighbours.Add ( 0 );
  66. }
  67. }
  68. //if there's no cell to our right, don't try to check it
  69. if ( current_i != columns - 1 )
  70. {
  71. //if there is, etc.
  72. if ( grid [current_i + 1, current_j, 4] == false )
  73. {
  74. unvisistedNeighbours.Add ( 1 );
  75. }
  76. }
  77.  
  78. if ( current_j != rows - 1 )
  79. {
  80. if ( grid [current_i, current_j + 1, 4] == false )
  81. {
  82. unvisistedNeighbours.Add ( 2 );
  83. }
  84. }
  85.  
  86. if ( current_i != 0 )
  87. {
  88. if ( grid [current_i - 1, current_j, 4] == false )
  89. {
  90. unvisistedNeighbours.Add ( 3 );
  91. }
  92. }
  93.  
  94. //if there was any unvisisted neighbours
  95. if ( unvisistedNeighbours.Count > 0 )
  96. {
  97.  
  98. //then randomly choose one element from the unvisisted neighbours list
  99. int chosen_unvisisted = Random.Range ( 0, unvisistedNeighbours.Count );
  100.  
  101. //if we choose the 0th element of that list, it's the cell above us
  102. if ( chosen_unvisisted == 0 )
  103. {
  104. //grab its i&j and remember them
  105. chosen_i = current_i;
  106. chosen_j = current_j - 1;
  107. //and remove the walls between the chosen cell and the current cell
  108. grid [current_i, current_j, 0] = false;
  109. grid [chosen_i, chosen_j, 2] = false;
  110. }
  111. //if we choose element 1 it's the cell to our right..
  112. if ( chosen_unvisisted == 1 )
  113. {
  114. //etc
  115. chosen_i = current_i + 1;
  116. chosen_j = current_j;
  117. grid [current_i, current_j, 1] = false;
  118. grid [chosen_i, chosen_j, 3] = false;
  119. }
  120. if ( chosen_unvisisted == 2 )
  121. {
  122. chosen_i = current_i;
  123. chosen_j = current_j + 1;
  124. grid [current_i, current_j, 2] = false;
  125. grid [chosen_i, chosen_j, 0] = false;
  126. }
  127. if ( chosen_unvisisted == 3 )
  128. {
  129. chosen_i = current_i - 1;
  130. chosen_j = current_j;
  131. grid [current_i, current_j, 3] = false;
  132. grid [chosen_i, chosen_j, 1] = false;
  133. }
  134.  
  135.  
  136. //now make the chosen cell the current cell and mark it as visisted
  137. current_i = chosen_i;
  138. current_j = chosen_j;
  139. grid [current_i, current_j, 4] = true;
  140. }
  141.  
  142.  
  143.  
  144. }
  145.  
  146. //draw it
  147. for ( int i = 0; i < columns; i++ )
  148. {
  149. for ( int j = 0; j < rows; j++ )
  150. {
  151. if ( grid [i, j, 0] == true )
  152. {
  153. Instantiate ( topwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
  154. }
  155. if ( grid [i, j, 1] == true )
  156. {
  157. Instantiate ( rightwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
  158. }
  159. if ( grid [i, j, 2] == true )
  160. {
  161. Instantiate ( botwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
  162. }
  163. if ( grid [i, j, 3] == true )
  164. {
  165. Instantiate ( leftwall, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
  166. }
  167. Instantiate ( floor, new Vector3 ( i * spacing, -j * spacing, 0 ), Quaternion.identity );
  168.  
  169. }
  170. }
  171.  
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement