daily pastebin goal
32%
SHARE
TWEET

Inventory.cs

a guest Jan 31st, 2018 2 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2.  
  3. public class Inventory : MonoBehaviour
  4. {
  5.  
  6.     [SerializeField] private ItemData[] m_inventory;
  7.     [SerializeField] private ushort m_capacity;
  8.     private ushort m_count;
  9.     private bool m_isFull;
  10.  
  11.     private void Start()
  12.     {
  13.         m_inventory = new ItemData[m_capacity];
  14.     }
  15.    
  16.     private void Update()
  17.     {
  18.         // Todo: Remove, only for testring purposes.
  19.         if (Input.GetKeyDown(KeyCode.Alpha1))
  20.         {
  21.             DropItem(0);
  22.         }
  23.  
  24.         if (Input.GetKeyDown(KeyCode.Alpha2))
  25.         {
  26.             DropItem(1);
  27.         }
  28.  
  29.         if (Input.GetKeyDown(KeyCode.Alpha3))
  30.         {
  31.             DropItem(2);
  32.         }
  33.  
  34.         if (Input.GetKeyDown(KeyCode.Alpha4))
  35.         {
  36.             DropItem(3);
  37.         }
  38.     }
  39.  
  40.     public bool AddItem(ItemData item)
  41.     {
  42.         if (!m_isFull)
  43.         {
  44.             m_count++;
  45.             if (m_count == m_capacity)
  46.                 m_isFull = true;
  47.  
  48.             m_inventory[m_count] = item.Copy();
  49.             return true;
  50.         }
  51.         else
  52.             return false;
  53.     }
  54.  
  55.     public bool DropItem(ushort index)
  56.     {
  57.         GameObject instance = Instantiate(m_inventory[index].prefab, transform.position, Quaternion.identity, GameObject.Find("Items").transform); // Todo: Remove Find.
  58.         if (instance != null)
  59.         {
  60.             EraseItem(index);
  61.             return true;
  62.         }
  63.         else
  64.         {
  65.             return false;
  66.         }
  67.     }
  68.  
  69.     public void EraseItem(ushort index)
  70.     {
  71.         m_inventory[index] = null;
  72.     }
  73.  
  74.     public bool MoveItem(ushort index, Inventory target)
  75.     {
  76.         if (target.AddItem(m_inventory[index]))
  77.         {
  78.             EraseItem(index);
  79.             return true;
  80.         }
  81.         else
  82.         {
  83.             return false;
  84.         }
  85.     }
  86.  
  87.     public ItemData GetItem(ushort index)
  88.     {
  89.         return m_inventory[index];
  90.     }
  91.  
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top