using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; using UnityEngine.Rendering; using UnityEngine.SceneManagement; public class MazeGenerator : MonoBehaviour { public Material Wall; public Color Hedge; public GameObject Plane; public Material SmileyMat; private List OuterBorder; public Shader Shader; List> Grid; List> CellGrid; ObjectPoints PointsOnThePlane; int MazeWidth = 11; int MazeHeight = 11; readonly int planeHeight = 11; readonly int planeWidth = 11; GameObject BorderParent; RecursiveBacktracker RB; SortingLayer sortingL; int sortingLayer = 0; bool goaled; private void OnEnable() { SmileyTrigger.OnEnterSmiley += ReduceMaze; } private void Onisable() { SmileyTrigger.OnEnterSmiley -= ReduceMaze; } void Start() { goaled = false; OuterBorder = new List(); BorderParent = new GameObject("BorderParent"); PointsOnThePlane = this.GetComponent(); RB = new RecursiveBacktracker(); CreateNewMaze(); } private void Update() { if (goaled) { BorderParent.transform.Translate(Vector3.down * Time.deltaTime); if (BorderParent.transform.position.y < -2.3f) { Singleton.Instance.transform.position = Vector3.zero; SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } } } public void CreateNewMaze() { Destroy(BorderParent); BorderParent = new GameObject("BorderParent"); MazeWidth = planeWidth; MazeHeight = planeHeight; CreateTheGrid(); CellGrid = RB.GetNewMaze(Grid); ShowMaze(); } void CreateTheGrid() //creates grid with given width and length from the points on the plane { Grid = new List>(); List OneRow = new List(); for (int y = 0; y < MazeHeight; y++) { for (int x = 0; x < MazeWidth; x++) { OneRow.Add(PointsOnThePlane.GetObjectGlobalVertices()[y + x * planeHeight]); //getting the points on the plane from the upper left corner } Grid.Add(OneRow); OneRow = new List(); } } void ShowMaze() //Creates maz with borders as cubes { GameObject VerticalBorder = GameObject.CreatePrimitive(PrimitiveType.Cube); //vertically scaled cube for up and down borders VerticalBorder.transform.localScale = new Vector3(1.5f, 1, 0.5f); //VerticalBorder.GetComponent().material.SetColor("_Color", Hedge); VerticalBorder.GetComponent().material = Wall; GameObject HorizontalBorder = GameObject.CreatePrimitive(PrimitiveType.Cube);//horizontally scaled cube for left and right borders HorizontalBorder.transform.localScale = new Vector3(0.5f, 1, 1.5f); //HorizontalBorder.GetComponent().material.SetColor("_Color", Hedge); HorizontalBorder.GetComponent().material = Wall; for (int x = 0; x < MazeWidth; x++) { for (int y = 0; y < MazeHeight; y++) { if (CellGrid[y][x].Borders.Contains(Direction.Up)) { PlaceHorizontalBorder(HorizontalBorder, CellGrid[y][x], Direction.Up); } if (CellGrid[y][x].Borders.Contains(Direction.Down)) { PlaceHorizontalBorder(HorizontalBorder, CellGrid[y][x], Direction.Down); } if (CellGrid[y][x].Borders.Contains(Direction.Right)) { PlaceVerticalBorder(VerticalBorder, CellGrid[y][x], Direction.Right); } if (CellGrid[y][x].Borders.Contains(Direction.Left)) { PlaceVerticalBorder(VerticalBorder, CellGrid[y][x], Direction.Left); } } } GameObject.Destroy(VerticalBorder); //destroy source objects GameObject.Destroy(HorizontalBorder); //Rigidbody rb = BorderParent.AddComponent(); //rb.constraints = RigidbodyConstraints.FreezeAll; BorderParent.layer = 12; foreach (Transform child in BorderParent.transform) { child.gameObject.tag = "Hedge"; child.gameObject.isStatic = true; BoxCollider bc = child.GetComponent(); child.gameObject.layer = 12; //child.gameObject.GetComponent().enabled = false; if (child.position.x == -5.5f || child.position.x == 5.5f || child.position.z == -5.5f || child.position.z == 5.5f) { OuterBorder.Add(child.gameObject); } //if (child.position==VRCam.transform.position) //{ //BorderCube löschen, wenn Camera da am Start liegt! //} } // Cut exit GameObject exit = OuterBorder[Random.Range(0, OuterBorder.Count - 1)]; Vector3 pos = exit.transform.position; Destroy(exit); // Smileyinstantiation and position GameObject smiley = GameObject.CreatePrimitive(PrimitiveType.Sphere); smiley.name = "Smiley"; smiley.transform.position = pos * 2; smiley.AddComponent(); MeshRenderer mr = smiley.GetComponent(); SphereCollider sc = smiley.GetComponent(); Rigidbody rbSmile = smiley.AddComponent(); sc.isTrigger = true; smiley.tag = "Smiley"; mr.material = SmileyMat; rbSmile.useGravity = false; // Scale and height final Maze BorderParent.transform.localScale *= 2; Plane.transform.localScale *= 2; Plane.transform.Translate(Vector3.down); //mc.CombineMeshes(true); //MeshRenderer bpmr = BorderParent.AddComponent(); //bpmr.material = Wall; } void PlaceHorizontalBorder(GameObject border, Cell c, Direction d) //borders are put moved away from the point to suround it { //upper border moved up, and lower border moved down along x axis Instantiate(border, c.position + Vector3.right * 0.5f * ((d == Direction.Up) ? 1 : -1), Quaternion.identity, BorderParent.transform); } void PlaceVerticalBorder(GameObject border, Cell c, Direction d) { //left border moved left, and right border moved right along z axis Instantiate(border, c.position + Vector3.forward * 0.5f * ((d == Direction.Left) ? 1 : -1), Quaternion.identity, BorderParent.transform); } private void ReduceMaze() { goaled = true; } }