98 lines
3.0 KiB
C#
98 lines
3.0 KiB
C#
using System;
|
|
using BITFALL.Entities.Equipment;
|
|
using BITFALL.Entities.Inventory;
|
|
using UnityEngine;
|
|
using BITKit;
|
|
using BITKit.Entities;
|
|
using BITKit.UX;
|
|
|
|
namespace BITFALL
|
|
{
|
|
public interface IPlayerInventoryWeightable
|
|
{
|
|
event Action<double,double> OnWeighted;
|
|
}
|
|
[CustomType(typeof(IPlayerInventoryWeightable))]
|
|
public class InventoryWeightable : EntityBehavior,IPlayerInventoryWeightable
|
|
{
|
|
[Header(Constant.Header.Data)]
|
|
public double currentWeight;
|
|
[Header(Constant.Header.Settings)]
|
|
public double maxWeight =8;
|
|
|
|
[Header(Constant.Header.InternalVariables)]
|
|
[Inject]
|
|
private IBasicItemContainer _container;
|
|
[Inject(true)]
|
|
private IEntityInventory _inventory;
|
|
[Inject(true)]
|
|
private IEntityEquipmentContainer playerEquipContainer;
|
|
|
|
[Inject(true)] private IUXPopup _popup;
|
|
public override void OnStart()
|
|
{
|
|
_container.AddFactory += AddFactory;
|
|
_container.OnAdd += OnAdd;
|
|
_container.OnRemove += OnRemove;
|
|
|
|
if (_inventory is not null)
|
|
{
|
|
_inventory.OnUsedItem += OnRemove;
|
|
}
|
|
if (playerEquipContainer is not null)
|
|
{
|
|
playerEquipContainer.OnEquip += OnEquip;
|
|
playerEquipContainer.OnDeEquip += DeEquip;
|
|
}
|
|
}
|
|
|
|
private bool AddFactory(IBasicItem item)
|
|
{
|
|
var asset = item.GetAssetable();
|
|
if (!asset.TryGetProperty<ItemWeight>(out var itemWeight)) return true;
|
|
|
|
var result = currentWeight + itemWeight <= maxWeight;
|
|
if (result is false)
|
|
{
|
|
_popup.Popup("<color=yellow>背包已满</color>");
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private void OnAdd(IBasicItem item)
|
|
{
|
|
var asset = item.GetAssetable();
|
|
if (!asset.TryGetProperty<ItemWeight>(out var itemWeight)) return;
|
|
currentWeight += itemWeight;
|
|
InvokeCallback();
|
|
}
|
|
private void OnRemove(IBasicItem item)
|
|
{
|
|
var asset = item.GetAssetable();
|
|
if (!asset.TryGetProperty<ItemWeight>(out var itemWeight)) return;
|
|
currentWeight -= itemWeight;
|
|
InvokeCallback();
|
|
|
|
}
|
|
|
|
private void DeEquip(IEquipmentSlot slot, IBasicItem item)
|
|
{
|
|
if (!item.GetAssetable().TryGetProperty<AddInventoryMaxWeight>(out var addWeight)) return;
|
|
maxWeight -= addWeight.AddWeight;
|
|
InvokeCallback();
|
|
}
|
|
|
|
private void OnEquip(IEquipmentSlot slot, IBasicItem item)
|
|
{
|
|
if (!item.GetAssetable().TryGetProperty<AddInventoryMaxWeight>(out var addWeight)) return;
|
|
maxWeight += addWeight.AddWeight;
|
|
InvokeCallback();
|
|
}
|
|
private void InvokeCallback()
|
|
{
|
|
OnWeighted?.Invoke(currentWeight, maxWeight);
|
|
}
|
|
public event Action<double,double> OnWeighted;
|
|
}
|
|
} |