using System.Collections.Generic; using System.Linq; using Unity.VisualScripting.FullSerializer; using UnityEngine; using GameObject = UnityEngine.GameObject; public class LevelManager : MonoBehaviour { public int width = 20; public int height = 20; public int maxRooms = 50; // Maximum number of rooms to generate public GameObject floorPrefab; public GameObject wallPrefab; private int[,] grid; private System.Random rng = new System.Random(); private int roomCount = 0; // Counter for the number of rooms created private List rooms = new List(); void Start() { grid = new int[width, height]; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { grid[i, j] = 0; } } grid[(int)(width / 2), (int)(height / 2)] = 1; rooms.Add(new Vector2((int)(width / 2), (int)(height / 2))); GenerateDungeon(maxRooms); RenderDungeon(); } void GenerateDungeon(int maxRooms) { int roomId = 0; int iteration = 0; while (rooms.Count < maxRooms && iteration <= 1000) { iteration++; for (int r = 0; r < 4; r++) { int posX = (int)rooms[roomId].x; int posY = (int)rooms[roomId].y; switch (r) { case 0: if (posX + 1 < grid.GetLength(0)) RoomCheck(posX + 1, posY); break; case 1: if (posX - 1 >= 0) RoomCheck(posX - 1, posY); break; case 2: if (posY + 1 < grid.GetLength(1)) RoomCheck(posX, posY + 1); break; case 3: if (posY - 1 >= 0) RoomCheck(posX, posY - 1); break; } if (rooms.Count >= maxRooms) { break; } } roomId = (roomId + 1) % rooms.Count; } if (iteration > 1000) { Debug.LogError("Maximum iterations reached."); } } void RoomCheck(int posX, int posY) { if (grid[posX, posY] == 1 || Random.Range(0, 2) == 1) { return; } int occupiedRoomCount = 0; if (posX + 1 < grid.GetLength(0)) occupiedRoomCount += grid[posX + 1, posY]; if (posX - 1 >= 0) occupiedRoomCount += grid[posX - 1, posY]; if (posY + 1 < grid.GetLength(1)) occupiedRoomCount += grid[posX, posY + 1]; if (posY - 1 >= 0) occupiedRoomCount += grid[posX, posY - 1]; if (occupiedRoomCount > 1) { return; } grid[posX, posY] = 1; rooms.Add(new Vector2(posX, posY)); } void RenderDungeon() { string dungeon = ""; for (int x = 0; x < width; x++) { string dungeon1 = ""; for (int y = 0; y < height; y++) { dungeon1 += grid[x, y]; } dungeon += dungeon1 + "\n"; } print(dungeon); } }