using System.Collections; using System.Collections.Generic; using System.Reflection; using UnityEngine; using UnityEngine.UIElements; public class RoomCreator : MonoBehaviour, IRoomCreationHandler { [Header("Prefabs")] public GameObject floorPrefab; [SerializeField] private GameObject wallPrefab; [SerializeField] private GameObject exitPrefabH; [SerializeField] private GameObject exitPrefabV; [SerializeField] private GameObject testPrefab; [Header("Room templates spawn")] public int numberOfTemplatesInX; public int numberOfTemplatesInY; private int templatesInX; private int templatesInY; [SerializeField] private List<GameObject> roomTemplates; //Just for organization inside the gameobject no function to it. GameObject floor; GameObject wallL; GameObject wallR; GameObject roof; int index = 0; ExitScript[] exits = new ExitScript[4]; Vector3 bottomLeftCorner; Vector3 topRightCorner; List<List<GameObject>> roomList = new List<List<GameObject>>(); private ProgressController progressController; private RoomInformation roomInfo; public RoomInformation InstantiateRoom(Vector2 coords) { progressController = GetComponent<ProgressController>(); templatesInX = numberOfTemplatesInX-1; templatesInY = numberOfTemplatesInY-1; floor = new GameObject("floor"); wallL = new GameObject("wallL"); wallR = new GameObject("wallR"); roof = new GameObject("roof"); floor.transform.parent = transform; wallL.transform.parent = transform; wallR.transform.parent = transform; roof.transform.parent = transform; NewFloor(0, floor); NewFloor(wallPrefab.transform.localScale.y * (templatesInY) + exitPrefabV.transform.localScale.y + floorPrefab.transform.localScale.y, roof); NewWall(-floorPrefab.transform.localScale.x / 2 + wallPrefab.transform.localScale.x / 2, wallL); NewWall(floorPrefab.transform.localScale.x * (templatesInX - 1) + exitPrefabH.transform.localScale.x + floorPrefab.transform.localScale.x / 2 - wallPrefab.transform.localScale.x / 2, wallR); FillTemplates(); roomInfo = new RoomInformation(exits , new Vector2(floorPrefab.transform.localScale.x*(numberOfTemplatesInX-1) + exitPrefabH.transform.localScale.x, wallPrefab.transform.localScale.y * (numberOfTemplatesInY-1) + exitPrefabV.transform.localScale.y + (2*floorPrefab.transform.localScale.y)), coords); progressController.StartMe(); return roomInfo; } public RoomInformation GetRoomInfo() { return roomInfo; } void NewWall(float x, GameObject parent) { int exitfloor = Random.Range(0, templatesInX-1); GameObject current = Instantiate(wallPrefab, parent.transform); current.transform.position = transform.position + new Vector3(x, floorPrefab.transform.localScale.y / 2 + wallPrefab.transform.localScale.y / 2, 0); current.name = "Collider holder bottom"; BoxCollider bc = current.AddComponent<BoxCollider>(); bc.center = new Vector3(0, (float)exitfloor / 2, 0); bc.size = new Vector3(1, exitfloor + 1, 5); for (int y = 0; y < templatesInY; y++) { GameObject temp; if (y != exitfloor) { temp = Instantiate(wallPrefab, parent.transform); temp.transform.position = current.transform.position + new Vector3(0, current.transform.localScale.y / 2 + wallPrefab.transform.localScale.y / 2, 0); if (y == exitfloor + 1) { BoxCollider bc2 = temp.AddComponent<BoxCollider>(); temp.name = "Collider holder top"; bc2.center = new Vector3(0, (float)(templatesInY - exitfloor - 2) / 2, 0); bc2.size = new Vector3(1, (templatesInY - exitfloor) - 1, 5); } } else { temp = Instantiate(exitPrefabV, parent.transform); temp.transform.position = current.transform.position + new Vector3(0, current.transform.localScale.y / 2 + exitPrefabV.transform.localScale.y / 2, 0); exits[index] = temp.GetComponent<ExitScript>(); exits[index].pC = progressController; index++; } current = temp; } } void NewFloor(float y, GameObject parent) { GameObject current = Instantiate(floorPrefab, parent.transform); current.name = "Collider holder left"; current.transform.position = transform.position + new Vector3(0, y, 0); int exitfloor = Random.Range(0, templatesInX-1); Vector3 startPos = current.transform.position; float dis = floorPrefab.transform.localScale.x * (exitfloor + 1); BoxCollider bc = current.AddComponent<BoxCollider>(); bc.center = new Vector3((float)exitfloor / 2, 0, 0); bc.size = new Vector3(exitfloor + 1, 1, 5); if (parent.name == "floor") { bottomLeftCorner = current.transform.position - new Vector3(current.transform.localScale.x / 2, current.transform.localScale.y / 2, 0); } for (int x = 0; x < templatesInX; x++) { GameObject temp; if (x != exitfloor) { temp = Instantiate(floorPrefab, parent.transform); temp.transform.position = current.transform.position + new Vector3(current.transform.localScale.x / 2 + floorPrefab.transform.localScale.x / 2, 0, 0); if (x == exitfloor + 1) { BoxCollider bc2 = temp.AddComponent<BoxCollider>(); temp.name = "Collider holder right"; bc2.center = new Vector3((float)(templatesInX - exitfloor - 2) / 2, 0, 0); bc2.size = new Vector3((templatesInX - exitfloor) - 1, 1, 5); } } else { temp = Instantiate(exitPrefabH, parent.transform); temp.transform.position = current.transform.position + new Vector3(current.transform.localScale.x / 2 + exitPrefabH.transform.localScale.x / 2, 0, 0); exits[index] = temp.GetComponent<ExitScript>(); exits[index].pC= progressController; index++; } current = temp; } if (parent.name == "roof") { topRightCorner = current.transform.position + new Vector3(current.transform.localScale.x / 2, current.transform.localScale.y / 2, 0); } } void FillTemplates() { int yRooms = numberOfTemplatesInY; int xRooms = numberOfTemplatesInX; float length = floorPrefab.transform.localScale.x*numberOfTemplatesInX - (2 * wallPrefab.transform.localScale.x); float height = (yRooms * wallPrefab.transform.localScale.y); Vector3 pointZero = new Vector3(bottomLeftCorner.x + floorPrefab.transform.localScale.x/2, bottomLeftCorner.y + floorPrefab.transform.localScale.y + wallPrefab.transform.localScale.y / 2, bottomLeftCorner.z); float centerX = (length / xRooms); float centerY = (height / yRooms); for (int i = 0; i < yRooms; i++) { roomList.Add(new List<GameObject>()); for (int j = 0; j < xRooms; j++) { roomList[i].Add(Instantiate(roomTemplates[Random.Range(0, roomTemplates.Count)], transform)); roomList[i][j].transform.position = pointZero + new Vector3(j * centerX, i * centerY, 0); } } } }