Advertisement
teleias

GridFinal.cs

Apr 14th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Grid : MonoBehaviour {
  6.    
  7.     //* -> Assigned in Inspector
  8.     public Transform CellPrefab;//*
  9.     public Vector3 GridSize;//*
  10.     public float Buffer;//*
  11.    
  12.     public List<Transform> Set = new List<Transform>();
  13.     public List<Transform> CompletedSet = new List<Transform>();
  14.    
  15.     void Start ()
  16.     {
  17.         //CreateGrid ();
  18.     }
  19.     Transform[,]GridArr;
  20.     public void CreateGrid ()
  21.     {
  22.         int x = (int)GridSize.x;
  23.         int z = (int)GridSize.z;
  24.         int maxXZ = Mathf.Max (x, z);
  25.         Camera.mainCamera.transform.position = new Vector3 (maxXZ / 2f, maxXZ, maxXZ / 8f);
  26.         GridArr = new Transform[x, z];
  27.         Transform newCell;
  28.         for (int ix = 0; ix < x; ix++) {
  29.             for (int iz = 0; iz < z; iz++) {
  30.                 newCell = (Transform)Instantiate (CellPrefab, new Vector3 (ix, 0, iz) * Buffer, Quaternion.identity);
  31.                 newCell.name = string.Format ("({0},0,{1})", ix, iz);
  32.                 newCell.parent = transform;
  33.                 newCell.GetComponent<Cell> ().Position = new Vector3 (ix, 0, iz);
  34.                 GridArr [ix, iz] = newCell;
  35.             }
  36.         }
  37.     }
  38.     void SetStart (int x, int z)
  39.     {
  40.         AddToSet(GridArr[x,z]);
  41.     }
  42.     // n is 'Next'
  43.     void AddToSet (Transform n)
  44.     {
  45.         Set.Insert (0, n);//Recursive
  46.         //Set.Insert (Random.Range (0, Set.Count), n);//Prims
  47.         Cell nScript = n.GetComponent<Cell> ();
  48.         nScript.IsOpened = true;
  49.         n.renderer.material.color = Color.grey;
  50.     }
  51.    
  52.     public List<Vector3> Directions; //*
  53.     void FindNext ()
  54.     {
  55.        
  56.         if (Set.Count == 0) {
  57.             Debug.Log ("We're done! Took " + Time.timeSinceLevelLoad + " seconds for a " + GridSize.x + " by " + GridSize.z + " grid.");
  58.             CancelInvoke("FindNext");
  59.             return;
  60.         }
  61.        
  62.         Transform previous = Set [0];
  63.         Cell pScript = Set [0].GetComponent<Cell> ();
  64.        
  65.         Transform next;
  66.         Cell nScript;
  67.        
  68.         int prevX = (int)pScript.Position.x;
  69.         int prevZ = (int)pScript.Position.z;
  70.        
  71.         int randSeed = Random.Range (0, 4);
  72.         int counter = 0;
  73.        
  74.         int nextX;
  75.         int nextZ;
  76.        
  77.        
  78.         do {
  79.             do {
  80.                 Vector3 randDirection = Directions [randSeed];
  81.                 nextX = prevX + (int)randDirection.x;
  82.                 nextZ = prevZ + (int)randDirection.z;
  83.                 randSeed = (randSeed + 1) % 4;
  84.                 counter++;
  85.                 if (counter > 4) {
  86.                     AddToCompletedSet (previous);
  87.                     return;
  88.                 }
  89.             } while(nextX < 0 || nextZ < 0 || nextX >= GridSize.x || nextZ >= GridSize.z);
  90.             next = GridArr [nextX, nextZ];
  91.             nScript = next.GetComponent <Cell> ();
  92.         } while(nScript.IsOpened);
  93.        
  94.         DrawDebugLines (10, previous, next);
  95.        
  96.         ClearWalls (previous, next);
  97.        
  98.         AddToSet (next);
  99.        
  100.     }
  101.    
  102.     void AddToCompletedSet (Transform  toAdd)
  103.     {
  104.         Set.Remove (toAdd);
  105.         CompletedSet.Add (toAdd);
  106.         toAdd.renderer.material.color = Color.black;
  107.        
  108.     }
  109.    
  110.     // p is 'Previous'
  111.     // n is 'Next'
  112.     void DrawDebugLines (int lines, Transform p, Transform n)
  113.     {
  114.         for (int i = 0; i < lines; i++) {
  115.             Debug.DrawLine (p.position, n.position, Color.red, 100f);
  116.         }
  117.     }
  118.    
  119.     // p is 'Previous'
  120.     // n is 'Next'
  121.     void ClearWalls(Transform p, Transform n)
  122.     {
  123.         RaycastHit[] hitInfo;
  124.         hitInfo = Physics.RaycastAll (p.position + Vector3.up, n.position - p.position, Buffer);
  125.         foreach (RaycastHit hit in hitInfo) {
  126.             Destroy (hit.transform.gameObject);
  127.         }
  128.     }
  129.     void Update ()
  130.     {
  131.         if (Input.GetKeyDown (KeyCode.Tab) || Input.GetKey (KeyCode.Space)) {
  132.             InvokeRepeating("FindNext", 0, 0.001f);//FindNext ();
  133.         }
  134.         if (Input.GetKeyDown (KeyCode.LeftShift)) {
  135.             FindNext();
  136.         }
  137.         if(Input.GetKeyDown(KeyCode.F1))
  138.         {
  139.             Application.LoadLevel(0);
  140.         }
  141.     }
  142.     bool start = true;
  143.     void OnGUI()
  144.     {
  145.         GUILayout.Label("[Space] to create maze, \n[Shift] to step through maze, \n[F1] to restart");
  146.         if(!start)
  147.         {
  148.             return;
  149.         }
  150.         GUILayout.BeginArea(new Rect(Screen.width/2f-100, Screen.height/2f, 200, 500));
  151.         if(GUILayout.Button("10 x 3", GUILayout.Width(100), GUILayout.Height(30)))
  152.         {
  153.             GridSize = new Vector3(3,0,10);
  154.             start = false;
  155.             CreateGrid();
  156.             SetStart (0, 0);
  157.         }
  158.         if(GUILayout.Button(" 9 x 4", GUILayout.Width(100), GUILayout.Height(30)))
  159.         {
  160.             GridSize = new Vector3(4,0,9);
  161.             start = false;
  162.             CreateGrid();
  163.             SetStart (0, 0);
  164.         }
  165.         if(GUILayout.Button("10 x 10", GUILayout.Width(100), GUILayout.Height(30)))
  166.         {
  167.             GridSize = new Vector3(10,0,10);
  168.             start = false;
  169.             CreateGrid();
  170.             SetStart (0, 0);
  171.         }
  172.         if(GUILayout.Button("25 x 25", GUILayout.Width(100), GUILayout.Height(30)))
  173.         {
  174.             GridSize = new Vector3(25,0,25);
  175.             start = false;
  176.             CreateGrid();
  177.             SetStart (0, 0);
  178.         }
  179.         GUILayout.EndArea();
  180.     }
  181.    
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement