/// <summary> /// On each module heartbeat, iterate over the list of queued spawns. /// If enough time has elapsed and spawn table rules are met, spawn the object and remove it from the queue. /// </summary> public static void ProcessQueuedSpawns() { var now = DateTime.UtcNow; for (var index = _queuedSpawns.Count - 1; index >= 0; index--) { var queuedSpawn = _queuedSpawns.ElementAt(index); if (now > queuedSpawn.RespawnTime) { var detail = _spawns[queuedSpawn.SpawnDetailId]; var spawnedObject = SpawnObject(queuedSpawn.SpawnDetailId, detail); // A valid spawn wasn't found because the spawn table didn't provide a resref. // Either the table is configured wrong or the requirements for that specific table weren't met. // In this case, we bump the next respawn time and move to the next queued respawn. if (spawnedObject == OBJECT_INVALID) { queuedSpawn.RespawnTime = now.AddMinutes(detail.RespawnDelayMinutes); continue; } var activeSpawn = new ActiveSpawn { SpawnDetailId = queuedSpawn.SpawnDetailId, SpawnObject = spawnedObject }; _activeSpawnsByArea[detail.Area].Add(activeSpawn); RemoveQueuedSpawn(queuedSpawn); } } }
private IEnumerator SpawnObjects() { while (true) { var def = Lifetime.Define(_levelComponent.Lifetime); int index = 0; var i = 0; while (i++ < 3) { index = _random.Next(_factories.Count); if (index != _lastSpawnIndex) { break; } _lastSpawnIndex = index; } var factory = _factories[index]; var spawn = factory.Pop(); spawn.transform.SetParent(_levelComponent.ActiveSpawnTransform); var point = _spawnPoints[_random.Next(_spawnPoints.Count)]; spawn.transform.position = point; _levelComponent.SpawnEffectController.Explosion(point); var active = new ActiveSpawn { Spawn = spawn, Lifetime = def, Speed = _settings.SpawnSpeed, Velocity = _settings.SpawnVelocity }; _activeSpawns.Add(active); def.Lifetime.AddAction(() => { spawn.Hit = false; factory.Push(spawn); ToPool(spawn.transform); _activeSpawns.Remove(active); }); yield return(new WaitForSeconds(_settings.SpawnTime)); } }