Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace FRM.Inventory
- {
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Inventory : IInventory
- {
- [SerializeField] private InventoryTemplate template;
- [SerializeField] private IItem[] inventoryItems;
- public InventoryTemplate Template
- {
- set { template = value; }
- }
- public IItem[] Items
- {
- get { return inventoryItems; }
- set { inventoryItems = value; }
- }
- public int Rows { get { return template.Rows; } }
- public int Columns { get { return template.Columns; } }
- public int Cells { get { return template.Cells; } }
- public int HotbarCells { get { return template.HotbarCells; } }
- public bool UseHotbar { get { return template.UseHotbar; } }
- public Inventory(InventoryTemplate inventoryTemplate)
- {
- template = inventoryTemplate;
- inventoryItems = new IItem[template.Cells];
- }
- // returns null if it transferred fully, if not then it returns the item and the rest of the quantity
- public IItem AddItem(IItem item, int toSlot = -1, int splitQuantity = -1)
- {
- // temporarily store the original item quantity
- int originalQuantity = item.Quantity;
- // if we use item splitting, we need to limit split quantity to the original item quantity just in case
- if (splitQuantity != -1 && splitQuantity != item.Quantity)
- item.Quantity = Mathf.Clamp(splitQuantity, 1, originalQuantity);
- int startIndex = 0;
- if (template.UseHotbar && !item.Info.HotbarPriority)
- startIndex = template.HotbarCells;
- // if its slot specific, go for that slot
- if (toSlot != -1)
- item.Quantity = AddItemToSlot(item, toSlot);
- // first search for the same item and check if you can stack it (if it's stackable)
- if (item.Quantity != 0 && item.Info.MaxStack > 1)
- {
- for (int i = 0; i < inventoryItems.Length; i++)
- {
- if (inventoryItems[i] == null || inventoryItems[i].Info != item.Info)
- continue;
- item.Quantity = AddItemToSlot(item, i);
- if (item.Quantity == 0 || inventoryItems[i].Dragged)
- break;
- }
- }
- // if there is anything left or if its not stackable, try to store the rest it in empty spaces
- int count = inventoryItems.Length;
- if (item.Quantity > 0)
- {
- for (int i = startIndex; i < count; i++)
- {
- item.Quantity = AddItemToSlot(item, i);
- if (item.Quantity == 0 || inventoryItems[i].Dragged)
- break;
- if (i == inventoryItems.Length - 1 && !item.Info.HotbarPriority)
- {
- i = -1;
- count = template.HotbarCells;
- }
- }
- }
- // if there is any leftover of the split quantity transferred, we solve it here
- if (splitQuantity != -1 && splitQuantity != originalQuantity)
- item.Quantity = originalQuantity - splitQuantity + item.Quantity;
- // if we got rid of the item fully, make it null
- if (item.Quantity == 0)
- item = null;
- return item;
- }
- public void AddItemToSlotOverride(IItem item, int slotIndex)
- {
- if (item != null)
- inventoryItems[slotIndex] = CoreInventory.Instance.CreateItem(item.Stats);
- else
- inventoryItems[slotIndex] = null;
- }
- public void RemoveItem(int slot)
- {
- inventoryItems[slot] = null;
- }
- // should return 0 if all is good(no leftover), returns leftover if there was no space for the whole item amount
- private int AddItemToSlot(IItem item, int slotIndex = -1)
- {
- // if slot is null, place it here fully and set quantity to zero
- if (inventoryItems[slotIndex] == null)
- {
- // we need to make a new data object for here in case we got leftover
- inventoryItems[slotIndex] = CoreInventory.Instance.CreateItem(item.Stats);
- item.Quantity = 0;
- }
- else if (inventoryItems[slotIndex] != null && inventoryItems[slotIndex].Info == item.Info && !inventoryItems[slotIndex].Dragged)
- {
- // if its the same item, place as much as you can here, but if it's not the dragged one
- item.Quantity = inventoryItems[slotIndex].AddToStack(item.Quantity);
- }
- // return how much of the item is left
- return item.Quantity;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement