BITFALL/Assets/Artists/Scripts/UX/UXArmor.cs

108 lines
2.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using BITFALL.Entities.Armor;
using BITFALL.Items.Armor;
using BITKit;
using BITKit.Entities;
using BITKit.Entities.Player;
using BITKit.UX;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITFALL.UX
{
public class UXArmor : MonoBehaviour
{
[SerializeReference,SubclassSelector] private IPlayerService playerService;
[SerializeField] private VisualTreeAsset armorTemplate;
[Inject] private IArmor _armor;
[UXBindPath("armor-container")]
private VisualElement _armorContainer;
[UXBindPath("armor-name")]
private Label _armorName;
[UXBindPath("armor-icon")]
private VisualElement _armorIcon;
[UXBindPath("armor-hp")]
private Label _armorHP;
[UXBindPath("armor-usage")]
private Label _armorUsage;
private void Start()
{
playerService.OnPlayerInitialized += OnPlayerInitialized;
playerService.OnPlayerDisposed += OnPlayerDisposed;
destroyCancellationToken.Register(() =>
{
playerService.OnPlayerInitialized -= OnPlayerInitialized;
playerService.OnPlayerDisposed -= OnPlayerDisposed;
});
BITKit.UX.UXUtils.Inject(this);
}
private void OnPlayerDisposed(Entity obj)
{
InjectAttribute.Clear(this);
}
private void OnPlayerInitialized(Entity obj)
{
obj.Inject(this);
_armor.OnPlatesChanged += OnPlatesChanged;
_armor.OnEquipArmor += OnEquipArmor;
_armor.OnUnEquipArmor += OnUnEquipArmor;
_armor.OnArmorChanged += OnArmorChanged;
OnPlatesChanged(_armor.Plates);
OnUnEquipArmor(null);
}
private void OnArmorChanged(int obj)
{
_armorHP.text = $"{obj}HP";
}
private void OnUnEquipArmor(IBasicItem obj)
{
_armorIcon.style.backgroundImage = null;
_armorName.text = "未装备护甲";
}
private void OnEquipArmor(IBasicItem obj)
{
_armorIcon.style.backgroundImage = obj.GetAssetable().SquareIcon;
_armorName.text = obj.Name;
}
private void OnPlatesChanged(IBasicItem[] obj)
{
_armorContainer.Clear();
_armorUsage.text = $"{_armor.Plates.Length}/{_armor.PlateCapacity}";
for (var index = 0; index < obj.Length; index++)
{
var currentIndex = index;
var x = obj[index];
var armor = x.GetOrCreateProperty<AsArmor>();
var container = new UXContainer(_armorContainer.Create(armorTemplate));
container.Get<Label>(0).text = x.Name;
container.Get<Label>(1).text = $"{armor.CurrentPoint}/{armor.MaxPoint}";
container.icon.style.backgroundImage = x.GetAssetable().SquareIcon;
container.SetProcess((float)armor.CurrentPoint / armor.MaxPoint);
container.visualElement.Q(UXConstant.ProcessBarFill).style.backgroundColor =
ScriptableItem.GetQualityColor(x.Quality);
container.button.clicked += Clicked;
continue;
void Clicked()
{
_armor.UninstallArmor(currentIndex);
}
}
}
}
}