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
- {
- ///////////////////////// Variables /////////////////////////
- //Inventory
- KeyValuePair<string, int>[] inv = new KeyValuePair<string, int>[12]; //Inventory Name Array
- int count = 0; //Number of slots filled.
- ///////////////////////// Functions /////////////////////////
- //Inventory Management
- void additem(int quantity, string item, GameObject obj)
- {
- if (count == 0)
- {
- inv[count] = new KeyValuePair<string, int>(item, quantity);
- GameObject.Destroy(obj);
- count++;
- return;
- }
- if (count < 12)
- {
- for (int i = 0; i < inv.Length; i++)
- {
- if (inv[i].Key == item)
- {
- inv[i] = new KeyValuePair<string, int>(item, inv[i].Value + quantity);
- GameObject.Destroy(obj);
- return;
- }
- }
- inv[count] = new KeyValuePair<string, int>(item, quantity);
- GameObject.Destroy(obj);
- count++;
- return;
- }
- }
- int PickItemQuantity(string Name)
- {
- switch (Name)
- {
- case "Weapon 1":
- return 5;
- case "Weapon 2":
- return 5;
- case "Weapon 3":
- return 5;
- case "Health 1":
- return 2;
- case "Health 2":
- return 2;
- case "Health 3":
- return 2;
- default:
- return 1;
- }
- }
- void useItem(string name, int index)
- {
- PlayerMovement playmov = GetComponent<PlayerMovement>();
- switch (name)
- {
- case "Weapon 1":
- break;
- case "Weapon 2":
- break;
- case "Weapon 3":
- break;
- case "Health 1":
- playmov.ChangeHP(10);
- break;
- case "Health 2":
- playmov.ChangeHP(20);
- break;
- case "Health 3":
- playmov.ChangeHP(30);
- break;
- default:
- break;
- }
- if(inv[index].Value > 1)
- {
- inv[index] = new KeyValuePair<string, int>(name, inv[index].Value - 1);
- }
- else if (inv[index].Value == 1)
- {
- for(int i = index; i < count; i++)
- {
- if(i < 11)
- {
- inv[i] = new KeyValuePair<string, int>(inv[i + 1].Key, inv[i + 1].Value);
- }
- }
- inv[count-1] = new KeyValuePair<string, int>(null, 0);
- count--;
- }
- }
- //GUI Management
- void pickcolor(string name)
- {
- switch (name)
- {
- case "Weapon 1":
- GUI.backgroundColor = Color.yellow;
- break;
- case "Weapon 2":
- GUI.backgroundColor = Color.blue;
- break;
- case "Weapon 3":
- GUI.backgroundColor = Color.red;
- break;
- case "Health 1":
- GUI.backgroundColor = Color.yellow;
- break;
- case "Health 2":
- GUI.backgroundColor = Color.blue;
- break;
- case "Health 3":
- GUI.backgroundColor = Color.red;
- break;
- default:
- break;
- }
- }
- ///////////////////////// Unity Functions /////////////////////////
- void Start()
- {
- GameObject player = GameObject.Find("Player");
- }
- void OnTriggerEnter(Collider col)
- {
- switch (col.gameObject.tag)
- {
- case "Enemy":
- break;
- case "Item":
- additem(PickItemQuantity(col.gameObject.name), col.gameObject.name, col.gameObject);
- break;
- default:
- break;
- }
- }
- void OnGUI()
- {
- //Normal Inventory
- if (Cursor.lockState == CursorLockMode.None)
- {
- pickcolor(inv[0].Key);
- if (GUI.Button(new Rect(40, 400, 125, 25), inv[0].Value + " - " + inv[0].Key))
- {
- useItem(inv[0].Key, 0);
- }
- GUI.backgroundColor = Color.gray;
- pickcolor(inv[1].Key);
- if (GUI.Button(new Rect(40, 425, 125, 25), inv[1].Value + " - " + inv[1].Key))
- {
- useItem(inv[1].Key, 1);
- }
- GUI.backgroundColor = Color.gray;
- pickcolor(inv[2].Key);
- if (GUI.Button(new Rect(40, 450, 125, 25), inv[2].Value + " - " + inv[2].Key))
- {
- useItem(inv[2].Key, 2);
- }
- GUI.backgroundColor = Color.gray;
- pickcolor(inv[3].Key);
- if (GUI.Button(new Rect(40, 475, 125, 25), inv[3].Value + " - " + inv[3].Key))
- {
- useItem(inv[3].Key, 3);
- }
- GUI.backgroundColor = Color.gray;
- pickcolor(inv[4].Key);
- if (GUI.Button(new Rect(40, 500, 125, 25), inv[4].Value + " - " + inv[4].Key))
- {
- useItem(inv[4].Key, 4);
- }
- GUI.backgroundColor = Color.gray;
- pickcolor(inv[5].Key);
- if (GUI.Button(new Rect(40, 525, 125, 25), inv[5].Value + " - " + inv[5].Key))
- {
- useItem(inv[5].Key, 5);
- }
- GUI.backgroundColor = Color.gray;
- if (count > 6)
- {
- pickcolor(inv[6].Key);
- if (GUI.Button(new Rect(165, 400, 125, 25), inv[6].Value + " - " + inv[6].Key))
- {
- useItem(inv[6].Key, 6);
- }
- GUI.backgroundColor = Color.gray;
- }
- if (count > 7)
- {
- pickcolor(inv[7].Key);
- if (GUI.Button(new Rect(165, 425, 125, 25), inv[7].Value + " - " + inv[7].Key))
- {
- useItem(inv[7].Key, 7);
- }
- GUI.backgroundColor = Color.gray;
- }
- if (count > 8)
- {
- pickcolor(inv[8].Key);
- if (GUI.Button(new Rect(165, 450, 125, 25), inv[8].Value + " - " + inv[8].Key))
- {
- useItem(inv[8].Key, 8);
- }
- GUI.backgroundColor = Color.gray;
- }
- if (count > 9)
- {
- pickcolor(inv[9].Key);
- if (GUI.Button(new Rect(165, 475, 125, 25), inv[9].Value + " - " + inv[9].Key))
- {
- useItem(inv[9].Key, 9);
- }
- GUI.backgroundColor = Color.gray;
- }
- if (count > 10)
- {
- pickcolor(inv[10].Key);
- if (GUI.Button(new Rect(165, 500, 125, 25), inv[10].Value + " - " + inv[10].Key))
- {
- useItem(inv[10].Key, 10);
- }
- GUI.backgroundColor = Color.gray;
- }
- if (count > 11)
- {
- pickcolor(inv[11].Key);
- if (GUI.Button(new Rect(165, 525, 125, 25), inv[11].Value + " - " + inv[11].Key))
- {
- useItem(inv[11].Key, 11);
- }
- GUI.backgroundColor = Color.gray;
- }
- GUI.Button(new Rect(25, 25, 25, 25), count + "");
- //Quick Inventory
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement