/// <summary> /// (Re)Instantiates Rooms and Wall objects /// </summary> public void InstantiateAll() { InstantiateParent(ref roomParent, "Rooms"); InstantiateParent(ref wallParent, "Walls"); GameObject[][,] wallGroups = WallGenerator.InstantiateWalls(wallParent.transform, wallPrefab, n, radius); GameObject[,] rooms = RoomGenerator.InstantiateRooms(roomParent.transform, roomPrefab, radius); for (int i = 0; i < wallGroups.Length; i++) { float angle = WallGenerator.CalculateRotation(i, n) * Mathf.Deg2Rad; for (int roomY = 0; roomY < rooms.GetLength(1); roomY++) { int offset = -Mathf.Max(radius - roomY - 1, 0); for (int roomX = Mathf.Max(radius - roomY - 1, 0); roomX < rooms.GetLength(0); roomX++) { GameObject go = rooms[roomX, roomY]; if (go != null) { RoomController room = go.GetComponent <RoomController>(); if (i == 0) { WallController w1 = wallGroups[i][roomX + offset, roomY].GetComponent <WallController>(); WallController w2 = wallGroups[i][roomX + offset + 1, roomY].GetComponent <WallController>(); AddWalls(room, w1, w2); } else { Vector2 roomPosition = new Vector2(room.transform.position.x, room.transform.position.z); Vector2 rotatedPosition = roomPosition.RotateAround(0f, 0f, angle); if (TryGetGameObjectAtPosition(rooms, new Vector3(rotatedPosition.x, 0f, rotatedPosition.y), out GameObject targetRoom)) { RoomController target = targetRoom.GetComponent <RoomController>(); WallController w1 = target.Walls[0], w2 = target.Walls[1]; w1 = wallGroups[i][w1.X, w1.Y].GetComponent <WallController>(); w2 = wallGroups[i][w2.X, w2.Y].GetComponent <WallController>(); AddWalls(room, w1, w2); } else { Debug.Log($"Couldn't find room at position {rotatedPosition}"); } } } } } }