private void Update() { if (!showGenerationProcess) { return; } timer -= Time.deltaTime; if (addRoomsDone) { return; } if (timer <= 0) { timer = generationDelay; for (int i = 0; i < mainRoomData.Count; i++) { if (mainRoomData[i].roomCount < mainRoomData[i].numberOfRooms) { int threshold = (int)(mainRoomData[i].spawnThreshold * mainRoomData[i].numberOfRooms); if (threshold >= allRooms.Count - 1) { threshold = allRooms.Count - 1; } currentRoom = allRooms[Random.Range(threshold, allRooms.Count)]; GenerationData data = mainRoomData[i]; allRooms.Add(Generate(ref data)); mainRoomccc++; mainRoomData[i] = data; } else { mainRoomsDone = true; } } if (mainRoomsDone) { for (int i = 0; i < additionalRoomData.Count; i++) { if (additionalRoomData[i].roomCount < additionalRoomData[i].numberOfRooms) { if (additionalRoomData[i].spawnThreshold >= allRooms.Count - 1) { additionalRoomData[i].spawnThreshold = allRooms.Count - 1; } int threshold = (int)(additionalRoomData[i].spawnThreshold * mainRoomccc); int v = Random.Range(threshold, allRooms.Count - 1); currentRoom = allRooms[v]; GenerationData data = additionalRoomData[i]; allRooms.Add(Generate(ref data)); additionalRoomData[i] = data; } else { addRoomsDone = true; } } } } }
public void Init() { startRoom = Instantiate(mainRoomData[0].possibleRooms[0].roomObject, transform.position, Quaternion.identity, transform).GetComponent <RoomObject>(); mainRoomData[0].roomInstances.Add(startRoom); startRoom.OnRoomEnter(); startRoom.gameObject.GetComponent <GridScript>().Init(); allRooms.Add(startRoom); currentRoom = startRoom; if (randomizeSeed) { seed = Random.Range(0, 9999999); } Random.seed = seed; int mainRoomCount = 0; if (showGenerationProcess) { return; } //SetupData(); for (int i = 0; i < mainRoomData.Count; i++) { // CREATE MAIN DUNGEON PATHS int threshold = (int)(mainRoomData[i].spawnThreshold * mainRoomData[i].numberOfRooms); if (threshold >= allRooms.Count - 1) { threshold = allRooms.Count - 1; } currentRoom = allRooms[Random.Range(threshold, allRooms.Count)]; while (mainRoomData[i].roomCount < mainRoomData[i].numberOfRooms) { GenerationData data = mainRoomData[i]; allRooms.Add(Generate(ref data)); mainRoomData[i] = data; } mainRoomCount += mainRoomData[i].numberOfRooms; } for (int i = 0; i < additionalRoomData.Count; i++) { // APPEND ADDITIONAL ROOMS BASED ON THRESHOLDS while (additionalRoomData[i].roomCount < additionalRoomData[i].numberOfRooms) { if (additionalRoomData[i].spawnThreshold >= allRooms.Count - 1) { additionalRoomData[i].spawnThreshold = allRooms.Count - 1; } int threshold = (int)(additionalRoomData[i].spawnThreshold * mainRoomCount); int v = Random.Range(threshold, allRooms.Count - 1); currentRoom = allRooms[v]; GenerationData data = additionalRoomData[i]; allRooms.Add(Generate(ref data)); additionalRoomData[i] = data; } } for (int i = 1; i < allRooms.Count; i++) { allRooms[i].OnRoomExit(); } }
public RoomObject Generate(ref GenerationData data) { didPlaceRoom = false; //Vector2 direction = RandomDir(); GameObject toSpawn = null; RoomObject newRoom = null; Transform me = null; Transform other = null; int roomAttempts = Random.Range((int)0, (int)data.possibleRooms.Count); int direction = Random.Range((int)0, (int)4); while (!didPlaceRoom) { toSpawn = data.possibleRooms[roomAttempts].roomObject; newRoom = Instantiate(toSpawn, currentRoom.transform.position, Quaternion.identity, transform).GetComponent <RoomObject>(); roomAttempts = Random.Range((int)0, (int)data.possibleRooms.Count - 1); direction = Random.Range((int)0, (int)4); #region Placement if (direction == 0) { me = currentRoom.northDoors[Random.Range(0, currentRoom.northDoors.Count)]; other = newRoom.southDoors[Random.Range(0, newRoom.southDoors.Count)]; Vector3 distance = (me.position - other.position); newRoom.transform.position += (distance) + (Vector3)(Vector2.up * separation); } else if (direction == 1) { me = currentRoom.southDoors[Random.Range(0, currentRoom.southDoors.Count)]; other = newRoom.northDoors[Random.Range(0, newRoom.northDoors.Count)]; Vector3 distance = (me.position - other.position); newRoom.transform.position += (distance) + (Vector3)(Vector2.down * separation); } else if (direction == 2) { me = currentRoom.westDoors[Random.Range(0, currentRoom.westDoors.Count)]; other = newRoom.eastDoors[Random.Range(0, newRoom.eastDoors.Count)]; Vector3 distance = (me.position - other.position); newRoom.transform.position += (distance) + (Vector3)(Vector2.left * separation); } else if (direction == 3) { me = currentRoom.eastDoors[Random.Range(0, currentRoom.eastDoors.Count)]; other = newRoom.westDoors[Random.Range(0, newRoom.westDoors.Count)]; Vector3 distance = (me.position - other.position); newRoom.transform.position += (distance) + (Vector3)(Vector2.right * separation); } #endregion RoomObject obstacle = CheckIfRoomCanSpawn(newRoom); if (obstacle == null) { didPlaceRoom = true; me.gameObject.SetActive(true); other.gameObject.SetActive(true); other.GetComponent <Door>().to = currentRoom; me.GetComponent <Door>().from = currentRoom; currentRoom = newRoom; data.roomInstances.Add(currentRoom); data.roomCount++; currentRoom.GetComponent <SpriteRenderer>().color = new Color(data.color.r, data.color.g, data.color.b, data.color.a); other.GetComponent <Door>().from = currentRoom; me.GetComponent <Door>().to = currentRoom; } else { me = null; other = null; didPlaceRoom = false; DestroyImmediate(newRoom.gameObject); } #region Loop exceptions mx++; nmx++; if (mx >= max) { if (data.roomInstances.Count > 0) { currentRoom = data.roomInstances[Random.Range(0, data.roomInstances.Count - 1)]; } else { currentRoom = allRooms[0]; } mx = 0; //return null; } if (nmx >= mmax) { Debug.LogError("Can't fit [" + data.name + "] dungeon"); nmx = 0; return(null); } #endregion } return(newRoom); }