public void GenerateRooms() { roomGenerationComplete = false; debugMode = dungeonGenerator.debugMode; dungeonParent = new GameObject("Dungeon"); dungeonParent.tag = "DungeonParent"; int[] roomSizeDistribution = GetRoomSizeDistribution(); System.Random pseudoRNG = new System.Random(dungeonGenerator.seed); for (int i = 0; i < dungeonGenerator.numberOfRooms; i++) { int roomWidth = roomSizeDistribution[pseudoRNG.Next(0, roomSizeDistribution.Length)]; int roomLength = roomSizeDistribution[pseudoRNG.Next(0, roomSizeDistribution.Length)]; GameObject room = Instantiate(roomPrefab, DungeonUtilities.CalculatePointInCircle(dungeonGenerator.roomSpawnRadius, pseudoRNG), Quaternion.identity, dungeonParent.transform) as GameObject; if (debugMode) { room.gameObject.GetComponent <MeshRenderer>().enabled = true; room.GetComponent <Renderer>().material.mainTextureScale = new Vector2(roomWidth, roomLength); } room.transform.localScale = new Vector3(roomWidth, .05f, roomLength); room.name = "Room " + i; room.GetComponent <Room>().roomID = i; dungeonGenerator.dungeonRooms.Add(room.GetComponent <Room>()); } StartCoroutine(SeperateRooms()); }
private void CombineMeshes() { foreach (Room room in dungeonGenerator.dungeonRooms) { room.gameObject.GetComponent <BoxCollider>().enabled = false; DungeonUtilities.CombineChildMeshes(room.floorParent); room.floorParent.gameObject.AddComponent <BoxCollider>(); DungeonUtilities.CombineChildMeshes(room.wallParent); room.wallParent.gameObject.AddComponent <MeshCollider>(); } }
public void ConstructDungeon() { dungeonParent = GameObject.FindGameObjectWithTag("DungeonParent"); debugMode = dungeonGenerator.debugMode; FixScale(); CreateDungeonArray(); CreateRooms(); CreateConnections(); CreateWalls(); CombineMeshes(); if (debugMode) { DungeonUtilities.WriteDungeonArrayToTextFile(dungeon2DArray, empty); } }
private IEnumerator SeperateRooms() { List <Collider> colliders = new List <Collider>(); for (int i = 0; i < dungeonParent.transform.childCount; i++) { colliders.Add(dungeonParent.transform.GetChild(i).GetComponent <Collider>()); } bool seperationComplete = false; while (!seperationComplete) { seperationComplete = true; IComparer roomSorter = new RoomSorter(); foreach (Collider col in colliders) { Collider[] intersectingColliders = Physics.OverlapBox(col.transform.position, new Vector3(col.transform.localScale.x / 2f, 1f, col.transform.localScale.z / 2f)); Array.Sort(intersectingColliders, roomSorter); if (intersectingColliders.Length > 1) { seperationComplete = false; foreach (Collider c in intersectingColliders) { if (col != c) { Vector3 directionToMove = col.transform.position - c.transform.position; directionToMove.Normalize(); col.transform.position = new Vector3(col.transform.position.x + (directionToMove.x), 0f, col.transform.position.z + (directionToMove.z)); } } col.GetComponent <Room>().isSnappedToGrid = true; DungeonUtilities.SnapBoundsToGrid(col.transform); } else if (!col.GetComponent <Room>().isSnappedToGrid) { col.GetComponent <Room>().isSnappedToGrid = true; DungeonUtilities.SnapBoundsToGrid(col.transform); } } } SnapRoomsToPositiveXZ(); roomGenerationComplete = true; yield return(null); }