Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Grid : MonoBehaviour {
- //* -> Assigned in Inspector
- public Transform CellPrefab;//*
- public Vector3 GridSize;//*
- public float Buffer;//*
- public List<Transform> Set = new List<Transform>();
- public List<Transform> CompletedSet = new List<Transform>();
- void Start ()
- {
- //CreateGrid ();
- }
- Transform[,]GridArr;
- public void CreateGrid ()
- {
- int x = (int)GridSize.x;
- int z = (int)GridSize.z;
- int maxXZ = Mathf.Max (x, z);
- Camera.mainCamera.transform.position = new Vector3 (maxXZ / 2f, maxXZ, maxXZ / 8f);
- GridArr = new Transform[x, z];
- Transform newCell;
- for (int ix = 0; ix < x; ix++) {
- for (int iz = 0; iz < z; iz++) {
- newCell = (Transform)Instantiate (CellPrefab, new Vector3 (ix, 0, iz) * Buffer, Quaternion.identity);
- newCell.name = string.Format ("({0},0,{1})", ix, iz);
- newCell.parent = transform;
- newCell.GetComponent<Cell> ().Position = new Vector3 (ix, 0, iz);
- GridArr [ix, iz] = newCell;
- }
- }
- }
- void SetStart (int x, int z)
- {
- AddToSet(GridArr[x,z]);
- }
- // n is 'Next'
- void AddToSet (Transform n)
- {
- Set.Insert (0, n);//Recursive
- //Set.Insert (Random.Range (0, Set.Count), n);//Prims
- Cell nScript = n.GetComponent<Cell> ();
- nScript.IsOpened = true;
- n.renderer.material.color = Color.grey;
- }
- public List<Vector3> Directions; //*
- void FindNext ()
- {
- if (Set.Count == 0) {
- Debug.Log ("We're done! Took " + Time.timeSinceLevelLoad + " seconds for a " + GridSize.x + " by " + GridSize.z + " grid.");
- CancelInvoke("FindNext");
- return;
- }
- Transform previous = Set [0];
- Cell pScript = Set [0].GetComponent<Cell> ();
- Transform next;
- Cell nScript;
- int prevX = (int)pScript.Position.x;
- int prevZ = (int)pScript.Position.z;
- int randSeed = Random.Range (0, 4);
- int counter = 0;
- int nextX;
- int nextZ;
- do {
- do {
- Vector3 randDirection = Directions [randSeed];
- nextX = prevX + (int)randDirection.x;
- nextZ = prevZ + (int)randDirection.z;
- randSeed = (randSeed + 1) % 4;
- counter++;
- if (counter > 4) {
- AddToCompletedSet (previous);
- return;
- }
- } while(nextX < 0 || nextZ < 0 || nextX >= GridSize.x || nextZ >= GridSize.z);
- next = GridArr [nextX, nextZ];
- nScript = next.GetComponent <Cell> ();
- } while(nScript.IsOpened);
- DrawDebugLines (10, previous, next);
- ClearWalls (previous, next);
- AddToSet (next);
- }
- void AddToCompletedSet (Transform toAdd)
- {
- Set.Remove (toAdd);
- CompletedSet.Add (toAdd);
- toAdd.renderer.material.color = Color.black;
- }
- // p is 'Previous'
- // n is 'Next'
- void DrawDebugLines (int lines, Transform p, Transform n)
- {
- for (int i = 0; i < lines; i++) {
- Debug.DrawLine (p.position, n.position, Color.red, 100f);
- }
- }
- // p is 'Previous'
- // n is 'Next'
- void ClearWalls(Transform p, Transform n)
- {
- RaycastHit[] hitInfo;
- hitInfo = Physics.RaycastAll (p.position + Vector3.up, n.position - p.position, Buffer);
- foreach (RaycastHit hit in hitInfo) {
- Destroy (hit.transform.gameObject);
- }
- }
- void Update ()
- {
- if (Input.GetKeyDown (KeyCode.Tab) || Input.GetKey (KeyCode.Space)) {
- InvokeRepeating("FindNext", 0, 0.001f);//FindNext ();
- }
- if (Input.GetKeyDown (KeyCode.LeftShift)) {
- FindNext();
- }
- if(Input.GetKeyDown(KeyCode.F1))
- {
- Application.LoadLevel(0);
- }
- }
- bool start = true;
- void OnGUI()
- {
- GUILayout.Label("[Space] to create maze, \n[Shift] to step through maze, \n[F1] to restart");
- if(!start)
- {
- return;
- }
- GUILayout.BeginArea(new Rect(Screen.width/2f-100, Screen.height/2f, 200, 500));
- if(GUILayout.Button("10 x 3", GUILayout.Width(100), GUILayout.Height(30)))
- {
- GridSize = new Vector3(3,0,10);
- start = false;
- CreateGrid();
- SetStart (0, 0);
- }
- if(GUILayout.Button(" 9 x 4", GUILayout.Width(100), GUILayout.Height(30)))
- {
- GridSize = new Vector3(4,0,9);
- start = false;
- CreateGrid();
- SetStart (0, 0);
- }
- if(GUILayout.Button("10 x 10", GUILayout.Width(100), GUILayout.Height(30)))
- {
- GridSize = new Vector3(10,0,10);
- start = false;
- CreateGrid();
- SetStart (0, 0);
- }
- if(GUILayout.Button("25 x 25", GUILayout.Width(100), GUILayout.Height(30)))
- {
- GridSize = new Vector3(25,0,25);
- start = false;
- CreateGrid();
- SetStart (0, 0);
- }
- GUILayout.EndArea();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement