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

136 lines
3.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BITFALL.Entities.Equipment;
using BITFALL.Entities.Inventory;
using BITFALL.Guns.Modify;
using BITFALL.Items;
using BITFALL.Player.Equip;
using BITKit;
using BITKit.Entities;
using BITKit.Entities.Player;
using BITKit.Modification;
using BITKit.UX;
using UnityEngine;
using UnityEngine.UIElements;
namespace BITFALL.UX
{
public class UXItemModify : MonoBehaviour
{
[SerializeReference, SubclassSelector] private IPlayerService playerService;
private VisualElement _container => UXInventoryInspector.Container;
[Inject] private IEntityInventory _inventory;
[Inject] private IEquipService _equipService;
[Inject] private IUXPopup _popup;
private void Start()
{
UXInventoryInspector.OnInspect += OnInspect;
playerService.OnPlayerInitialized += OnPlayerInitialized;
destroyCancellationToken.Register(() =>
{
UXInventoryInspector.OnInspect -= OnInspect;
playerService.OnPlayerInitialized -= OnPlayerInitialized;
});
}
private void OnPlayerInitialized(Entity obj)
{
obj.Inject(this);
}
private void OnInspect(IBasicItem obj)
{
if (_inventory is null)
{
Debug.LogWarning("Inventory is null");
return;
}
if (obj is null) return;
var assetable = obj.GetAssetable();
if (obj.TryGetProperty<IModifyManager>(out var modify) is false) return;
var playerItems = _inventory.GetItems();
foreach (var pair in modify.Modifies)
{
var container = _container.Create<VisualElement>();
var dropdown = container.Create<DropdownField>();
container.style.flexDirection = FlexDirection.Row;
dropdown.style.flexGrow = 1;
dropdown.label = pair.Key.GetType().Name;
if (modify.Modified.TryGetValue(pair.Key,out var value) && value is IBasicItem modifyItem)
{
dropdown.SetValueWithoutNotify(modifyItem.Name);
dropdown.SetEnabled(false);
var button = container.Create<Button>();
button.text = "卸下";
button.clicked += () =>
{
modify.Remove(pair.Key, out var value);
var copy = value.As<ICloneable>().Clone().As<IBasicItem>();
if (_inventory.Add(copy))
{
_popup.Popup("配件已返回背包");
}
else if (_inventory.DropOrSpawn(copy))
{
_popup.Popup("背包可能已满,配件已掉落");
}
UXInventoryInspector.Entry(obj);
RebuildPlayerLayout();
};
}
else
{
var components = (
from item in playerItems
where item.GetAssetable() is ScriptableIndustryComponent attachment
&& attachment.Type.GetType().IsInstanceOfType(pair.Key)
select item
).ToArray();
if (components.Length is 0 && pair.Value is null)
{
dropdown.SetValueWithoutNotify("无可用配件");
dropdown.SetEnabled(false);
}
dropdown.choices = components.Select(item => item.Name).ToList();
var nextAttachmentList = components.ToList();
dropdown.RegisterValueChangedCallback(x =>
{
var next = nextAttachmentList[dropdown.index];
if (_inventory.Remove(next))
{
modify.Add(pair.Key, next);
_popup.Popup($"配件{next.Name}已装备在{obj.Name}");
}
UXInventoryInspector.Entry(obj);
RebuildPlayerLayout();
});
}
}
}
private async void RebuildPlayerLayout()
{
_equipService.AllowEquip.AddDisableElements(this);
await Task.Delay(100, destroyCancellationToken);
_equipService.AllowEquip.RemoveDisableElements(this);
}
}
}