riversong code showcase
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using Unity.Collections;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class TileHighlight : IDisposable
|
||||
{
|
||||
private NativeArray<Color32> _clear;
|
||||
|
||||
private NativeGrid<Color32> _grid;
|
||||
|
||||
public Texture2D Texture { get; private set; }
|
||||
|
||||
public void Create(int2 size)
|
||||
{
|
||||
Texture = new Texture2D(size.x, size.y, TextureFormat.RGBA32, false);
|
||||
|
||||
_clear = new NativeArray<Color32>(size.x * size.y, Allocator.Persistent);
|
||||
_grid = new NativeGrid<Color32>(size, Allocator.Persistent);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Texture)
|
||||
{
|
||||
Object.Destroy(Texture);
|
||||
Texture = null;
|
||||
}
|
||||
|
||||
if (_clear.IsCreated)
|
||||
{
|
||||
_clear.Dispose();
|
||||
_clear = default;
|
||||
}
|
||||
|
||||
if (_grid != null)
|
||||
{
|
||||
_grid.Dispose();
|
||||
_grid = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Apply()
|
||||
{
|
||||
var dataArray = _grid.GetNativeArray();
|
||||
|
||||
Texture.SetPixelData(dataArray, 0);
|
||||
Texture.Apply();
|
||||
|
||||
NativeArray<Color32>.Copy(_clear, dataArray);
|
||||
}
|
||||
|
||||
public void AddTile(int2 tile, Color32 color)
|
||||
{
|
||||
_grid.SetValue(tile, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public enum TileHighlightType
|
||||
{
|
||||
ValidTile,
|
||||
|
||||
InvalidTile,
|
||||
|
||||
DeletePreview
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user