Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class Inventory : MonoBehaviour
- {
- [SerializeField] private ItemData[] m_inventory;
- [SerializeField] private ushort m_capacity;
- private ushort m_count;
- private bool m_isFull;
- private void Start()
- {
- m_inventory = new ItemData[m_capacity];
- }
- private void Update()
- {
- // Todo: Remove, only for testring purposes.
- if (Input.GetKeyDown(KeyCode.Alpha1))
- {
- DropItem(0);
- }
- if (Input.GetKeyDown(KeyCode.Alpha2))
- {
- DropItem(1);
- }
- if (Input.GetKeyDown(KeyCode.Alpha3))
- {
- DropItem(2);
- }
- if (Input.GetKeyDown(KeyCode.Alpha4))
- {
- DropItem(3);
- }
- }
- public bool AddItem(ItemData item)
- {
- if (!m_isFull)
- {
- m_count++;
- if (m_count == m_capacity)
- m_isFull = true;
- m_inventory[m_count] = item.Copy();
- return true;
- }
- else
- return false;
- }
- public bool DropItem(ushort index)
- {
- GameObject instance = Instantiate(m_inventory[index].prefab, transform.position, Quaternion.identity, GameObject.Find("Items").transform); // Todo: Remove Find.
- if (instance != null)
- {
- EraseItem(index);
- return true;
- }
- else
- {
- return false;
- }
- }
- public void EraseItem(ushort index)
- {
- m_inventory[index] = null;
- }
- public bool MoveItem(ushort index, Inventory target)
- {
- if (target.AddItem(m_inventory[index]))
- {
- EraseItem(index);
- return true;
- }
- else
- {
- return false;
- }
- }
- public ItemData GetItem(ushort index)
- {
- return m_inventory[index];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement