using System; using UnityEngine; using BITKit; using BITKit.Entities; namespace BITFALL { public interface IPlayerInventoryWeightable { event Action OnWeighted; } [CustomType(typeof(IPlayerInventoryWeightable))] public class InventoryWeightable : EntityComponent,IPlayerInventoryWeightable { [Header(Constant.Header.Data)] public double currentWeight; [Header(Constant.Header.Settings)] public double maxWeight =8; [Header(Constant.Header.InternalVariables)] private IBasicItemContainer container; public override void OnStart() { base.OnStart(); container = entity.Get(); container.AddFactory += AddFactory; container.OnAdd += OnAdd; container.OnUsed += OnRemove; container.OnRemove += OnRemove; var playerEquipContainer = entity.Get(); playerEquipContainer.OnEquip += OnEquip; playerEquipContainer.OnDeEquip += DeEquip; } private bool AddFactory(IBasicItem item) { var asset = item.GetAssetable(); if (!asset.TryGetProperty(out var itemWeight)) return true; return currentWeight + itemWeight <= maxWeight; } 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); } public event Action OnWeighted; } }