public void TrySpawnAll() { foreach (Spawnable spawnable in spawnables) { startTime = Time.realtimeSinceStartup; for (int i = 0; i < spawnable.quantity; i++) { GameObject prefab = spawnable.prefabs.RandomElement(); SpawnObstacle prefabObstacle = prefab.GetComponentInChildren <SpawnObstacle>(); SphericalRegionCircle prefabRegion = (prefabObstacle != null)? prefabObstacle.region : null; Vector3 direction; bool zeroDirection, outsideSpawnRegion, blockedBySomeObstacle; bool IsStillOnTime() => (curTime - startTime) <= maxTimeRandomizing; do { direction = UnityEngine.Random.insideUnitSphere.normalized; curTime = Time.realtimeSinceStartup; zeroDirection = direction == Vector3.zero; outsideSpawnRegion = !spawnRegion.Contains(direction); blockedBySomeObstacle = SpawnObstacle.AnyBlocksSpawn(obstacleIds, prefabRegion, direction); } while ((zeroDirection || outsideSpawnRegion || blockedBySomeObstacle) && IsStillOnTime()); if (!IsStillOnTime()) { Debug.Log("Timed out spawning " + prefab + " #" + (i + 1) + "." + (outsideSpawnRegion? " Outside spawn region.":"") + (blockedBySomeObstacle ? " Blocked by some obstacle." : "")); break; } Transform newObj = Instantiate(prefab).transform; newObj.position = direction.normalized * radius; newObj.up = direction; newObj.parent = parent; } Debug.Log("Time spent spawning: " + (Time.realtimeSinceStartup - startTime)); } }
public bool BlocksSpawn(List <string> possibilities, SphericalRegionCircle other, Vector3 hypotheticalUp) => MatchesIdentifiers(possibilities) && region.WouldOverlap(other, hypotheticalUp);
public static bool AnyBlocksSpawn(List <string> possibilities, SphericalRegionCircle other, Vector3 hypotheticalUp) => instanceList.Exists(i => i.BlocksSpawn(possibilities, other, hypotheticalUp));