Advertisement
Abysinian

Inventory Script

Feb 3rd, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Inventory : MonoBehaviour
  6. {
  7. //Size of inventory.
  8. public int invSlotsX;
  9. public int invSlotsY;
  10.  
  11. public List<Item> inventory = new List<Item>(); //Slots taken up by an item.
  12. public List<Item> slots = new List<Item>(); //Empty inventory slots.
  13.  
  14. public GUISkin skin;
  15. public GUISkin dragSkin;
  16. int guiDepth;
  17.  
  18. private bool openInventory = false;
  19.  
  20. private bool showTooltip;
  21. private string tooltip;
  22.  
  23. private bool draggingItem;
  24. private Item draggedItem;
  25. private int draggedIndex;
  26.  
  27. private ItemDatabase itemDB;
  28.  
  29. public GameManager gm;
  30. public PlayerController pc;
  31.  
  32. public Rect windowRect = new Rect(20, 20, 220, 300);
  33.  
  34. // Use this for initialization
  35. void Start ()
  36. {
  37. //Allows this object to persist between scenes.
  38. //DontDestroyOnLoad(transform.gameObject);
  39.  
  40. for(int i = 0; i < invSlotsX * invSlotsY; i++)
  41. {
  42. slots.Add (new Item());
  43. inventory.Add (new Item());
  44. }
  45. gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameManager>();
  46. pc = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
  47. itemDB = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
  48. /*AddItem(1);
  49. AddItem(0);
  50. AddItem(2);*/
  51. }
  52.  
  53. void Update ()
  54. {
  55. //If player presses the Inventory button, change openInventory bool to the opposite of it's current
  56. //state. This allows you to both open and close the inventory with the same button.
  57. if (Input.GetButtonDown("Inventory"))
  58. {
  59. openInventory = !openInventory;
  60. }
  61. if (pc == null)
  62. {
  63. pc = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
  64. }
  65. }
  66.  
  67. void OnGUI ()
  68. {
  69. Event e = Event.current;
  70. tooltip = "";
  71. GUI.skin = skin;
  72. GUI.skin = dragSkin;
  73.  
  74. if(openInventory)
  75. {
  76. windowRect = GUI.Window(0, windowRect, DrawInventory, "Inventory", skin.GetStyle ("MainGUI"));
  77. //DrawInventory();
  78. if(showTooltip)
  79. {
  80. DrawTooltip();
  81. }
  82. }
  83. if(draggingItem)
  84. {
  85. GUI.depth = 0;
  86. GUI.DrawTexture(new Rect(e.mousePosition.x, e.mousePosition.y, 50, 50), draggedItem.itemIcon);
  87. }
  88. }
  89.  
  90. //Displays the Inventory Window.
  91. private void DrawInventory (int windowID)
  92. {
  93. Event e = Event.current;
  94. int i = 0; //Inventory index counter.
  95. GUI.depth = 2;
  96. GUI.DragWindow(new Rect(0, 0, 10000, 20));
  97. //Y before X in order to draw rows instead of columns.
  98. for(int y = 0; y < invSlotsY; y++)
  99. {
  100. for(int x = 0; x < invSlotsX; x++)
  101. {
  102. Rect slotRect = new Rect(7 + (x * 52), 33 + (y * 52), 50, 50);
  103. //Draws a GUI Box made up of x by y "slots" with the defined GUI skin.
  104. GUI.depth = 1;
  105. GUI.Box(slotRect, "", skin.GetStyle("Inventory Slot"));
  106. slots[i] = inventory[i];
  107. Item item = slots[i];
  108.  
  109. //If current slot contains an item...
  110. if (slots[i].itemName != null)
  111. {
  112. GUI.DrawTexture(slotRect, slots[i].itemIcon);
  113. if(slotRect.Contains(e.mousePosition))
  114. {
  115. if (!draggingItem)
  116. {
  117. tooltip = CreateTooltip(slots[i]);
  118. showTooltip = true;
  119. }
  120.  
  121. //If mouse is clicking and dragging...
  122. if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem)
  123. {
  124. draggingItem = true;
  125. draggedIndex = i;
  126. draggedItem = slots[i]; //Set draggedItem to the item being moved.
  127. inventory[i] = new Item(); //Empty inventory slot that item is moved from.
  128. }
  129.  
  130. //If mouse button has been released and an item was being dragged...
  131. if(e.type == EventType.mouseUp && draggingItem)
  132. {
  133. //Swaps item dragged with item in slot being hovered over.
  134. inventory[draggedIndex] = inventory[i];
  135. inventory[i] = draggedItem;
  136. draggingItem = false;
  137. draggedItem = null;
  138. }
  139.  
  140. //If right mouse button is being clicked...
  141. if(e.isMouse && e.type == EventType.mouseDown && e.button == 1)
  142. {
  143. if(item.itemType == Item.ItemType.Consumable)
  144. {
  145. UseConsumable(slots[i],i,true);
  146. }
  147. }
  148. }
  149. }
  150. else
  151. {
  152. if(slotRect.Contains (e.mousePosition))
  153. {
  154. if(e.type == EventType.mouseUp && draggingItem)
  155. {
  156. //Drops item in to empty item slot.
  157. inventory[i] = draggedItem;
  158. draggingItem = false;
  159. draggedItem = null;
  160. }
  161. }
  162. }
  163. if(tooltip == "")
  164. {
  165. showTooltip = false;
  166. }
  167. i++; //Increment inventory index counter.
  168. }
  169. }
  170. }
  171.  
  172. private void DrawTooltip()
  173. {
  174. //float dynamicSize = skin.box.CalcHeight(new GUIContent(tooltip), 200);
  175. Event e = Event.current;
  176. GUI.depth = -10;
  177. GUI.Box(new Rect(e.mousePosition.x + 10, e.mousePosition.y, 200, 200), tooltip, skin.GetStyle("Tooltip"));
  178. }
  179.  
  180. private string CreateTooltip(Item item)
  181. {
  182. //Generates tooltips based on ItemType.
  183. if (item.itemType == Item.ItemType.Consumable)
  184. {
  185. tooltip = "<color=#ffffff><b>" + item.itemName + "</b></color>\n\n" + item.itemDesc + "\n\n" +
  186. "<b>Restores: </b>\n" + "HP: " + item.itemHealHP + "\n" + "MP: " + item.itemHealMP + "\n\n"
  187. + "<color=#CCB75C><b>" + item.itemValue + "G</b></color>";
  188. }
  189. else if (item.itemType == Item.ItemType.Weapon)
  190. {
  191. tooltip = "<color=#ffffff><b>" + item.itemName + "</b></color>\n\n" + item.itemDesc + "\n\n" +
  192. "<b>Stats: </b>\n" + "Atk: " + item.itemAtk + "\n" + "M.Atk: " + item.itemMAtk + "\n" + "Spd: " + item.itemSpd
  193. + "\n\n" + "Int: " + item.itemInt + "\n" + "Str: " + item.itemStr + "\n" + "Dex: " + item.itemDex + "\n\n"
  194. + "<color=#CCB75C><b>" + item.itemValue + "G</b></color>";
  195. }
  196. else if (item.itemType == Item.ItemType.Armour || item.itemType == Item.ItemType.Accessory)
  197. {
  198. tooltip = "<color=#ffffff><b>" + item.itemName + "</b></color>\n\n" + item.itemDesc + "\n\n" +
  199. "<b>Stats: </b>\n" + "Def: " + item.itemDef + "\n" + "M.Def: " + item.itemMDef + "\n\n" + "Int: " + item.itemInt +
  200. "\n" + "Str: " + item.itemStr + "\n" + "Dex: " + item.itemDex + "\n\n" + "<color=#CCB75C><b>" + item.itemValue + "G</b></color>";
  201. }
  202. else if (item.itemType == Item.ItemType.Misc)
  203. {
  204. tooltip = "<color=#ffffff><b>" + item.itemName + "</b></color>\n\n" + item.itemDesc + "\n\n"
  205. + "<color=#CCB75C><b>" + item.itemValue + "G</b></color>";
  206. }
  207. else if (item.itemType == Item.ItemType.Quest)
  208. {
  209. tooltip = "<color=#ffffff><b>" + item.itemName + "</b></color>\n\n" + item.itemDesc + "\n\n"
  210. + "<color=#CCB75C><b>Quest Item</b></color>";
  211. }
  212. return tooltip;
  213. }
  214.  
  215. //Add an item to the Player's inventory.
  216. public void AddItem(int id)
  217. {
  218. for(int i = 0; i < inventory.Count; i++)
  219. {
  220. //Check if inventory slot is empty.
  221. if (inventory[i].itemName == null)
  222. {
  223. for(int j = 0; j < itemDB.items.Count; j++)
  224. {
  225. //Find item with id matching the one passed in.
  226. if(itemDB.items[j].itemID == id)
  227. {
  228. //Put item in this empty slot.
  229. inventory[i] = itemDB.items[j];
  230. }
  231. }
  232. break;
  233. }
  234. }
  235. }
  236.  
  237. //Remove an item from Player's inventory.
  238. public void RemoveItem(int id)
  239. {
  240. for (int i = 0; i < inventory.Count; i++)
  241. {
  242. //If item found in inventory...
  243. if (inventory[i].itemID == id)
  244. {
  245. inventory[i] = new Item(); //Change item to an empty slot.
  246. break;
  247. }
  248. }
  249. }
  250.  
  251. //Check if an item already exists in the Player's inventory (can be used for quests/stacking, etc).
  252. public bool InventoryContains(int id)
  253. {
  254. for (int i = 0; i < inventory.Count; i++)
  255. {
  256. if (inventory[i].itemID == id)
  257. {
  258. return true;
  259. }
  260. }
  261. return false;
  262. }
  263.  
  264. private void UseConsumable(Item item, int slot, bool deleteItem)
  265. {
  266. switch(item.itemID)
  267. {
  268. case 1:
  269. {
  270. //Restore 20HP.
  271. int healed = 20;
  272. print ("Used: " + item.itemName);
  273. pc.SendMessage("PlayerHealed", healed, SendMessageOptions.DontRequireReceiver);
  274. pc.SendMessage ("ConsumableUsed",SendMessageOptions.DontRequireReceiver);
  275. break;
  276. }
  277. case 2:
  278. {
  279. //Restore 50HP.
  280. int healed = 50;
  281. print ("Used: " + item.itemName);
  282. pc.SendMessage("PlayerHealed", healed, SendMessageOptions.DontRequireReceiver);
  283. pc.SendMessage ("ConsumableUsed",SendMessageOptions.DontRequireReceiver);
  284. break;
  285. }
  286. }
  287. if (deleteItem)
  288. {
  289. inventory[slot] = new Item();
  290. }
  291. }
  292.  
  293. private void EquipItem(Item item, int slot, bool deleteItem)
  294. {
  295. if(item.itemType == Item.ItemType.Weapon)
  296. {
  297. pc.atk += item.itemAtk;
  298. pc.mAtk += item.itemMAtk;
  299. pc.atkSpd += item.itemSpd;
  300. }
  301. }
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement