70 lines
2.0 KiB
C#
70 lines
2.0 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Concurrent;
|
||
|
using System.Collections.Generic;
|
||
|
using BITFALL.Player.Medical;
|
||
|
using BITKit;
|
||
|
using BITKit.Entities;
|
||
|
using BITKit.Entities.Player;
|
||
|
using BITKit.UX;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UIElements;
|
||
|
|
||
|
namespace BITFALL.UX
|
||
|
{
|
||
|
public class UXMedical : MonoBehaviour
|
||
|
{
|
||
|
[SerializeReference,SubclassSelector] private IPlayerService _playerService;
|
||
|
[SerializeField] private VisualTreeAsset _woundTemplate;
|
||
|
[Inject] private IPlayerMedical _playerMedical;
|
||
|
[UXBindPath("medical-container")]
|
||
|
private VisualElement _woundContainer;
|
||
|
private readonly ConcurrentDictionary<IPlayerMedicalWoundType, UXContainer> _dictionary = new();
|
||
|
private void Start()
|
||
|
{
|
||
|
BITKit.UX.UXUtils.Inject(this);
|
||
|
_woundContainer.Clear();
|
||
|
_playerService.OnPlayerInitialized += OnPlayerInitialized;
|
||
|
_playerService.OnPlayerDisposed += OnPlayerDisposed;
|
||
|
destroyCancellationToken.Register(() =>
|
||
|
{
|
||
|
_playerService.OnPlayerInitialized -= OnPlayerInitialized;
|
||
|
_playerService.OnPlayerDisposed -= OnPlayerDisposed;
|
||
|
});
|
||
|
}
|
||
|
private void OnPlayerInitialized(Entity obj)
|
||
|
{
|
||
|
obj.Inject(this);
|
||
|
_playerMedical.OnWoundChanged += OnWoundChanged;
|
||
|
_playerMedical.OnWoundAdded += OnWoundChanged;
|
||
|
_playerMedical.OnWoundRemoved += OnWoundRemoved;
|
||
|
}
|
||
|
private void OnWoundRemoved(IPlayerMedicalWoundType arg1, IPlayerMedicalWound arg2)
|
||
|
{
|
||
|
if(_dictionary.TryRemove(arg1, out var element))
|
||
|
element.visualElement.RemoveFromHierarchy();
|
||
|
}
|
||
|
private void OnWoundChanged(IPlayerMedicalWoundType arg1, IPlayerMedicalWound arg2)
|
||
|
{
|
||
|
var element = _dictionary.GetOrAdd(arg1, Create);
|
||
|
element.contextLabel.text = arg1.DisplayName;
|
||
|
}
|
||
|
private UXContainer Create(IPlayerMedicalWoundType woundType)
|
||
|
{
|
||
|
var element = new UXContainer(_woundContainer.Create(_woundTemplate))
|
||
|
{
|
||
|
contextLabel =
|
||
|
{
|
||
|
text = woundType.DisplayName
|
||
|
}
|
||
|
};
|
||
|
return element;
|
||
|
}
|
||
|
private void OnPlayerDisposed(Entity obj)
|
||
|
{
|
||
|
InjectAttribute.Clear(this);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|