I am working on dungeon generator and I am stuck in the very beginning: placing rooms (DungeonBlocks). My idea is: place N rooms in scene then shift them so there is no intersections. The very first room stays in place, the second moves in according to the first one, the third - in according to the first and second and so on. Apparently, I am stuck in infinite loop (Unity says waiting for code execution), but I don`t understand how and why it appears.
Code for room shifting:
private void SortRooms()
{
_spawnedDungeonBlocks[0].gameObject.SetActive(true);
for (int i = 1; i < _spawnedDungeonBlocks.Count; i++)
{
DungeonBlock db = _spawnedDungeonBlocks*;*
db.gameObject.SetActive(true);
Vector3 direction;
List neighbours = new List();
FindOverlappingRooms(ref neighbours, db, i);
int neighbourCount = neighbours.Count;
Debug.Log(db.transform.position + " " + neighbourCount);
while (neighbourCount > 0)
{
Debug.Log(“Running…”);
direction = Vector3.zero;
foreach (var collider in neighbours)
direction += (db.transform.position - collider.transform.position);
direction = direction.normalized;
direction.x *= _mPerUnit.x;
direction.z *= _mPerUnit.y;
Vector3 newPosition = db.transform.position + direction;
db.transform.position = newPosition;
FindOverlappingRooms(ref neighbours, db, i);
neighbourCount = neighbours.Count;
}
}
Debug.Log(_spawnedDungeonBlocks.Count);
}
Code for detecting intersections:
private void FindOverlappingRooms(ref List neighbours, DungeonBlock db, int i)
{
neighbours.Clear();
for (int j = 0; j < i; j++)
if (db.GetComponent().bounds.Intersects(_spawnedDungeonBlocks[j]
.GetComponent().bounds))
neighbours.Add(_spawnedDungeonBlocks[j]);
}
Variables are:
- _MPerUnit - size of the grid cell,
- _spawnedDungeonBlocks - list of spawned rooms
Sometimes it does not enter infinite loop, but I just can`t see what is wrong with while(neighbourCount > 0) {…}