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 Lines : MonoBehaviour {
- public int canvasWidth;
- public float cellWidth;
- public int canvasHight;
- public float cellHight;
- int cols;
- int rows;
- Cell[,]grids;
- void Awake(){
- cols = (int) (canvasWidth / cellWidth);
- rows = (int) (canvasHight / cellHight);
- grids= new Cell[rows,cols];
- //DrawCanvas ();
- }
- void Start () {
- fillCells ();
- DrawCells ();
- int randRows = Random.Range (0, rows);
- int randCols = Random.Range (0, cols);
- Traverse (ref grids [randRows,randCols]);
- }
- void Update(){
- Clear ();
- DrawCells ();
- }
- void DrawCells(){
- Color CellColor = Color.white;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- DrawTopWall (i,j,CellColor);
- DrawBottomWall (i, j,CellColor);
- DrawLeftWall (i, j,CellColor);
- DrawRightWall (i, j,CellColor);
- }
- }
- }
- void DrawTopWall(int i , int j , Color CellColor) {
- if (grids [i, j].GetTop ()) {
- Vector3 start = new Vector3 ((canvasWidth / 2) - (j * cellWidth), (canvasHight / 2) - (i * cellHight), 0.0f);
- Vector3 end = new Vector3 (start.x-cellWidth, start.y, 0.0f);
- DrawLine (start, end, CellColor);
- }
- }
- void DrawBottomWall (int i, int j, Color CellColor){
- if (grids [i, j].GetBottom ()) {
- Vector3 start = new Vector3 ((canvasWidth / 2) - (j * cellWidth), (canvasHight / 2) - ((i+1) * cellHight), 0.0f);
- Vector3 end = new Vector3 (start.x-cellWidth, start.y, 0.0f);
- DrawLine (start, end, CellColor);
- }
- }
- void DrawLeftWall (int i , int j , Color CellColor){
- if (grids [i, j].GetLeft ()) {
- Vector3 start = new Vector3 ((canvasWidth / 2) - ((j+1) * cellWidth), (canvasHight / 2) - (i * cellHight), 0.0f);
- Vector3 end = new Vector3 (start.x, start.y-cellHight, 0.0f);
- DrawLine (start, end, CellColor);
- }
- }
- void DrawRightWall (int i , int j , Color CellColor){
- if (grids [i, j].GetRight ()) {
- Vector3 start = new Vector3 ((canvasWidth / 2) - (j * cellWidth), (canvasHight / 2) - (i * cellHight), 0.0f);
- Vector3 end = new Vector3 (start.x, start.y - cellHight, 0.0f);
- DrawLine (start, end, CellColor);
- }
- }
- void DrawCanvas(){
- DrawCanvasWidth ();
- DrawCanvasHight ();
- }
- void fillCells(){
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- Cell temp = new Cell(i,j);
- grids [i,j] = temp;
- }
- }
- }
- void DrawCanvasWidth (){
- Color myColor = Color.red;
- for (int i = -1; i < 2; i+=2) {
- Vector3 start = new Vector3 (i*canvasWidth/2, i*canvasHight/2, 0.0f);
- Vector3 end = new Vector3 (-1*i*canvasWidth/2, i*canvasHight/2, 0.0f);
- DrawLine (start,end,myColor);
- }
- }
- void DrawCanvasHight(){
- Color myColor = Color.red;
- for (int i = -1; i < 2; i+=2) {
- Vector3 start = new Vector3 (i*canvasWidth/2, i*canvasHight/2, 0.0f);
- Vector3 end = new Vector3 (i*canvasWidth/2, -i*canvasHight/2, 0.0f);
- DrawLine (start,end,myColor);
- }
- }
- public void DrawLine(Vector3 start, Vector3 end, Color color)
- {
- GameObject myLine = new GameObject();
- myLine.gameObject.tag = "line";
- myLine.transform.position = start;
- myLine.AddComponent<LineRenderer>();
- LineRenderer lr = myLine.GetComponent<LineRenderer>();
- lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
- lr.SetColors(color, color);
- lr.SetWidth(0.1f, 0.1f);
- lr.SetPosition(0, start);
- lr.SetPosition(1, end);
- }
- int[] RandomArray(){
- System.Random randnum = new System.Random ();
- int[] arr2 = new int[4];
- int i =0;
- int[] arr = { 1, 2, 3, 4 };
- while (true) {
- int x = randnum.Next (1, 5);
- for(int j = 0 ; j <4 ; j ++)
- {
- if(arr[j]==x){
- arr2 [i++] = arr [j];
- arr [j] = 0;
- }
- }
- if (i == 4)
- break;
- }
- return arr2;
- }
- void Traverse(ref Cell currentCell){
- currentCell.SetVisited (true);
- print ("i : " + currentCell.Get_i ());
- print ("j : " + currentCell.Get_j ());
- int []arr= RandomArray();
- for (int i = 0; i < 4; i++) {
- if (arr [i] == 1)
- TraverseTop (ref currentCell);
- else if (arr [i] == 2)
- TraverseRight (ref currentCell);
- else if (arr [i] == 3)
- TraverseBottom (ref currentCell);
- else if (arr [i] == 4)
- TraverseLeft (ref currentCell);
- }
- }
- void TraverseTop(ref Cell currentCell){
- if (currentCell.Get_i()> 0) {
- Cell nextCell = grids [currentCell.Get_i()- 1, currentCell.Get_j()];
- if (!nextCell.GetVisited()) {
- currentCell.SetTop (false);
- nextCell.SetBottom(false);
- Traverse (ref grids [currentCell.Get_i()- 1, currentCell.Get_j()]);
- }
- }
- }
- void TraverseBottom(ref Cell currentCell){
- if (currentCell.Get_i() < rows-1 ) {
- Cell nextCell = grids [currentCell.Get_i()+1 , currentCell.Get_j()];
- if (!nextCell.GetVisited()) {
- currentCell.SetBottom (false);
- nextCell.SetTop(false);
- Traverse (ref grids [currentCell.Get_i()+1 , currentCell.Get_j()]);
- }
- }
- }
- void TraverseRight(ref Cell currentCell){
- if (currentCell.Get_j () > 0 ) {
- Cell nextCell = grids [currentCell.Get_i() , currentCell.Get_j()-1];
- if (!nextCell.GetVisited()) {
- currentCell.SetRight (false);
- nextCell.SetLeft(false);
- Traverse (ref grids [currentCell.Get_i() , currentCell.Get_j()-1]);
- }
- }
- }
- void TraverseLeft(ref Cell currentCell){
- if (currentCell.Get_j () < cols-1 ) {
- Cell nextCell = grids [currentCell.Get_i() , currentCell.Get_j()+1];
- if (!nextCell.GetVisited()) {
- currentCell.SetLeft (false);
- nextCell.SetRight(false);
- Traverse (ref grids [currentCell.Get_i() , currentCell.Get_j()+1]);
- }
- }
- }
- void Clear ()
- {
- GameObject[] lines = GameObject.FindGameObjectsWithTag ("line");
- foreach (GameObject line in lines) {
- Destroy (line);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement