/// ---------------------------------------------
/// Ultimate Inventory System
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.UltimateInventorySystem.Core.InventoryCollections
{
using Opsive.Shared.Utility;
using Opsive.UltimateInventorySystem.Core.DataStructures;
using System;
using UnityEngine;
[Serializable]
public class ItemCollectionWeightRestriction : IItemRestriction
{
[Tooltip("The itemCollections affected by this restriction.")]
[SerializeField] protected string[] m_ItemCollectionNames;
[SerializeField] int m_MaxWeightLimit = 100;
[System.NonSerialized] protected IInventory m_Inventory;
[System.NonSerialized] protected ResizableArray<ItemCollection> m_ItemCollections;
[System.NonSerialized] protected bool m_Initialized;
/// <summary>
/// Initialize with the Inventory.
/// </summary>
/// <param name="inventory">The inventory.</param>
/// <param name="force">Force Initialization.</param>
public void Initialize(IInventory inventory, bool force)
{
if (m_Initialized && !force && inventory == m_Inventory) { return; }
if (m_Inventory != inventory)
{
m_Inventory = inventory;
if (m_ItemCollections == null) { m_ItemCollections = new ResizableArray<ItemCollection>(); }
m_ItemCollections.Clear();
for (int i = 0; i < m_ItemCollectionNames.Length; i++)
{
var match = m_Inventory.GetItemCollection(m_ItemCollectionNames[i]);
if (match == null) { continue; }
m_ItemCollections.Add(match);
}
}
m_Initialized = true;
}
/// <summary>
/// Can the Item be added to the item collection?
/// </summary>
/// <param name="itemInfo">The item to add.</param>
/// <param name="receivingCollection">The item collection the item is added to.</param>
/// <returns>The item that can be added.</returns>
public ItemInfo? CanAddItem(ItemInfo itemInfo, ItemCollection receivingCollection)
{
if (m_ItemCollections.Contains(receivingCollection) == false) { return itemInfo; }
var item = itemInfo.Item;
float currentCollectionWeight = CurrentWeight(receivingCollection);
Debug.Log("Collection weight for " + receivingCollection.Name + " is " + currentCollectionWeight);
float itemWeight = 1;
item.TryGetAttributeValue("Weight", out itemWeight);
if (itemWeight < 1)
{
itemWeight = 1;
Debug.Log("UH OH Weight of " + item.name + " was 0 and changed back to 1");
}
float availableWeight = m_MaxWeightLimit - currentCollectionWeight;
int maxAmountThatCanBeAdded = Mathf.RoundToInt(availableWeight / itemWeight);
int itemAmountThatFits = Mathf.Min(itemInfo.Amount, maxAmountThatCanBeAdded);
Debug.Log("Current weight is " + currentCollectionWeight + " and can only fit " + itemAmountThatFits + " of " + item.name);
if (itemAmountThatFits == 0)
{
return null;
}
return (itemAmountThatFits, itemInfo);
}
float CurrentWeight(ItemCollection itemCollection)
{
float weight = 0;
var stacks = itemCollection.GetAllItemStacks();
Debug.Log("Stack count is "+ stacks.Count);
for (int i = 0; i < stacks.Count; i++)
{
if (stacks[i].Item.TryGetAttributeValue("Weight", out float itemWeight))
{
Debug.Log("Weight of " + stacks[i].Item + " is " + itemWeight);
weight += (itemWeight * stacks[i].Amount);
}
else
{
//Couldnt find a weight for the item, adding a default value of 1 per amount.
weight += stacks[i].Amount;
}
}
return weight;
}
/// <summary>
/// Can the Item be removed.
/// </summary>
/// <param name="itemInfo">The item info.</param>
/// <returns>The item Info.</returns>
public ItemInfo? CanRemoveItem(ItemInfo itemInfo)
{
return itemInfo;
}
/// <summary>
/// Easy to read string.
/// </summary>
/// <returns>String representation.</returns>
public override string ToString()
{
var weightLimit = $" Max Weight Limit {m_MaxWeightLimit}";
if (m_ItemCollectionNames == null || m_ItemCollectionNames.Length == 0)
{
return "EMPTY" + weightLimit;
}
var collectionNames = m_ItemCollectionNames[0];
for (int i = 1; i < m_ItemCollectionNames.Length; i++)
{
collectionNames = ", " + m_ItemCollectionNames[i];
}
return collectionNames + " " + weightLimit;
}
}
}