using System; using BITFALL.Entities.Equipment; using BITFALL.Entities.Inventory; using BITFALL.Player.Movement; using UnityEngine; using BITKit; using BITKit.Entities; using BITKit.UX; namespace BITFALL { public interface IPlayerInventoryWeightable { event Action 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 IEntityMovement _movement; [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(out var itemWeight)) return true; if (currentWeight + itemWeight > maxWeight) { _popup.Popup("背包已满,无法奔跑"); } return true; } private void OnAdd(IBasicItem item) { var asset = item.GetAssetable(); if (!asset.TryGetProperty(out var itemWeight)) return; currentWeight += itemWeight; InvokeCallback(); } private void OnRemove(IBasicItem item) { var asset = item.GetAssetable(); if (!asset.TryGetProperty(out var itemWeight)) return; currentWeight -= itemWeight; InvokeCallback(); } private void DeEquip(IEquipmentSlot slot, IBasicItem item) { if (!item.GetAssetable().TryGetProperty(out var addWeight)) return; maxWeight -= addWeight.AddWeight; InvokeCallback(); } private void OnEquip(IEquipmentSlot slot, IBasicItem item) { if (!item.GetAssetable().TryGetProperty(out var addWeight)) return; maxWeight += addWeight.AddWeight; InvokeCallback(); } private void InvokeCallback() { OnWeighted?.Invoke(currentWeight, maxWeight); _movement?.ExecuteCommand(new PlayerPauseRunCommand(this, currentWeight > maxWeight)); } public event Action OnWeighted; } }