using System.Collections; using System.Collections.Generic; using UnityEngine; using BITKit; using BITKit.Entities; using UnityEngine.AddressableAssets; namespace BITFALL { public interface IWeightedCallback { public void OnWeighted(float current, float max); } public class InventoryWeightable : EntityComponent,IPlayerEquipCallback { [Header(Constant.Header.Data)] public float currentWeight; [Header(Constant.Header.Settings)] public float maxWeight =8; [Header(Constant.Header.InternalVariables)] IBasicItemContainer container; public override void OnStart() { base.OnStart(); container = entity.Get(); container.AddFactory += AddFactory; container.OnAdd += OnAdd; container.OnRemove += OnRemove; entity.RegisterCallback(this); } bool AddFactory(IBasicItem item) { var asset = item.GetAssetable(); if (asset.TryGetProperty(out var itemWeight)) { if (currentWeight + itemWeight <= maxWeight) { } else if(currentWeight+ itemWeight > maxWeight) { return false; } } return true; } void OnAdd(IBasicItem item) { var asset = item.GetAssetable(); if (asset.TryGetProperty(out var itemWeight)) { currentWeight += itemWeight; InvokeCallback(); } } void OnRemove(IBasicItem item) { var asset = item.GetAssetable(); if (asset.TryGetProperty(out var itemWeight)) { currentWeight -= itemWeight; InvokeCallback(); } } public void DeEquip(IEquipmentSlot slot, IBasicItem item) { if (item.GetAssetable().TryGetProperty(out var addWeight)) { maxWeight -= addWeight.AddWeight; InvokeCallback(); } } public void OnEquip(IEquipmentSlot slot, IBasicItem item) { if (item.GetAssetable().TryGetProperty(out var addWeight)) { maxWeight += addWeight.AddWeight; InvokeCallback(); } } void InvokeCallback() { foreach (var x in entity.GetCallbacks()) { x.OnWeighted(currentWeight, maxWeight); } } } }