riversong code showcase
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class BuildingBadgeUIController : UIControllerSystem, IInitializable, IDisposable, IUpdatable
|
||||
{
|
||||
[InjectService]
|
||||
private ISignalBus _signalBus;
|
||||
|
||||
[InjectService]
|
||||
private IWorldUIService _worldUIService;
|
||||
|
||||
[InjectService]
|
||||
private ICameraProperties _cameraProperties;
|
||||
|
||||
[InjectService]
|
||||
private GameConfig _gameConfig;
|
||||
|
||||
[InjectService]
|
||||
private World _world;
|
||||
|
||||
private VisualElement _container;
|
||||
|
||||
private readonly Dictionary<int, BadgeEntry> _entries = new();
|
||||
|
||||
public BuildingBadgeUIController(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
public override async UniTask InitializeAsync()
|
||||
{
|
||||
await base.InitializeAsync();
|
||||
|
||||
_container = UIRoot.RootVisualElement.Q(className: "world-ui-layer");
|
||||
|
||||
_signalBus.Subscribe<BuildingVisualizationCreatedSignal>(OnBuildingVisualizationCreated);
|
||||
_signalBus.Subscribe<BuildingDeletedSignal>(OnBuildingDeleted);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_signalBus.Unsubscribe<BuildingVisualizationCreatedSignal>(OnBuildingVisualizationCreated);
|
||||
_signalBus.Unsubscribe<BuildingDeletedSignal>(OnBuildingDeleted);
|
||||
}
|
||||
|
||||
private void OnBuildingVisualizationCreated(BuildingVisualizationCreatedSignal signal)
|
||||
{
|
||||
var building = signal.Building;
|
||||
if (building.Definition.WorkerCount <= 0) return;
|
||||
|
||||
CreateBadgeAsync(building, signal.Visualization).Forget();
|
||||
}
|
||||
|
||||
private async UniTask CreateBadgeAsync(Building building, BuildingVisualization visualization)
|
||||
{
|
||||
var element = UIService.TemplateLibrary.WorldUI.Badge.CloneTree();
|
||||
_container.Add(element);
|
||||
|
||||
var view = (BuildingBadgeUIView)await UIService.CreateView(typeof(BuildingBadgeUIView), element);
|
||||
view.RootElement.SetPickingModeRecursive(PickingMode.Ignore);
|
||||
view.Show(false);
|
||||
|
||||
_worldUIService.Register(view.RootElement, visualization.transform);
|
||||
|
||||
_entries.Add(building.Id, new BadgeEntry(building, view));
|
||||
}
|
||||
|
||||
private void OnBuildingDeleted(BuildingDeletedSignal signal)
|
||||
{
|
||||
if (!_entries.Remove(signal.Building.Id, out var entry)) return;
|
||||
|
||||
_worldUIService.Unregister(entry.View.RootElement);
|
||||
entry.View.RootElement.RemoveFromHierarchy();
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
var zoomRange = _gameConfig.Camera.ZoomRange;
|
||||
var config = UIService.TemplateLibrary.WorldUI;
|
||||
|
||||
var normalizedZoom = Mathf.InverseLerp(zoomRange.x, config.BadgeZoomCullThreshold, _cameraProperties.Zoom);
|
||||
var offset = Mathf.Lerp(config.BadgeYOffset.x, config.BadgeYOffset.y, normalizedZoom);
|
||||
var scale = Mathf.Lerp(config.BadgeScale.x, config.BadgeScale.y, normalizedZoom);
|
||||
|
||||
var culledByZoom = _cameraProperties.Zoom > config.BadgeZoomCullThreshold;
|
||||
UpdateBadges(culledByZoom);
|
||||
|
||||
foreach (var entry in _entries.Values)
|
||||
{
|
||||
entry.View.RootElement.SetScale(scale);
|
||||
|
||||
_worldUIService.SetWorldOffset(entry.View.RootElement, offset * Vector3.up);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateBadges(bool culledByZoom)
|
||||
{
|
||||
foreach (var entry in _entries.Values)
|
||||
{
|
||||
ref var sleepState = ref entry.Building.GetSleepStateRW();
|
||||
|
||||
var show = !culledByZoom && _world.TimeState.DayNightCycleStep == DayNightCycleStep.Day && sleepState.HasHomelessWorkers;
|
||||
|
||||
entry.View.Show(show);
|
||||
}
|
||||
}
|
||||
|
||||
private readonly struct BadgeEntry
|
||||
{
|
||||
public readonly Building Building;
|
||||
|
||||
public readonly BuildingBadgeUIView View;
|
||||
|
||||
public BadgeEntry(Building building, BuildingBadgeUIView view)
|
||||
{
|
||||
Building = building;
|
||||
View = view;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public class BuildingBadgeUIView : UIView
|
||||
{
|
||||
}
|
||||
}
|
||||
14
Source/Riversong/Game/UI/Panels/WorldUI/IWorldUIService.cs
Normal file
14
Source/Riversong/Game/UI/Panels/WorldUI/IWorldUIService.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
public interface IWorldUIService
|
||||
{
|
||||
void Register(VisualElement element, Transform target);
|
||||
|
||||
void Unregister(VisualElement element);
|
||||
|
||||
void SetWorldOffset(VisualElement element, Vector3 offset);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Pool;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace DanieleMarotta.RiversongCodeShowcase
|
||||
{
|
||||
[Service(typeof(IWorldUIService))]
|
||||
[GameSystemGroup(typeof(UISystemGroup))]
|
||||
public class WorldUITrackingSystem : GameSystem, IUpdatable, IWorldUIService
|
||||
{
|
||||
[InjectService]
|
||||
private IScene _scene;
|
||||
|
||||
private readonly Dictionary<VisualElement, TrackedElement> _trackedElements = new();
|
||||
|
||||
public WorldUITrackingSystem(IServiceLocator serviceLocator) : base(serviceLocator)
|
||||
{
|
||||
}
|
||||
|
||||
public void Register(VisualElement element, Transform target)
|
||||
{
|
||||
_trackedElements[element] = new TrackedElement { Target = target };
|
||||
}
|
||||
|
||||
public void Unregister(VisualElement element)
|
||||
{
|
||||
_trackedElements.Remove(element);
|
||||
}
|
||||
|
||||
public void SetWorldOffset(VisualElement element, Vector3 offset)
|
||||
{
|
||||
var trackedElement = _trackedElements[element];
|
||||
trackedElement.Offset = offset;
|
||||
_trackedElements[element] = trackedElement;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (_trackedElements.Count == 0) return;
|
||||
|
||||
using var invalidScope = ListPool<VisualElement>.Get(out var invalidElements);
|
||||
var camera = _scene.MainCamera;
|
||||
|
||||
foreach (var (element, trackedElement) in _trackedElements)
|
||||
{
|
||||
var target = trackedElement.Target;
|
||||
if (element.panel == null || target == null)
|
||||
{
|
||||
invalidElements.Add(element);
|
||||
continue;
|
||||
}
|
||||
|
||||
var screenPoint = camera.WorldToScreenPoint(target.position + trackedElement.Offset);
|
||||
var isVisible = screenPoint.x >= 0 && screenPoint.x <= Screen.width && screenPoint.y >= 0 && screenPoint.y <= Screen.height && screenPoint.z > 0;
|
||||
|
||||
element.style.visibility = isVisible ? Visibility.Visible : Visibility.Hidden;
|
||||
if (isVisible) element.SetAbsoluteScreenPosition(screenPoint);
|
||||
}
|
||||
|
||||
foreach (var invalidElement in invalidElements) _trackedElements.Remove(invalidElement);
|
||||
}
|
||||
|
||||
private struct TrackedElement
|
||||
{
|
||||
public Transform Target;
|
||||
|
||||
public Vector3 Offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user