Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Inventory : MonoBehaviour
- {
- int inventorySize = 69;
- public short[,] PlayerInventory = new short[69, 2];
- public static Inventory instance;
- public GameObject inventoryDisplay;
- public delegate void OnItemChanged();
- public OnItemChanged onItemChangedCallback;
- void Awake()
- {
- if (instance != null)
- return;
- instance = this;
- }
- void Start()
- {
- CleanInventory();
- }
- public void AddItem(short id, short amount)
- {
- bool hasItem = false;
- for (int i = 0; i < inventorySize; i++)
- {
- if (PlayerInventory[i, 0] == id)
- {
- PlayerInventory[i, 0] = id;
- PlayerInventory[i, 1] += amount;
- Debug.Log("Added item ID: " + PlayerInventory[i, 0] + " (" + PlayerInventory[i, 1] + " total).");
- hasItem = true;
- break;
- }
- }
- if (!hasItem)
- {
- for (int i = 0; i < inventorySize; i++)
- {
- if (PlayerInventory[i, 0] == -1)
- {
- PlayerInventory[i, 0] = id;
- PlayerInventory[i, 1] += amount;
- Debug.Log("Added item ID: " + PlayerInventory[i, 0] + " (" + PlayerInventory[i, 1] + " total).");
- break;
- }
- }
- }
- if (onItemChangedCallback != null)
- onItemChangedCallback.Invoke();
- }
- public void RemoveItem(int slot, short amount)
- {
- //Check if they're trying to remove too many items
- if(amount > PlayerInventory[slot, 1] || PlayerInventory[slot, 0] == -1)
- {
- Debug.Log("Nothing to remove.");
- return;
- }
- //Check if it is their last item
- else if(PlayerInventory[slot, 1] == 1)
- {
- Debug.Log("Removed " + amount + " item ID " +
- PlayerInventory[slot, 0] + " from inventory. (" + (PlayerInventory[slot, 1] - (short)1) + " remaining.)");
- PlayerInventory[slot, 0] = -1;
- PlayerInventory[slot, 1] = 0;
- }
- else
- {
- Debug.Log("Removed " + amount + " item ID " +
- PlayerInventory[slot, 0] + " from inventory. (" + (PlayerInventory[slot, 1] - (short)1) + " remaining.)");
- PlayerInventory[slot, 1] -= amount;
- }
- if (onItemChangedCallback != null)
- onItemChangedCallback.Invoke();
- }
- public void MoveItem(int slotA, int slotB)
- {
- //Temp values
- short slotItemA = PlayerInventory[slotA, 0];
- short slotAmountA = PlayerInventory[slotA, 1];
- short slotItemB = PlayerInventory[slotB, 0];
- short slotAmountB = PlayerInventory[slotB, 1];
- //Swap the items
- PlayerInventory[slotA, 0] = slotItemB;
- PlayerInventory[slotA, 1] = slotAmountB;
- PlayerInventory[slotB, 0] = slotItemA;
- PlayerInventory[slotB, 1] = slotAmountA;
- Debug.Log("Swapped slot " + slotA + " with slot " + slotB + ".");
- if (onItemChangedCallback != null)
- onItemChangedCallback.Invoke();
- }
- public void PrintInventory()
- {
- for (int i = 0; i < inventorySize; i++)
- {
- Debug.Log("Slot " + i + ": item ID: " + PlayerInventory[i, 0] + " (" + PlayerInventory[i, 1] + ").");
- }
- if (onItemChangedCallback != null)
- onItemChangedCallback.Invoke();
- }
- public void CleanInventory()
- {
- for (int i = 0; i < inventorySize; i++)
- {
- //Set item ID to -1
- PlayerInventory[i, 0] = -1;
- //Set the item amount to 0
- PlayerInventory[i, 1] = 0;
- }
- if(onItemChangedCallback != null)
- onItemChangedCallback.Invoke();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement