using System; using Cysharp.Threading.Tasks; using UnityEngine; using Object = UnityEngine.Object; namespace DanieleMarotta.RiversongCodeShowcase { [RequiresWorldReadyForUpdate] [GameSystemGroup(typeof(LateGameSystemGroup))] public class TileHighlightSystem : GameSystem, IInitializable, IDisposable, IUpdatable, IOnWorldGenerationCompletedCallback { private static readonly int TileHighlightTextureID = Shader.PropertyToID("_Tile_Highlight_Texture"); [InjectService] private GameConfig _config; [InjectService] private World _world; [InjectService] private WorldRenderingState _renderingState; [InjectService] private ISignalBus _signalBus; private GameObject _tileHighlightGameObject; public TileHighlightSystem(IServiceLocator serviceLocator) : base(serviceLocator) { } public UniTask InitializeAsync() { _signalBus.Subscribe(OnWorldGenerationCompleted); return UniTask.CompletedTask; } public void Dispose() { _signalBus.Unsubscribe(OnWorldGenerationCompleted); _renderingState.TileHighlight.Dispose(); if (_tileHighlightGameObject) { Object.Destroy(_tileHighlightGameObject); _tileHighlightGameObject = null; } } private void OnWorldGenerationCompleted(WorldGenerationCompletedSignal signal) { signal.Callbacks.Add(this); } public async UniTask OnWorldGenerationCompletedAsync(World world) { _renderingState.TileHighlight.Create(_world.Size); Shader.SetGlobalTexture(TileHighlightTextureID, _renderingState.TileHighlight.Texture); var tileHighlightPrefab = await _config.UI.TileHighlight.Prefab.LoadAssetAsync(); var tileHighlightGameObject = Object.Instantiate(tileHighlightPrefab); tileHighlightGameObject.transform.position = new Vector3(_world.Size.x * 0.5f, _config.GeneralSettings.BaseElevation + 0.002f, _world.Size.x * 0.5f); tileHighlightGameObject.transform.localScale = new Vector3(_world.Size.x, 1, _world.Size.y); } public void Update() { _renderingState.TileHighlight.Apply(); } } }