I’ve spent the past couple of days toying with a dungeon generator. I made lots of progress (with some help of course ). However, I’ve run into a new problem.
After implementing a system that continues spawning rooms in a set direction for k amount of times, the number of rooms generated became substantially lower than the set amount should be.
For example, I want to spawn 150 rooms-- 107 spawn.
I feel like this has something to do with the for loop that deals with these “direction repeats” (lines 55-70), but maybe not?
Any suggestions on how I could begin to fix this would be extremely appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DungeonGenerationManager : MonoBehaviour
{
public GameObject room_prefab;
public int roomSize;
#region GENERATION SETTINGS
public int numberOfRooms;
public int maxDirectionRepeat = 1;
#endregion
public int roomNumber;
private static readonly List<Vector3Int> roomDirections = new List<Vector3Int>()
{
Vector3Int.forward,
Vector3Int.back,
Vector3Int.right,
Vector3Int.left,
Vector3Int.up,
Vector3Int.down
};
private void Start()
{
InitialIteration();
}
void InitialIteration() //Create rooms
{
List<Vector3Int> occupiedCoordinates = new List<Vector3Int>();
Vector3Int currentPosition = Vector3Int.zero;
for(int i = 0; i < numberOfRooms; i++)
{
List<Vector3Int> possibleSpawnDirections = new List<Vector3Int>();
for(int j = 0; j < roomDirections.Count; j++)
{
Vector3Int possibleDirection = roomDirections[j];
if(!occupiedCoordinates.Contains(currentPosition + possibleDirection * roomSize))
{
possibleSpawnDirections.Add(possibleDirection);
}
}
if(possibleSpawnDirections.Count > 0)
{
Vector3Int chosenDirection = possibleSpawnDirections[Random.Range(0, possibleSpawnDirections.Count)];
int repeatTime = Random.Range(1, maxDirectionRepeat);
for(int k = 0; k < repeatTime; k++) //keep generating in this direction k times
{
Vector3Int possiblePosition = currentPosition + (chosenDirection * roomSize);
if (!occupiedCoordinates.Contains(possiblePosition))
{
currentPosition = currentPosition + (chosenDirection * roomSize);
occupiedCoordinates.Add(currentPosition);
GameObject instantiatedRoom = Instantiate(room_prefab, currentPosition, Quaternion.identity);
roomNumber++;
instantiatedRoom.name = "Room_" + roomNumber;
i++; //make sure to increase i, becuase if you don't, these "repeated rooms" wont count towards the total room count
}
else
{
print("no more available spots!");
currentPosition = occupiedCoordinates[Random.Range(0, occupiedCoordinates.Count)];
}
}
}
else //if there are no more available positions left, choose a random occupied position and branch from there!
{
print("no more available spots!");
currentPosition = occupiedCoordinates[Random.Range(0, occupiedCoordinates.Count)];
}
}
}
}