Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class Inventory : MonoBehaviour
- {
- //Size of inventory.
- public int invSlotsX;
- public int invSlotsY;
- public List<Item> inventory = new List<Item>(); //Slots taken up by an item.
- public List<Item> slots = new List<Item>(); //Empty inventory slots.
- public GUISkin skin;
- public GUISkin dragSkin;
- int guiDepth;
- private bool openInventory = false;
- private bool showTooltip;
- private string tooltip;
- private bool draggingItem;
- private Item draggedItem;
- private int draggedIndex;
- private ItemDatabase itemDB;
- public GameManager gm;
- public PlayerController pc;
- public Rect windowRect = new Rect(20, 20, 220, 300);
- // Use this for initialization
- void Start ()
- {
- //Allows this object to persist between scenes.
- //DontDestroyOnLoad(transform.gameObject);
- for(int i = 0; i < invSlotsX * invSlotsY; i++)
- {
- slots.Add (new Item());
- inventory.Add (new Item());
- }
- gm = GameObject.FindGameObjectWithTag("GM").GetComponent<GameManager>();
- pc = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
- itemDB = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
- /*AddItem(1);
- AddItem(0);
- AddItem(2);*/
- }
- void Update ()
- {
- //If player presses the Inventory button, change openInventory bool to the opposite of it's current
- //state. This allows you to both open and close the inventory with the same button.
- if (Input.GetButtonDown("Inventory"))
- {
- openInventory = !openInventory;
- }
- if (pc == null)
- {
- pc = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
- }
- }
- void OnGUI ()
- {
- Event e = Event.current;
- tooltip = "";
- GUI.skin = skin;
- GUI.skin = dragSkin;
- if(openInventory)
- {
- windowRect = GUI.Window(0, windowRect, DrawInventory, "Inventory", skin.GetStyle ("MainGUI"));
- //DrawInventory();
- if(showTooltip)
- {
- DrawTooltip();
- }
- }
- if(draggingItem)
- {
- GUI.depth = 0;
- GUI.DrawTexture(new Rect(e.mousePosition.x, e.mousePosition.y, 50, 50), draggedItem.itemIcon);
- }
- }
- //Displays the Inventory Window.
- private void DrawInventory (int windowID)
- {
- Event e = Event.current;
- int i = 0; //Inventory index counter.
- GUI.depth = 2;
- GUI.DragWindow(new Rect(0, 0, 10000, 20));
- //Y before X in order to draw rows instead of columns.
- for(int y = 0; y < invSlotsY; y++)
- {
- for(int x = 0; x < invSlotsX; x++)
- {
- Rect slotRect = new Rect(7 + (x * 52), 33 + (y * 52), 50, 50);
- //Draws a GUI Box made up of x by y "slots" with the defined GUI skin.
- GUI.depth = 1;
- GUI.Box(slotRect, "", skin.GetStyle("Inventory Slot"));
- slots[i] = inventory[i];
- Item item = slots[i];
- //If current slot contains an item...
- if (slots[i].itemName != null)
- {
- GUI.DrawTexture(slotRect, slots[i].itemIcon);
- if(slotRect.Contains(e.mousePosition))
- {
- if (!draggingItem)
- {
- tooltip = CreateTooltip(slots[i]);
- showTooltip = true;
- }
- //If mouse is clicking and dragging...
- if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem)
- {
- draggingItem = true;
- draggedIndex = i;
- draggedItem = slots[i]; //Set draggedItem to the item being moved.
- inventory[i] = new Item(); //Empty inventory slot that item is moved from.
- }
- //If mouse button has been released and an item was being dragged...
- if(e.type == EventType.mouseUp && draggingItem)
- {
- //Swaps item dragged with item in slot being hovered over.
- inventory[draggedIndex] = inventory[i];
- inventory[i] = draggedItem;
- draggingItem = false;
- draggedItem = null;
- }
- //If right mouse button is being clicked...
- if(e.isMouse && e.type == EventType.mouseDown && e.button == 1)
- {
- if(item.itemType == Item.ItemType.Consumable)
- {
- UseConsumable(slots[i],i,true);
- }
- }
- }
- }
- else
- {
- if(slotRect.Contains (e.mousePosition))
- {
- if(e.type == EventType.mouseUp && draggingItem)
- {
- //Drops item in to empty item slot.
- inventory[i] = draggedItem;
- draggingItem = false;
- draggedItem = null;
- }
- }
- }
- if(tooltip == "")
- {
- showTooltip = false;
- }
- i++; //Increment inventory index counter.
- }
- }
- }
- private void DrawTooltip()
- {
- //float dynamicSize = skin.box.CalcHeight(new GUIContent(tooltip), 200);
- Event e = Event.current;
- GUI.depth = -10;
- GUI.Box(new Rect(e.mousePosition.x + 10, e.mousePosition.y, 200, 200), tooltip, skin.GetStyle("Tooltip"));
- }
- private string CreateTooltip(Item item)
- {
- //Generates tooltips based on ItemType.
- if (item.itemType == Item.ItemType.Consumable)
- {
- tooltip = "<color=#ffffff><b>" + item.itemName + "</b></color>\n\n" + item.itemDesc + "\n\n" +
- "<b>Restores: </b>\n" + "HP: " + item.itemHealHP + "\n" + "MP: " + item.itemHealMP + "\n\n"
- + "<color=#CCB75C><b>" + item.itemValue + "G</b></color>";
- }
- else if (item.itemType == Item.ItemType.Weapon)
- {
- tooltip = "<color=#ffffff><b>" + item.itemName + "</b></color>\n\n" + item.itemDesc + "\n\n" +
- "<b>Stats: </b>\n" + "Atk: " + item.itemAtk + "\n" + "M.Atk: " + item.itemMAtk + "\n" + "Spd: " + item.itemSpd
- + "\n\n" + "Int: " + item.itemInt + "\n" + "Str: " + item.itemStr + "\n" + "Dex: " + item.itemDex + "\n\n"
- + "<color=#CCB75C><b>" + item.itemValue + "G</b></color>";
- }
- else if (item.itemType == Item.ItemType.Armour || item.itemType == Item.ItemType.Accessory)
- {
- tooltip = "<color=#ffffff><b>" + item.itemName + "</b></color>\n\n" + item.itemDesc + "\n\n" +
- "<b>Stats: </b>\n" + "Def: " + item.itemDef + "\n" + "M.Def: " + item.itemMDef + "\n\n" + "Int: " + item.itemInt +
- "\n" + "Str: " + item.itemStr + "\n" + "Dex: " + item.itemDex + "\n\n" + "<color=#CCB75C><b>" + item.itemValue + "G</b></color>";
- }
- else if (item.itemType == Item.ItemType.Misc)
- {
- tooltip = "<color=#ffffff><b>" + item.itemName + "</b></color>\n\n" + item.itemDesc + "\n\n"
- + "<color=#CCB75C><b>" + item.itemValue + "G</b></color>";
- }
- else if (item.itemType == Item.ItemType.Quest)
- {
- tooltip = "<color=#ffffff><b>" + item.itemName + "</b></color>\n\n" + item.itemDesc + "\n\n"
- + "<color=#CCB75C><b>Quest Item</b></color>";
- }
- return tooltip;
- }
- //Add an item to the Player's inventory.
- public void AddItem(int id)
- {
- for(int i = 0; i < inventory.Count; i++)
- {
- //Check if inventory slot is empty.
- if (inventory[i].itemName == null)
- {
- for(int j = 0; j < itemDB.items.Count; j++)
- {
- //Find item with id matching the one passed in.
- if(itemDB.items[j].itemID == id)
- {
- //Put item in this empty slot.
- inventory[i] = itemDB.items[j];
- }
- }
- break;
- }
- }
- }
- //Remove an item from Player's inventory.
- public void RemoveItem(int id)
- {
- for (int i = 0; i < inventory.Count; i++)
- {
- //If item found in inventory...
- if (inventory[i].itemID == id)
- {
- inventory[i] = new Item(); //Change item to an empty slot.
- break;
- }
- }
- }
- //Check if an item already exists in the Player's inventory (can be used for quests/stacking, etc).
- public bool InventoryContains(int id)
- {
- for (int i = 0; i < inventory.Count; i++)
- {
- if (inventory[i].itemID == id)
- {
- return true;
- }
- }
- return false;
- }
- private void UseConsumable(Item item, int slot, bool deleteItem)
- {
- switch(item.itemID)
- {
- case 1:
- {
- //Restore 20HP.
- int healed = 20;
- print ("Used: " + item.itemName);
- pc.SendMessage("PlayerHealed", healed, SendMessageOptions.DontRequireReceiver);
- pc.SendMessage ("ConsumableUsed",SendMessageOptions.DontRequireReceiver);
- break;
- }
- case 2:
- {
- //Restore 50HP.
- int healed = 50;
- print ("Used: " + item.itemName);
- pc.SendMessage("PlayerHealed", healed, SendMessageOptions.DontRequireReceiver);
- pc.SendMessage ("ConsumableUsed",SendMessageOptions.DontRequireReceiver);
- break;
- }
- }
- if (deleteItem)
- {
- inventory[slot] = new Item();
- }
- }
- private void EquipItem(Item item, int slot, bool deleteItem)
- {
- if(item.itemType == Item.ItemType.Weapon)
- {
- pc.atk += item.itemAtk;
- pc.mAtk += item.itemMAtk;
- pc.atkSpd += item.itemSpd;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement