- Home /
Procedural Generation - Rooms spawning at same positions.
Hello,
I've been working on my procedural generation for quite some time now. I'm having a bit of trouble, and I think it might be the method that I am using to determine where to spawn my next room. I'm currently using vector math, and I think this might be the wrong way...I'm wondering/hoping if you have a better idea. If you can point out what I'm doing wrong in this massive method, I would be greatly appreciated.
The problem seems to spawn when I am spawning a branch, some of the rooms are spawning on top of each other, when I dont think they should be!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DSSpawn : MonoBehaviour {
public GameObject RoomPre;
private Vector3 doorPosition;
private Vector3 branchRoomSpawnPosition;
private int roomCount;
private int northDoor;
private int southDoor;
private int eastDoor;
private int westDoor;
private int currentBranchCount;
private int branchLimit = 3;
private int sizeOfList;
private bool forceNorth;
private bool forceSouth;
private bool forceWest;
private bool forceEast;
private string roomName;
private List<string> doorList = new List<string>();
Dictionary<int, List<string>> roomUnattachedDoors = new Dictionary<int, List<string>>();
Dictionary<int, Vector3> roomPosition = new Dictionary<int, Vector3>();
Dictionary<int, List<string>> branchUnattachedDoors = new Dictionary<int, List<string>>();
Dictionary<int, Vector3> branchRoomPosition = new Dictionary<int, Vector3>();
void Update () {
if(Input.GetKeyDown ("space")) {
SpawnDungeon();
}
}
// x + 40 = right spawn
// x - 40 = left spawn
// y + 21 = up spawn
// y - 21 = down spawn
void SpawnDungeon() {
doorPosition = new Vector3(0,0,0);
//Spawn inital room
DoorSpawn();
Instantiate (RoomPre, doorPosition, RoomPre.transform.rotation);
roomCount = 1;
roomUnattachedDoors.Add (roomCount, new List<string>(doorList));
roomPosition.Add (roomCount, doorPosition);
doorList.Clear();
// Begin loop to create branches for each unattached door.
List<string> unattachedMainRoom = roomUnattachedDoors[1];
//List<string> doorsToRemove = roomUnattachedDoors[1];
foreach (string unattachedDoor in unattachedMainRoom) {
//Debug.Log ("unattached door loop");
//doorsToRemove.Remove (unattachedDoor);
//roomUnattachedDoors[1] = new List<string>(doorsToRemove);
currentBranchCount = 1;
doorPosition = new Vector3(0,0,0);
while (currentBranchCount <= branchLimit) {
//Debug.Log ("currentBranchCount loop");
// Here I need if logic to get 2nd iteration of loop, if 2nd iteration, need to grab an unattached door from last spawned room and spawn next room appropriately.
// if currentBranchCount > 1 logic below, else grab unattached doors from list and grab one at random. use that to spawn next. repeat.
if (currentBranchCount < 2 ) {
if (unattachedDoor == "northDoorPre") {
forceSouth = true;
doorPosition.y = doorPosition.y + 21;
} else if (unattachedDoor == "southDoorPre") {
forceNorth = true;
doorPosition.y = doorPosition.y + -21;
} else if (unattachedDoor == "westDoorPre") {
forceEast = true;
doorPosition.x = doorPosition.x + -40;
} else if (unattachedDoor == "eastDoorPre") {
forceWest = true;
doorPosition.x = doorPosition.x + 40;
}
Debug.Log("First branch loop position = " + doorPosition);
roomCount = roomCount + 1;
roomPosition.Add (roomCount, doorPosition);
DoorSpawn();
Instantiate (RoomPre, doorPosition, RoomPre.transform.rotation);
roomUnattachedDoors.Add (roomCount, new List<string>(doorList));
branchUnattachedDoors.Add(currentBranchCount, new List<string>(doorList));
branchRoomPosition.Add(currentBranchCount, doorPosition);
doorList.Clear ();
currentBranchCount = currentBranchCount + 1;
} else {
List<string> previousSpawned = new List<string>();
previousSpawned = roomUnattachedDoors[roomCount];
sizeOfList = previousSpawned.Count;
roomName = previousSpawned[Random.Range (0, sizeOfList)];
if (roomName == "northDoorPre") {
forceSouth = true;
doorPosition.y = doorPosition.y + 21;
} else if (roomName == "southDoorPre") {
forceNorth = true;
doorPosition.y = doorPosition.y + -21;
} else if (roomName == "westDoorPre") {
forceEast = true;
doorPosition.x = doorPosition.x + -40;
} else if (roomName == "eastDoorPre") {
forceWest = true;
doorPosition.x = doorPosition.x + 40;
}
Debug.Log("Next Branch Loop position = " + doorPosition);
roomCount = roomCount + 1;
roomPosition.Add (roomCount, doorPosition);
DoorSpawn();
Instantiate (RoomPre, doorPosition, RoomPre.transform.rotation);
branchUnattachedDoors.Add(currentBranchCount, new List<string>(doorList));
branchRoomPosition.Add(currentBranchCount, doorPosition);
roomUnattachedDoors.Add (roomCount, new List<string>(doorList));
doorList.Clear ();
currentBranchCount = currentBranchCount + 1;
}
} // End while loop for branch spawning
currentBranchCount = 1;
foreach (List<string> unattachedDoors in branchUnattachedDoors.Values) {
foreach (string unattached in unattachedDoors) {
branchRoomSpawnPosition = branchRoomPosition[currentBranchCount];
if (unattached == "northDoorPre") {
forceSouth = true;
forceWest = false;
forceEast = false;
forceNorth = false;
branchRoomSpawnPosition.y = branchRoomSpawnPosition.y + 21;
} else if (unattached == "southDoorPre") {
forceNorth = true;
forceSouth = false;
forceWest = false;
forceEast = false;
branchRoomSpawnPosition.y = branchRoomSpawnPosition.y + -21;
} else if (unattached == "westDoorPre") {
forceEast = true;
forceNorth = false;
forceSouth = false;
forceWest = false;
branchRoomSpawnPosition.x = branchRoomSpawnPosition.x + -40;
} else if (unattached == "eastDoorPre") {
forceWest = true;
forceNorth = false;
forceSouth = false;
forceEast = false;
branchRoomSpawnPosition.x = branchRoomSpawnPosition.x + 40;
}
DoorSpawn();
Instantiate(RoomPre, branchRoomSpawnPosition, transform.rotation);
}
currentBranchCount = currentBranchCount + 1;
} // End loop for closing branch rooms
branchUnattachedDoors.Clear();
branchRoomPosition.Clear();
} // End main loop for initial room unattached
}
void DoorSpawn() {
northDoor = Random.Range (0,2);
southDoor = Random.Range (0,2);
eastDoor = Random.Range (0,2);
westDoor = Random.Range (0,2);
if (forceNorth)
{
northDoor = 1;
}
if (forceSouth)
{
southDoor = 1;
}
if (forceWest)
{
westDoor = 1;
}
if (forceEast)
{
eastDoor = 1;
}
if (northDoor == 0 && southDoor == 0 && westDoor == 0 && eastDoor == 0)
{
int random = Random.Range(0, 5);
if (random == 1)
{
northDoor = 1;
} else if (random == 2) {
southDoor = 1;
}
else if (random == 3)
{
westDoor = 1;
}
else if (random == 4)
{
eastDoor = 1;
}
}
SpriteRenderer[] doors = RoomPre.GetComponentsInChildren<SpriteRenderer>(true);
foreach (SpriteRenderer renderer in doors) {
if (renderer.name == "northDoorPre") {
if (northDoor == 1) {
renderer.enabled = true;
if (!forceNorth)
{
doorList.Add(renderer.name);
}
forceNorth = false;
} else {
renderer.enabled = false;
}
} else if (renderer.name == "southDoorPre") {
if (southDoor == 1) {
renderer.enabled = true;
if (!forceSouth)
{
doorList.Add(renderer.name);
}
forceSouth = false;
} else {
renderer.enabled = false;
}
} else if (renderer.name == "westDoorPre") {
if (westDoor == 1) {
renderer.enabled = true;
if (!forceWest)
{
doorList.Add(renderer.name);
}
forceWest = false;
} else {
renderer.enabled = false;
}
} else if (renderer.name == "eastDoorPre") {
if (eastDoor == 1) {
renderer.enabled = true;
if (forceEast)
{
doorList.Add(renderer.name);
}
forceEast = false;
} else {
renderer.enabled = false;
}
}
} // End foreach loop
} // End door Spawn
}
Appologies for the massive code post, I'm sure you don't want to read through it all! I figured i'd post the entire method so you can get the hang of what i'm doing.
I believe the problem is down at the lines where im doing doorPosition.x + and doorPosition.y +. I think this is a problem, but I can't seem to think of a way to position the room in the appropriate direction.
I am able to tell which direction it needs to go by the unattached room I am looping through. But how do I actually instantiate it there without a vector 3? Is there a spawn right method or something I'm not aware of, is my math off or something?
Any help is muchly appreciated!!
Thanks,
JayOhh Games.