1
This commit is contained in:
141
Assets/Artists/Scripts/UX/UXGunModify.cs
Normal file
141
Assets/Artists/Scripts/UX/UXGunModify.cs
Normal file
@@ -0,0 +1,141 @@
|
||||
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.UX;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace BITFALL.UX
|
||||
{
|
||||
public class UXGunModify : MonoBehaviour
|
||||
{
|
||||
[SerializeReference, SubclassSelector] private IPlayerService playerService;
|
||||
private VisualElement _container => UXInventoryInspector.Container;
|
||||
|
||||
[Inject] private IEntityInventory _inventory;
|
||||
[Inject] private IEntityEquipment _equipment;
|
||||
[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<GunModify>(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 (pair.Value is AssetableItem modifyItem)
|
||||
{
|
||||
dropdown.SetValueWithoutNotify(modifyItem.Name);
|
||||
}
|
||||
|
||||
if (pair.Value is not null)
|
||||
{
|
||||
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 attachments = (
|
||||
from item in playerItems
|
||||
where item.GetAssetable() is AssetableAttachment attachment &&
|
||||
attachment.Type.GetType().IsInstanceOfType(pair.Key)
|
||||
select item
|
||||
).ToArray();
|
||||
|
||||
if (attachments.Length is 0 && pair.Value is null)
|
||||
{
|
||||
dropdown.SetValueWithoutNotify("无可用配件");
|
||||
dropdown.SetEnabled(false);
|
||||
}
|
||||
|
||||
dropdown.choices = attachments.Select(item => item.Name).ToList();
|
||||
|
||||
var nextAttachmentList = attachments.ToList();
|
||||
|
||||
dropdown.RegisterValueChangedCallback(x =>
|
||||
{
|
||||
var next = nextAttachmentList[dropdown.index];
|
||||
if (_inventory.Remove(next))
|
||||
{
|
||||
var nextAsset = next.GetAssetable();
|
||||
modify.Add(pair.Key, nextAsset);
|
||||
_popup.Popup($"配件{nextAsset.name}已装备在{obj.Name}");
|
||||
}
|
||||
|
||||
UXInventoryInspector.Entry(obj);
|
||||
RebuildPlayerLayout();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
private async void RebuildPlayerLayout()
|
||||
{
|
||||
_equipService.AllowEquip.AddDisableElements(this);
|
||||
await Task.Delay(100, destroyCancellationToken);
|
||||
_equipService.AllowEquip.RemoveDisableElements(this);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user