// Generate the list of rooms and dig them public void GenerateRooms(ref List <Room_2D> _rooms, QuadTreeMapLayer _quadTree) { // Childless node if (_quadTree.northWest == null && _quadTree.northEast == null && _quadTree.southWest == null && _quadTree.southEast == null) { _rooms.Add(GenerateRoom(_quadTree)); return; } // Recursive call if (_quadTree.northWest != null) { GenerateRooms(ref _rooms, _quadTree.northWest); } if (_quadTree.northEast != null) { GenerateRooms(ref _rooms, _quadTree.northEast); } if (_quadTree.southWest != null) { GenerateRooms(ref _rooms, _quadTree.southWest); } if (_quadTree.southEast != null) { GenerateRooms(ref _rooms, _quadTree.southEast); } }
// Generate a single room public Room_2D GenerateRoom(QuadTreeMapLayer _quadTree) { // Center of the room XY roomCenter = new XY(); roomCenter.x = Random.Range(ROOM_WALL_BORDER + _quadTree.boundary.Left() + ROOM_MIN_SIZE / 2.0f, _quadTree.boundary.Right() - ROOM_MIN_SIZE / 2.0f - ROOM_WALL_BORDER); roomCenter.y = Random.Range(ROOM_WALL_BORDER + _quadTree.boundary.Bottom() + ROOM_MIN_SIZE / 2.0f, _quadTree.boundary.Top() - ROOM_MIN_SIZE / 2.0f - ROOM_WALL_BORDER); // Half size of the room XY roomHalf = new XY(); float halfX = (_quadTree.boundary.Right() - roomCenter.x - ROOM_WALL_BORDER); float halfX2 = (roomCenter.x - _quadTree.boundary.Left() - ROOM_WALL_BORDER); if (halfX2 < halfX) { halfX = halfX2; } if (halfX > ROOM_MAX_SIZE / 2.0f) { halfX = ROOM_MAX_SIZE / 2.0f; } float halfY = (_quadTree.boundary.Top() - roomCenter.y - ROOM_WALL_BORDER); float halfY2 = (roomCenter.y - _quadTree.boundary.Bottom() - ROOM_WALL_BORDER); if (halfY2 < halfY) { halfY = halfY2; } if (halfY > ROOM_MAX_SIZE / 2.0f) { halfY = ROOM_MAX_SIZE / 2.0f; } roomHalf.x = Random.Range((float)ROOM_MIN_SIZE / 2.0f, halfX); roomHalf.y = Random.Range((float)ROOM_MIN_SIZE / 2.0f, halfY); // Eliminate ugly zones if (ROOM_UGLY_ENABLED == false) { float aspect_ratio = roomHalf.x / roomHalf.y; if (aspect_ratio > ROOM_MAX_RATIO || aspect_ratio < 1.0f / ROOM_MAX_RATIO) { return(GenerateRoom(_quadTree)); } } // Create AABB AABB randomAABB = new AABB(roomCenter, roomHalf); // Dig the room in our tilemap DigRoom(randomAABB.BottomTile(), randomAABB.LeftTile(), randomAABB.TopTile() - 1, randomAABB.RightTile() - 1); // Return the room return(new Room_2D(randomAABB, _quadTree)); }
/* * METHODS */ // Clean QuadTree public void Clear() { seed = -1; boundary = new AABB(); northWest = null; northEast = null; southWest = null; southEast = null; room = null; }
// Symmetric division public void Subdivide(int level, int maxLevels) { if (level > maxLevels) { return; } northWest = new QuadTreeMapLayer(new AABB(new XY((boundary.center.x - boundary.half.x / 2), (boundary.center.y + boundary.half.y / 2)), boundary.half / 2), dungeon); northEast = new QuadTreeMapLayer(new AABB(new XY((boundary.center.x + boundary.half.x / 2), (boundary.center.y + boundary.half.y / 2)), boundary.half / 2), dungeon); southWest = new QuadTreeMapLayer(new AABB(new XY((boundary.center.x - boundary.half.x / 2), (boundary.center.y - boundary.half.y / 2)), boundary.half / 2), dungeon); southEast = new QuadTreeMapLayer(new AABB(new XY((boundary.center.x + boundary.half.x / 2), (boundary.center.y - boundary.half.y / 2)), boundary.half / 2), dungeon); northWest.Subdivide(level + 1, maxLevels); northEast.Subdivide(level + 1, maxLevels); southWest.Subdivide(level + 1, maxLevels); southEast.Subdivide(level + 1, maxLevels); }
// On Awake public void create(object o) { // Initialize the tilemap resetBlocks(); // Init QuadTree quadTree = new QuadTreeMapLayer(new AABB(new XY(MAP_COLS / 2.0f, MAP_ROWS / 2.0f), new XY(MAP_COLS / 2.0f, MAP_ROWS / 2.0f)), this); // List of rooms rooms = new List <Room_2D> (); //barriers = new List<GameObject> (); // Set the randome seed //Random.seed = seed; // Generate Dungeon Debug.Log("Dungeon Generation Started"); GenerateDungeon(seed); }
// Generate the quadtree system void GenerateQuadTree(ref QuadTreeMapLayer _quadTree) { _quadTree.GenerateQuadTree(seed); }
// Procedurally generate QuadTrees // Make a maximum of "max_depth" levels of depth public void GenerateZones(int depth, int max_depth) { // Reached max depth if (depth > max_depth) { return; } // No place for 4 rooms? return if (boundary.half.x < roomRealMinSize) { return; } if (boundary.half.y < roomRealMinSize) { return; } // Random possibilty of not generating zone int r = Random.Range(0, 100); if (r < Dungeon.CHANCE_STOP && depth > 1) { return; } // Try to make a slice with space for 4 rooms // Retry "N=SLICE_TRIES" times XY slice = new XY(); bool new_slice = RandomSlice(1, Dungeon.SLICE_TRIES, ref slice); if (new_slice == false) { return; } // Generate children QuadTrees // NORTHWEST AABB aabb1 = new AABB(); XY size = new XY(slice.x - boundary.Left(), boundary.Top() - slice.y); aabb1.center = new XY(slice.x - size.x / 2.0f, slice.y + size.y / 2.0f); aabb1.half = new XY(size.x / 2.0f, size.y / 2.0f); northWest = new QuadTreeMapLayer(aabb1, dungeon); northWest.parent = this; northWest.GenerateZones(depth + 1, max_depth); // NORTHEAST AABB aabb2 = new AABB(); size = new XY(boundary.Right() - slice.x, boundary.Top() - slice.y); aabb2.center = new XY(slice.x + size.x / 2.0f, slice.y + size.y / 2.0f); aabb2.half = new XY(size.x / 2.0f, size.y / 2.0f); northEast = new QuadTreeMapLayer(aabb2, dungeon); northEast.parent = this; northEast.GenerateZones(depth + 1, max_depth); // SOUTHWEST AABB aabb3 = new AABB(); size = new XY(slice.x - boundary.Left(), slice.y - boundary.Bottom()); aabb3.center = new XY(slice.x - size.x / 2.0f, slice.y - size.y / 2.0f); aabb3.half = new XY(size.x / 2.0f, size.y / 2.0f); southWest = new QuadTreeMapLayer(aabb3, dungeon); southWest.parent = this; southWest.GenerateZones(depth + 1, max_depth); // SOUTEAST AABB aabb4 = new AABB(); size = new XY(boundary.Right() - slice.x, slice.y - boundary.Bottom()); aabb4.center = new XY(slice.x + size.x / 2.0f, slice.y - size.y / 2.0f); aabb4.half = new XY(size.x / 2.0f, size.y / 2.0f); southEast = new QuadTreeMapLayer(aabb4, dungeon); southEast.parent = this; southEast.GenerateZones(depth + 1, max_depth); }
public Room_2D(AABB b, QuadTreeMapLayer q) { boundary = b; quadtree = q; quadtree.room = this; }