Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.55 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEngine;
  4.  
  5. internal class Slot
  6. {
  7.     public GameObject slot;
  8.     public Item item;
  9.     public int amount;
  10.     public int size;
  11. }
  12.  
  13. public class Inventory : MonoBehaviour
  14. {
  15.     #region Data
  16.     private GameObject _slotHolder;
  17.  
  18.     private int _slotAmount;
  19.     private GameObject[] _slots;
  20.  
  21.     private bool _visible = false;
  22.  
  23.     private readonly List<Slot> _slotList = new List<Slot>();
  24.  
  25.     public GameObject inventory;
  26.     #endregion
  27.  
  28.     #region Events
  29.     private void Awake()
  30.     {
  31.         if (inventory == null)
  32.         {
  33.             Debug.LogError("No Inventory object assigned!");
  34.             return;
  35.         }
  36.  
  37.         _slotHolder = inventory.transform.Find("SlotHolder").gameObject;
  38.         if (_slotHolder == null)
  39.         {
  40.             Debug.LogError("Inventory object does not contain a SlotHolder object");
  41.             return;
  42.         }
  43.  
  44.         _GrabInventorySlots();
  45.     }
  46.     #endregion
  47.  
  48.     #region API
  49.     public bool IsVisible()
  50.     {
  51.         return _visible;
  52.     }
  53.  
  54.     public void ToggleVisibility()
  55.     {
  56.         _visible = !_visible;
  57.         inventory.SetActive(_visible);
  58.     }
  59.  
  60.     public void AddItem(Item aItem, int aAmount = 1)
  61.     {
  62.         // Check if the item is stackable and the inventory contains this item already
  63.         bool contains = _slotList.Any(x => x.item.id == aItem.id);
  64.         if (contains)
  65.         {
  66.             var slots = _slotList.Where(x => x.item.id == aItem.id).ToArray();
  67.             foreach (var slot in slots)
  68.             {
  69.                 if ((slot.amount + aAmount) < slot.size)
  70.                 {
  71.                     // Enough space, insert directly
  72.                     slot.amount += aAmount;
  73.                     return;
  74.                 }
  75.  
  76.                 if ((slot.amount + aAmount) > slot.size)
  77.                 {
  78.                     // Check if enough empty slots are free for remainder
  79.                     int remainder = (slot.amount + aAmount) - slot.size;
  80.                     if (remainder > slot.size)
  81.                     {
  82.                         // Need more than one slot
  83.                     }
  84.                     else
  85.                     {
  86.                         // Remainded should fit into one slot
  87.  
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.     }
  93.  
  94.     private Slot _GetFreeSlot()
  95.     {
  96.         foreach (Slot slot in _slotList)
  97.         {
  98.             if (slot.amount == 0)
  99.             {
  100.                 return slot;
  101.             }
  102.         }
  103.  
  104.         return null;
  105.     }
  106.  
  107.     private Slot _GetFreeSlotContainingItem(Item aItem)
  108.     {
  109.         foreach (Slot slot in _slotList)
  110.         {
  111.             if (slot.item.id == aItem.id)
  112.             {
  113.                 if (slot.amount < slot.size)
  114.                 {
  115.                     return slot;
  116.                 }
  117.             }
  118.         }
  119.  
  120.         return null;
  121.     }
  122.  
  123.     private void _GrabInventorySlots()
  124.     {
  125.         _slotAmount = _slotHolder.transform.childCount;
  126.         _slots = new GameObject[_slotAmount];
  127.  
  128.         for (int i = 0; i < _slotAmount; i++)
  129.         {
  130.             _slots[i] = _slotHolder.transform.GetChild(i).gameObject;
  131.             Slot item = new Slot
  132.             {
  133.                 slot = _slots[i],
  134.                 item = null,
  135.                 amount = 0,
  136.                 size = _slots[i].GetComponent<InventorySlot>().slotSize
  137.             };
  138.  
  139.             _slotList.Add(item);
  140.         }
  141.  
  142.         Debug.Log("Found " + _slotAmount + " inventory slots");
  143.     }
  144.     #endregion
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement