Files
2026-05-21 16:04:49 +02:00

73 lines
2.4 KiB
C#

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<WorldGenerationCompletedSignal>(OnWorldGenerationCompleted);
return UniTask.CompletedTask;
}
public void Dispose()
{
_signalBus.Unsubscribe<WorldGenerationCompletedSignal>(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<GameObject>();
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();
}
}
}