// Splits area considered for the sub dungeon based on given room size limitations public bool Cut(int minSize) { if (!IsLeaf()) { return(false); } // Default value to avoid issues bool hCut = false; // If wider than a particular tolerance (usually 1.25), split vertically or horizontally if ((float)subDunWidth / subDunHeight >= tolerance) { hCut = false; } else if ((float)subDunHeight / subDunWidth >= tolerance) { hCut = true; } int max = 0; if (hCut) { max = subDunHeight - minSize; } else { max = subDunWidth - minSize; } // Checks to see if current sub area can be further divided into more, if not then this is a leaf and we are done with this section if (max <= minSize) { Debug.Log("Sub-Dungeon " + debugId + " is leaf"); return(false); } int cutPoint = _random.Next(minSize, max); // Cut current area horizontally on the randomly select cut point, the left child being the upper half, the right being the lower half // Upper half has a height equal to the size of cutPoint, lower has original sub area height minus cutPoint if (hCut) { left = new SubDungeon(_x, _y, subDunWidth, cutPoint, _random); right = new SubDungeon(_x, _y + cutPoint, subDunWidth, subDunHeight - cutPoint, _random); } else // Do the above but vice versa { left = new SubDungeon(_x, _y, cutPoint, subDunHeight, _random); right = new SubDungeon(_x + cutPoint, _y, subDunWidth - cutPoint, subDunHeight, _random); } return(true); }
public T GenerateMap() { _map.Initialize(_width, _height); _map.Clear(new Tile(Tile.Type.Block)); SubDungeon rootLeaf = new SubDungeon(0, 0, _map.Width, _map.Height, _random); Debug.Log("Root subdungeon added"); _subdungeons.Add(rootLeaf); bool splitSuccessfully = true; //Loop through all leaves until they can no longer split successfully while (splitSuccessfully) { splitSuccessfully = false; for (int i = 0; i < _subdungeons.Count; i++) { if (_subdungeons[i].IsLeaf()) { if ((_subdungeons[i].subDunWidth > _maxSize) || (_subdungeons[i].subDunHeight > _maxSize)) { //Try to split the leaf if (_subdungeons[i].Cut(_minSize)) { _subdungeons.Add(_subdungeons[i].left); Debug.Log("Left sub dungeon " + _subdungeons[i].left.debugId + " added successfully"); _subdungeons.Add(_subdungeons[i].right); Debug.Log("Left sub dungeon " + _subdungeons[i].right.debugId + " added successfully"); splitSuccessfully = true; } } } } } rootLeaf.CreateRooms <T>(this, _maxSize, _maxRoom, _minRoom); return(_map); }
void BSPDraw(SubDungeon subDungeon) { if (subDungeon == null) { return; } if (subDungeon.IsLeaf()) { for (int i = (int)subDungeon.room.x; i < subDungeon.room.xMax; i++) { for (int j = (int)subDungeon.room.y; j < subDungeon.room.yMax; j++) { GameObject instance = Instantiate(floorTile, new Vector2(i, j), Quaternion.identity) as GameObject; instance.transform.SetParent(transform); mapPositionsFloor[i, j] = instance; } } } else { BSPDraw(subDungeon.left); BSPDraw(subDungeon.right); } }