Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.84 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Inventory : MonoBehaviour
  6. {
  7.     ///////////////////////// Variables /////////////////////////
  8.  
  9.     //Inventory
  10.     KeyValuePair<string, int>[] inv = new KeyValuePair<string, int>[12]; //Inventory Name Array
  11.     int count = 0; //Number of slots filled.
  12.  
  13.     ///////////////////////// Functions /////////////////////////
  14.  
  15.     //Inventory Management
  16.     void additem(int quantity, string item, GameObject obj)
  17.     {
  18.         if (count == 0)
  19.         {
  20.             inv[count] = new KeyValuePair<string, int>(item, quantity);
  21.             GameObject.Destroy(obj);
  22.             count++;
  23.             return;
  24.         }
  25.         if (count < 12)
  26.         {
  27.             for (int i = 0; i < inv.Length; i++)
  28.             {
  29.                 if (inv[i].Key == item)
  30.                 {
  31.                     inv[i] = new KeyValuePair<string, int>(item, inv[i].Value + quantity);
  32.                     GameObject.Destroy(obj);
  33.                     return;
  34.                 }
  35.             }
  36.             inv[count] = new KeyValuePair<string, int>(item, quantity);
  37.             GameObject.Destroy(obj);
  38.             count++;
  39.             return;
  40.         }
  41.     }
  42.     int PickItemQuantity(string Name)
  43.     {
  44.         switch (Name)
  45.         {
  46.             case "Weapon 1":
  47.                 return 5;
  48.  
  49.             case "Weapon 2":
  50.                 return 5;
  51.  
  52.             case "Weapon 3":
  53.                 return 5;
  54.  
  55.             case "Health 1":
  56.                 return 2;
  57.  
  58.             case "Health 2":
  59.                 return 2;
  60.  
  61.             case "Health 3":
  62.                 return 2;
  63.  
  64.             default:
  65.                 return 1;
  66.         }
  67.     }
  68.     void useItem(string name, int index)
  69.     {
  70.         PlayerMovement playmov = GetComponent<PlayerMovement>();
  71.         switch (name)
  72.         {
  73.             case "Weapon 1":
  74.                 break;
  75.  
  76.             case "Weapon 2":
  77.                 break;
  78.  
  79.             case "Weapon 3":
  80.                 break;
  81.  
  82.             case "Health 1":
  83.                 playmov.ChangeHP(10);
  84.                 break;
  85.  
  86.             case "Health 2":
  87.                 playmov.ChangeHP(20);
  88.                 break;
  89.  
  90.             case "Health 3":
  91.                 playmov.ChangeHP(30);
  92.                 break;
  93.  
  94.             default:
  95.                 break;
  96.         }
  97.         if(inv[index].Value > 1)
  98.         {
  99.             inv[index] = new KeyValuePair<string, int>(name, inv[index].Value - 1);
  100.         }
  101.         else if (inv[index].Value == 1)
  102.         {
  103.             for(int i = index; i < count; i++)
  104.             {
  105.                 if(i < 11)
  106.                 {
  107.                     inv[i] = new KeyValuePair<string, int>(inv[i + 1].Key, inv[i + 1].Value);
  108.                 }
  109.  
  110.             }
  111.             inv[count-1] = new KeyValuePair<string, int>(null, 0);
  112.             count--;
  113.         }
  114.     }
  115.  
  116.     //GUI Management
  117.     void pickcolor(string name)
  118.     {
  119.         switch (name)
  120.         {
  121.             case "Weapon 1":
  122.                 GUI.backgroundColor = Color.yellow;
  123.                 break;
  124.  
  125.             case "Weapon 2":
  126.                 GUI.backgroundColor = Color.blue;
  127.                 break;
  128.  
  129.             case "Weapon 3":
  130.                 GUI.backgroundColor = Color.red;
  131.                 break;
  132.  
  133.             case "Health 1":
  134.                 GUI.backgroundColor = Color.yellow;
  135.                 break;
  136.  
  137.             case "Health 2":
  138.                 GUI.backgroundColor = Color.blue;
  139.                 break;
  140.  
  141.             case "Health 3":
  142.                 GUI.backgroundColor = Color.red;
  143.                 break;
  144.  
  145.             default:
  146.                 break;
  147.         }
  148.     }
  149.  
  150.  
  151.     ///////////////////////// Unity Functions /////////////////////////
  152.  
  153.     void Start()
  154.     {
  155.         GameObject player = GameObject.Find("Player");
  156.     }
  157.    
  158.     void OnTriggerEnter(Collider col)
  159.     {
  160.         switch (col.gameObject.tag)
  161.         {
  162.             case "Enemy":
  163.                 break;
  164.  
  165.             case "Item":
  166.                 additem(PickItemQuantity(col.gameObject.name), col.gameObject.name, col.gameObject);
  167.                 break;
  168.  
  169.             default:
  170.                 break;
  171.         }
  172.     }
  173.  
  174.     void OnGUI()
  175.     {
  176.         //Normal Inventory
  177.         if (Cursor.lockState == CursorLockMode.None)
  178.         {
  179.             pickcolor(inv[0].Key);
  180.             if (GUI.Button(new Rect(40, 400, 125, 25), inv[0].Value + " - " + inv[0].Key))
  181.             {
  182.                 useItem(inv[0].Key, 0);
  183.             }
  184.             GUI.backgroundColor = Color.gray;
  185.  
  186.             pickcolor(inv[1].Key);
  187.             if (GUI.Button(new Rect(40, 425, 125, 25), inv[1].Value + " - " + inv[1].Key))
  188.             {
  189.                 useItem(inv[1].Key, 1);
  190.             }
  191.             GUI.backgroundColor = Color.gray;
  192.  
  193.             pickcolor(inv[2].Key);
  194.             if (GUI.Button(new Rect(40, 450, 125, 25), inv[2].Value + " - " + inv[2].Key))
  195.             {
  196.                 useItem(inv[2].Key, 2);
  197.             }
  198.             GUI.backgroundColor = Color.gray;
  199.  
  200.             pickcolor(inv[3].Key);
  201.             if (GUI.Button(new Rect(40, 475, 125, 25), inv[3].Value + " - " + inv[3].Key))
  202.             {
  203.                 useItem(inv[3].Key, 3);
  204.             }
  205.             GUI.backgroundColor = Color.gray;
  206.  
  207.             pickcolor(inv[4].Key);
  208.             if (GUI.Button(new Rect(40, 500, 125, 25), inv[4].Value + " - " + inv[4].Key))
  209.             {
  210.                 useItem(inv[4].Key, 4);
  211.             }
  212.             GUI.backgroundColor = Color.gray;
  213.  
  214.             pickcolor(inv[5].Key);
  215.             if (GUI.Button(new Rect(40, 525, 125, 25), inv[5].Value + " - " + inv[5].Key))
  216.             {
  217.                 useItem(inv[5].Key, 5);
  218.             }
  219.             GUI.backgroundColor = Color.gray;
  220.  
  221.             if (count > 6)
  222.             {
  223.                 pickcolor(inv[6].Key);
  224.                 if (GUI.Button(new Rect(165, 400, 125, 25), inv[6].Value + " - " + inv[6].Key))
  225.                 {
  226.                     useItem(inv[6].Key, 6);
  227.                 }
  228.                 GUI.backgroundColor = Color.gray;
  229.             }
  230.             if (count > 7)
  231.             {
  232.                 pickcolor(inv[7].Key);
  233.                 if (GUI.Button(new Rect(165, 425, 125, 25), inv[7].Value + " - " + inv[7].Key))
  234.                 {
  235.                     useItem(inv[7].Key, 7);
  236.                 }
  237.                 GUI.backgroundColor = Color.gray;
  238.             }
  239.             if (count > 8)
  240.             {
  241.                 pickcolor(inv[8].Key);
  242.                 if (GUI.Button(new Rect(165, 450, 125, 25), inv[8].Value + " - " + inv[8].Key))
  243.                 {
  244.                     useItem(inv[8].Key, 8);
  245.                 }
  246.                 GUI.backgroundColor = Color.gray;
  247.             }
  248.             if (count > 9)
  249.             {
  250.                 pickcolor(inv[9].Key);
  251.                 if (GUI.Button(new Rect(165, 475, 125, 25), inv[9].Value + " - " + inv[9].Key))
  252.                 {
  253.                     useItem(inv[9].Key, 9);
  254.                 }
  255.                 GUI.backgroundColor = Color.gray;
  256.             }
  257.             if (count > 10)
  258.             {
  259.                 pickcolor(inv[10].Key);
  260.                 if (GUI.Button(new Rect(165, 500, 125, 25), inv[10].Value + " - " + inv[10].Key))
  261.                 {
  262.                     useItem(inv[10].Key, 10);
  263.                 }
  264.                 GUI.backgroundColor = Color.gray;
  265.             }
  266.             if (count > 11)
  267.             {
  268.                 pickcolor(inv[11].Key);
  269.                 if (GUI.Button(new Rect(165, 525, 125, 25), inv[11].Value + " - " + inv[11].Key))
  270.                 {
  271.                     useItem(inv[11].Key, 11);
  272.                 }
  273.                 GUI.backgroundColor = Color.gray;
  274.             }
  275.             GUI.Button(new Rect(25, 25, 25, 25), count + "");
  276.  
  277.             //Quick Inventory
  278.  
  279.  
  280.  
  281.  
  282.         }
  283.     }
  284.  
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement