bool Linkable(DunGenRoom l, DunGenRoom c, int dir) { if (dir == 0) { if (l.rightOcc == false && c.leftOcc == false) { return(true); } } else if (dir == 1) { if (l.leftOcc == false && c.rightOcc == false) { return(true); } } else if (dir == 2) { if (l.downOcc == false && c.upOcc == false) { return(true); } } else if (dir == 3) { if (l.upOcc == false && c.downOcc == false) { return(true); } } return(false); }
void Generate() { debugTimer++; if (SpawnedRooms.Count == 0) { currentDirection = GetRandomDirection(); GameObject start = GetEndRoom(Random.Range(0, 4)); Spawn(start, currentDirection); } else if (SpawnedRooms.Count < totalNonDeadEndRooms && SpawnedRooms.Count != 0 && debugTimer < 25) { currentDirection = GetRandomDirection(); GameObject gRoom = GetRandomRoom(); selectedRoom = gRoom.GetComponent <DunGenRoom>(); if (Linkable(lastRoom, selectedRoom, currentDirection)) { if (!RoomInWayOfDirection(currentDirection)) { Spawn(gRoom, currentDirection); } } } else { FinishRooms(); } }
// Pathstuff here void Spawn(GameObject gRoom, int dir) { if (dir == 0) { GameObject roomClone = Instantiate(gRoom, transform) as GameObject; var r = roomClone.GetComponent <DunGenRoom>(); if (lastRoom == null) { } else { r.x = x + 1; r.y = y; lastRoom.rightOcc = true; r.leftOcc = true; lastRoom.connectedRightRoom = r; r.connectedLeftRoom = lastRoom; } roomClone.transform.position = new Vector3(r.x * 10, r.y * 10, -2); CheckForFurthestPoint(roomClone.transform.position); SpawnedRooms.Add(r); lastRoom = r; } else if (dir == 1) { GameObject roomClone = Instantiate(gRoom, transform) as GameObject; var r = roomClone.GetComponent <DunGenRoom>(); if (lastRoom == null) { } else { r.x = x - 1; r.y = y; lastRoom.leftOcc = true; r.rightOcc = true; lastRoom.connectedLeftRoom = r; r.connectedRightRoom = lastRoom; } roomClone.transform.position = new Vector3(r.x * 10, r.y * 10, -2); CheckForFurthestPoint(roomClone.transform.position); SpawnedRooms.Add(r); lastRoom = r; } else if (dir == 2) { GameObject roomClone = Instantiate(gRoom, transform) as GameObject; var r = roomClone.GetComponent <DunGenRoom>(); if (lastRoom == null) { } else { r.x = x; r.y = y - 1; lastRoom.downOcc = true; r.upOcc = true; lastRoom.connectedDownRoom = r; r.connectedUpRoom = lastRoom; } roomClone.transform.position = new Vector3(r.x * 10, r.y * 10, -2); CheckForFurthestPoint(roomClone.transform.position); SpawnedRooms.Add(r); lastRoom = r; } else if (dir == 3) { GameObject roomClone = Instantiate(gRoom, transform) as GameObject; var r = roomClone.GetComponent <DunGenRoom>(); if (lastRoom == null) { } else { r.x = x; r.y = y + 1; lastRoom.upOcc = true; r.downOcc = true; lastRoom.connectedUpRoom = r; r.connectedDownRoom = lastRoom; } roomClone.transform.position = new Vector3(r.x * 10, r.y * 10, -2); CheckForFurthestPoint(roomClone.transform.position); SpawnedRooms.Add(r); lastRoom = r; } x = lastRoom.x; y = lastRoom.y; debugTimer = 0; }
void FinishRooms() { for (int i = 0; i < SpawnedRooms.Count; i++) { if (!SpawnedRooms[i].locked) { lastRoom = SpawnedRooms[i]; x = lastRoom.x; y = lastRoom.y; if (!SpawnedRooms[i].rightOcc) { currentDirection = 0; GameObject grm = GetEndRoom(0); selectedRoom = grm.GetComponent <DunGenRoom>(); if (Linkable(lastRoom, selectedRoom, currentDirection)) { if (!RoomInWayOfDirection(currentDirection)) { Spawn(grm, currentDirection); } else { SpawnedRooms[i].rightOcc = true; } } } if (!SpawnedRooms[i].leftOcc) { currentDirection = 1; GameObject grm = GetEndRoom(1); selectedRoom = grm.GetComponent <DunGenRoom>(); if (Linkable(lastRoom, selectedRoom, currentDirection)) { if (!RoomInWayOfDirection(currentDirection)) { Spawn(grm, currentDirection); } else { SpawnedRooms[i].leftOcc = true; } } } if (!SpawnedRooms[i].downOcc) { currentDirection = 2; GameObject grm = GetEndRoom(2); selectedRoom = grm.GetComponent <DunGenRoom>(); if (Linkable(lastRoom, selectedRoom, currentDirection)) { if (!RoomInWayOfDirection(currentDirection)) { Spawn(grm, currentDirection); } else { SpawnedRooms[i].downOcc = true; } } } if (!SpawnedRooms[i].upOcc) { currentDirection = 3; GameObject grm = GetEndRoom(3); selectedRoom = grm.GetComponent <DunGenRoom>(); if (Linkable(lastRoom, selectedRoom, currentDirection)) { if (!RoomInWayOfDirection(currentDirection)) { Spawn(grm, currentDirection); } else { SpawnedRooms[i].upOcc = true; } } } complete = CheckIfComplete(); } } }