riversong code showcase
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class StorageTooltipController : UIControllerSystem<StorageTooltipUIView>, IUpdatable
|
||||
{
|
||||
[InjectService]
|
||||
private IPointerService _pointerService;
|
||||
|
||||
[InjectService]
|
||||
private ITileSpace _tileSpace;
|
||||
|
||||
[InjectService]
|
||||
private World _world;
|
||||
|
||||
[InjectService]
|
||||
private IEntityCollection _entityCollection;
|
||||
|
||||
[InjectService]
|
||||
private IProductCatalog _productCatalog;
|
||||
|
||||
[InjectService]
|
||||
private IScene _scene;
|
||||
|
||||
[InjectService]
|
||||
private IEditingService _editingService;
|
||||
|
||||
private List<IProductAmount> _products = new();
|
||||
|
||||
public StorageTooltipController(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
protected override StorageTooltipUIView View => UIRoot.GetView<StorageTooltipUIView>();
|
||||
|
||||
public override UniTask InitializeAsync()
|
||||
{
|
||||
for (var i = 0; i < _productCatalog.ProductTypeCount; i++) _products.Add(new ProductAmountAuthoring());
|
||||
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (_editingService.EditingState.ActiveTool != null)
|
||||
{
|
||||
View.Show(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_pointerService.TryGetPositionOnTerrain(out var pointer))
|
||||
{
|
||||
View.Show(false);
|
||||
return;
|
||||
}
|
||||
|
||||
var tile = _tileSpace.WorldToTile(pointer);
|
||||
if (!TryGetStorageEntityId(tile, out var storageEntityId, out var storageEntityRect))
|
||||
{
|
||||
View.Show(false);
|
||||
return;
|
||||
}
|
||||
|
||||
IProductStorageEntity storageEntity;
|
||||
switch (_entityCollection.Get(storageEntityId))
|
||||
{
|
||||
case Building building when building.Definition.ShowStorageTooltip:
|
||||
storageEntity = building;
|
||||
break;
|
||||
case ProductStack productStack:
|
||||
storageEntity = productStack;
|
||||
break;
|
||||
default:
|
||||
View.Show(false);
|
||||
return;
|
||||
}
|
||||
|
||||
ref var storage = ref storageEntity.GetStorageRW();
|
||||
if (storage.TotalCount <= 0)
|
||||
{
|
||||
View.Show(false);
|
||||
return;
|
||||
}
|
||||
|
||||
var count = 0;
|
||||
foreach (var (handle, amount) in storage)
|
||||
{
|
||||
var productAmount = _products[count++];
|
||||
productAmount.Product = _productCatalog.GetProduct(handle);
|
||||
productAmount.Amount = amount;
|
||||
}
|
||||
|
||||
View.SetProducts(_products, count);
|
||||
View.Show(true);
|
||||
|
||||
var worldCenter = _tileSpace.GetRectWorldCenter(storageEntityRect);
|
||||
var screenPoint = _scene.MainCamera.WorldToScreenPoint(worldCenter);
|
||||
View.RootElement.SetAbsoluteScreenPosition(screenPoint);
|
||||
}
|
||||
|
||||
private bool TryGetStorageEntityId(int2 tile, out int id, out TileRect rect)
|
||||
{
|
||||
ref var entityIdValue = ref _world.EntityIdMap.GetValueRW(tile);
|
||||
if (entityIdValue.BuildingId != Entity.InvalidId)
|
||||
{
|
||||
id = entityIdValue.BuildingId;
|
||||
rect = ((IBuildingShape)_entityCollection.Get<Entity>(id)).Rect;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_world.ProductStacks.At(tile, out var productStack))
|
||||
{
|
||||
id = productStack.Id;
|
||||
rect = TileRect.OneTile(tile);
|
||||
return true;
|
||||
}
|
||||
|
||||
id = Entity.InvalidId;
|
||||
rect = TileRect.Empty;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
[UIView("storage-tooltip")]
|
||||
public class StorageTooltipUIView : UIView
|
||||
{
|
||||
private VisualElement _products;
|
||||
|
||||
public override async UniTask InitializeAsync(UIService uiService, VisualElement rootElement)
|
||||
{
|
||||
await base.InitializeAsync(uiService, rootElement);
|
||||
|
||||
_products = rootElement.Q(className: "storage-tooltip__products");
|
||||
}
|
||||
|
||||
public void SetProducts(List<IProductAmount> source, int count)
|
||||
{
|
||||
_products.Clear();
|
||||
|
||||
var productTemplate = UIService.TemplateLibrary.Common.ProductAmount;
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var productAmount = source[i];
|
||||
|
||||
var element = productTemplate.CloneTree();
|
||||
element.dataSource = productAmount;
|
||||
|
||||
_products.Add(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user