Advertisement
Guest User

Untitled

a guest
Nov 29th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Lines : MonoBehaviour {
  6.  
  7. public int canvasWidth;
  8. public float cellWidth;
  9. public int canvasHight;
  10. public float cellHight;
  11. int cols;
  12. int rows;
  13. Cell[,]grids;
  14. void Awake(){
  15. cols = (int) (canvasWidth / cellWidth);
  16. rows = (int) (canvasHight / cellHight);
  17. grids= new Cell[rows,cols];
  18. //DrawCanvas ();
  19. }
  20. void Start () {
  21. fillCells ();
  22. DrawCells ();
  23. int randRows = Random.Range (0, rows);
  24. int randCols = Random.Range (0, cols);
  25. Traverse (ref grids [randRows,randCols]);
  26. }
  27. void Update(){
  28. Clear ();
  29. DrawCells ();
  30. }
  31. void DrawCells(){
  32. Color CellColor = Color.white;
  33. for (int i = 0; i < rows; i++) {
  34. for (int j = 0; j < cols; j++) {
  35. DrawTopWall (i,j,CellColor);
  36. DrawBottomWall (i, j,CellColor);
  37. DrawLeftWall (i, j,CellColor);
  38. DrawRightWall (i, j,CellColor);
  39. }
  40. }
  41. }
  42. void DrawTopWall(int i , int j , Color CellColor) {
  43. if (grids [i, j].GetTop ()) {
  44. Vector3 start = new Vector3 ((canvasWidth / 2) - (j * cellWidth), (canvasHight / 2) - (i * cellHight), 0.0f);
  45. Vector3 end = new Vector3 (start.x-cellWidth, start.y, 0.0f);
  46. DrawLine (start, end, CellColor);
  47. }
  48. }
  49. void DrawBottomWall (int i, int j, Color CellColor){
  50. if (grids [i, j].GetBottom ()) {
  51. Vector3 start = new Vector3 ((canvasWidth / 2) - (j * cellWidth), (canvasHight / 2) - ((i+1) * cellHight), 0.0f);
  52. Vector3 end = new Vector3 (start.x-cellWidth, start.y, 0.0f);
  53. DrawLine (start, end, CellColor);
  54. }
  55. }
  56. void DrawLeftWall (int i , int j , Color CellColor){
  57. if (grids [i, j].GetLeft ()) {
  58. Vector3 start = new Vector3 ((canvasWidth / 2) - ((j+1) * cellWidth), (canvasHight / 2) - (i * cellHight), 0.0f);
  59. Vector3 end = new Vector3 (start.x, start.y-cellHight, 0.0f);
  60. DrawLine (start, end, CellColor);
  61. }
  62. }
  63. void DrawRightWall (int i , int j , Color CellColor){
  64. if (grids [i, j].GetRight ()) {
  65. Vector3 start = new Vector3 ((canvasWidth / 2) - (j * cellWidth), (canvasHight / 2) - (i * cellHight), 0.0f);
  66. Vector3 end = new Vector3 (start.x, start.y - cellHight, 0.0f);
  67. DrawLine (start, end, CellColor);
  68. }
  69. }
  70. void DrawCanvas(){
  71. DrawCanvasWidth ();
  72. DrawCanvasHight ();
  73. }
  74. void fillCells(){
  75. for (int i = 0; i < rows; i++) {
  76. for (int j = 0; j < cols; j++) {
  77. Cell temp = new Cell(i,j);
  78. grids [i,j] = temp;
  79. }
  80. }
  81.  
  82. }
  83. void DrawCanvasWidth (){
  84. Color myColor = Color.red;
  85. for (int i = -1; i < 2; i+=2) {
  86. Vector3 start = new Vector3 (i*canvasWidth/2, i*canvasHight/2, 0.0f);
  87. Vector3 end = new Vector3 (-1*i*canvasWidth/2, i*canvasHight/2, 0.0f);
  88. DrawLine (start,end,myColor);
  89. }
  90. }
  91. void DrawCanvasHight(){
  92. Color myColor = Color.red;
  93. for (int i = -1; i < 2; i+=2) {
  94. Vector3 start = new Vector3 (i*canvasWidth/2, i*canvasHight/2, 0.0f);
  95. Vector3 end = new Vector3 (i*canvasWidth/2, -i*canvasHight/2, 0.0f);
  96. DrawLine (start,end,myColor);
  97. }
  98. }
  99. public void DrawLine(Vector3 start, Vector3 end, Color color)
  100. {
  101. GameObject myLine = new GameObject();
  102. myLine.gameObject.tag = "line";
  103. myLine.transform.position = start;
  104. myLine.AddComponent<LineRenderer>();
  105. LineRenderer lr = myLine.GetComponent<LineRenderer>();
  106. lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
  107. lr.SetColors(color, color);
  108. lr.SetWidth(0.1f, 0.1f);
  109. lr.SetPosition(0, start);
  110. lr.SetPosition(1, end);
  111. }
  112. int[] RandomArray(){
  113. System.Random randnum = new System.Random ();
  114. int[] arr2 = new int[4];
  115. int i =0;
  116. int[] arr = { 1, 2, 3, 4 };
  117. while (true) {
  118. int x = randnum.Next (1, 5);
  119. for(int j = 0 ; j <4 ; j ++)
  120. {
  121. if(arr[j]==x){
  122. arr2 [i++] = arr [j];
  123. arr [j] = 0;
  124. }
  125. }
  126. if (i == 4)
  127. break;
  128. }
  129. return arr2;
  130. }
  131. void Traverse(ref Cell currentCell){
  132. currentCell.SetVisited (true);
  133. print ("i : " + currentCell.Get_i ());
  134. print ("j : " + currentCell.Get_j ());
  135. int []arr= RandomArray();
  136. for (int i = 0; i < 4; i++) {
  137. if (arr [i] == 1)
  138. TraverseTop (ref currentCell);
  139. else if (arr [i] == 2)
  140. TraverseRight (ref currentCell);
  141. else if (arr [i] == 3)
  142. TraverseBottom (ref currentCell);
  143. else if (arr [i] == 4)
  144. TraverseLeft (ref currentCell);
  145. }
  146. }
  147. void TraverseTop(ref Cell currentCell){
  148. if (currentCell.Get_i()> 0) {
  149. Cell nextCell = grids [currentCell.Get_i()- 1, currentCell.Get_j()];
  150. if (!nextCell.GetVisited()) {
  151. currentCell.SetTop (false);
  152. nextCell.SetBottom(false);
  153. Traverse (ref grids [currentCell.Get_i()- 1, currentCell.Get_j()]);
  154. }
  155. }
  156. }
  157. void TraverseBottom(ref Cell currentCell){
  158. if (currentCell.Get_i() < rows-1 ) {
  159. Cell nextCell = grids [currentCell.Get_i()+1 , currentCell.Get_j()];
  160. if (!nextCell.GetVisited()) {
  161. currentCell.SetBottom (false);
  162. nextCell.SetTop(false);
  163. Traverse (ref grids [currentCell.Get_i()+1 , currentCell.Get_j()]);
  164. }
  165. }
  166. }
  167. void TraverseRight(ref Cell currentCell){
  168. if (currentCell.Get_j () > 0 ) {
  169. Cell nextCell = grids [currentCell.Get_i() , currentCell.Get_j()-1];
  170. if (!nextCell.GetVisited()) {
  171. currentCell.SetRight (false);
  172. nextCell.SetLeft(false);
  173. Traverse (ref grids [currentCell.Get_i() , currentCell.Get_j()-1]);
  174. }
  175. }
  176. }
  177. void TraverseLeft(ref Cell currentCell){
  178. if (currentCell.Get_j () < cols-1 ) {
  179. Cell nextCell = grids [currentCell.Get_i() , currentCell.Get_j()+1];
  180. if (!nextCell.GetVisited()) {
  181. currentCell.SetLeft (false);
  182. nextCell.SetRight(false);
  183. Traverse (ref grids [currentCell.Get_i() , currentCell.Get_j()+1]);
  184. }
  185. }
  186. }
  187. void Clear ()
  188. {
  189. GameObject[] lines = GameObject.FindGameObjectsWithTag ("line");
  190. foreach (GameObject line in lines) {
  191. Destroy (line);
  192. }
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement