BITFALL/Assets/Artists/Scripts/Entities/Inventory/InventoryWeightable.cs

98 lines
3.0 KiB
C#
Raw Normal View History

2023-09-01 14:33:54 +08:00
using System;
2023-10-20 22:46:14 +08:00
using BITFALL.Entities.Equipment;
2023-10-24 23:37:59 +08:00
using BITFALL.Entities.Inventory;
2023-06-08 14:09:50 +08:00
using UnityEngine;
using BITKit;
using BITKit.Entities;
2023-12-03 17:35:43 +08:00
using BITKit.UX;
2023-06-08 14:09:50 +08:00
namespace BITFALL
{
2023-09-01 14:33:54 +08:00
public interface IPlayerInventoryWeightable
2023-06-08 14:09:50 +08:00
{
2023-09-01 14:33:54 +08:00
event Action<double,double> OnWeighted;
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
[CustomType(typeof(IPlayerInventoryWeightable))]
2023-10-30 01:25:53 +08:00
public class InventoryWeightable : EntityBehavior,IPlayerInventoryWeightable
2023-06-08 14:09:50 +08:00
{
[Header(Constant.Header.Data)]
2023-09-01 14:33:54 +08:00
public double currentWeight;
2023-06-08 14:09:50 +08:00
[Header(Constant.Header.Settings)]
2023-09-01 14:33:54 +08:00
public double maxWeight =8;
2023-10-24 23:37:59 +08:00
2023-06-08 14:09:50 +08:00
[Header(Constant.Header.InternalVariables)]
2023-10-24 23:37:59 +08:00
[Inject]
private IBasicItemContainer _container;
[Inject(true)]
private IEntityInventory _inventory;
[Inject(true)]
2023-12-03 17:35:43 +08:00
private IEntityEquipmentContainer playerEquipContainer;
[Inject(true)] private IUXPopup _popup;
2023-06-08 14:09:50 +08:00
public override void OnStart()
{
2023-10-24 23:37:59 +08:00
_container.AddFactory += AddFactory;
_container.OnAdd += OnAdd;
_container.OnRemove += OnRemove;
2023-09-01 14:33:54 +08:00
2023-10-24 23:37:59 +08:00
if (_inventory is not null)
{
_inventory.OnUsedItem += OnRemove;
}
if (playerEquipContainer is not null)
{
playerEquipContainer.OnEquip += OnEquip;
playerEquipContainer.OnDeEquip += DeEquip;
}
2023-06-08 14:09:50 +08:00
}
2023-08-23 01:59:40 +08:00
private bool AddFactory(IBasicItem item)
2023-06-08 14:09:50 +08:00
{
2023-08-23 01:59:40 +08:00
var asset = item.GetAssetable();
2023-09-01 14:33:54 +08:00
if (!asset.TryGetProperty<ItemWeight>(out var itemWeight)) return true;
2023-12-03 17:35:43 +08:00
var result = currentWeight + itemWeight <= maxWeight;
if (result is false)
{
_popup.Popup("<color=yellow>背包已满</color>");
}
return result;
2023-06-08 14:09:50 +08:00
}
2023-08-23 01:59:40 +08:00
private void OnAdd(IBasicItem item)
2023-06-08 14:09:50 +08:00
{
var asset = item.GetAssetable();
2023-09-01 14:33:54 +08:00
if (!asset.TryGetProperty<ItemWeight>(out var itemWeight)) return;
currentWeight += itemWeight;
InvokeCallback();
2023-06-08 14:09:50 +08:00
}
2023-08-23 01:59:40 +08:00
private void OnRemove(IBasicItem item)
2023-06-08 14:09:50 +08:00
{
var asset = item.GetAssetable();
2023-08-23 01:59:40 +08:00
if (!asset.TryGetProperty<ItemWeight>(out var itemWeight)) return;
currentWeight -= itemWeight;
InvokeCallback();
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
private void DeEquip(IEquipmentSlot slot, IBasicItem item)
2023-06-08 14:09:50 +08:00
{
2023-09-01 14:33:54 +08:00
if (!item.GetAssetable().TryGetProperty<AddInventoryMaxWeight>(out var addWeight)) return;
maxWeight -= addWeight.AddWeight;
InvokeCallback();
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
private void OnEquip(IEquipmentSlot slot, IBasicItem item)
2023-06-08 14:09:50 +08:00
{
2023-09-01 14:33:54 +08:00
if (!item.GetAssetable().TryGetProperty<AddInventoryMaxWeight>(out var addWeight)) return;
maxWeight += addWeight.AddWeight;
InvokeCallback();
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
private void InvokeCallback()
2023-06-08 14:09:50 +08:00
{
2023-09-01 14:33:54 +08:00
OnWeighted?.Invoke(currentWeight, maxWeight);
2023-06-08 14:09:50 +08:00
}
2023-09-01 14:33:54 +08:00
public event Action<double,double> OnWeighted;
2023-06-08 14:09:50 +08:00
}
}