using System; using System.Collections.Generic; using Cysharp.Threading.Tasks; using UnityEngine; namespace DanieleMarotta.RiversongCodeShowcase { [GameSystemGroup(typeof(WorldGenSystemGroup))] [InitializeAfter(typeof(WorldCreationSystem))] public class WorldGenerationOperationsSystem : GameSystem, IInitializable, IDisposable { [InjectService] private World _world; [InjectService] private ISignalBus _signalBus; private readonly List _operations = new(); public WorldGenerationOperationsSystem(IServiceLocator serviceLocator) : base(serviceLocator) { } public UniTask InitializeAsync() { _operations.Add(new MapDataInitializationOperation()); _operations.Add(new TerrainMaterialsInitializationOperation()); _operations.Add(new TerrainGeneratorOperation()); _operations.Add(new TreeResourcesGeneratorOperation()); _operations.Add(new StoneResourcesGeneratorOperation()); _operations.Add(new FreeProductStacksGeneratorOperation()); _operations.Add(new CritterHerdsGeneratorOperation()); foreach (var operation in _operations) ServiceLocator.Inject(operation); _signalBus.Subscribe(OnGameStarted); return UniTask.CompletedTask; } public void Dispose() { _signalBus.Unsubscribe(OnGameStarted); } private void OnGameStarted(GameStartedSignal signal) { ExecuteOperationsAsync().Forget(Debug.LogException); } private async UniTask ExecuteOperationsAsync() { _world.Seed = Environment.TickCount; foreach (var operation in _operations) await operation.Execute(_world); var generationCompletedCallbacks = new List(); _signalBus.Raise(new WorldGenerationCompletedSignal(generationCompletedCallbacks)); foreach (var callback in generationCompletedCallbacks) await callback.OnWorldGenerationCompletedAsync(_world); _signalBus.Raise(new WorldReadySignal()); } } }