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 {
- private bool draggingItem;
- private Item draggedItem;
- private int previousDraggedIndex;
- public int slotsX, slotsY;
- public List<Item> inventory = new List<Item>();
- public List<Item> slots = new List<Item>();
- private ItemDatabase database;
- private bool showInventory;
- public GUISkin skin;
- private bool showTooltip;
- private string tooltip;
- public Wizard wizard;
- private int itemlistLength = 7;
- // Use this for initialization
- void Start () {
- wizard = GetComponent<Wizard>();
- for (int i = 0; i < (slotsX*slotsY); i++)
- {
- slots.Add(new Item());
- inventory.Add(new Item());
- }
- database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
- for (int i = 0; i <= itemlistLength; i++)
- {
- AddItem(i);
- }
- }
- void OnGUI()
- {
- tooltip = "";
- GUI.skin = skin;
- if (showInventory)
- {
- DrawInventory();
- }
- if (showTooltip)
- {
- GUI.Box(new Rect(Event.current.mousePosition.x +20f,Event.current.mousePosition.y,200,200), tooltip,skin.GetStyle("Tooltip"));
- }
- if (draggingItem)
- {
- GUI.DrawTexture((new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50)),draggedItem.itemIcon);
- }
- }
- void DrawInventory()
- {
- Event currentEvent = Event.current;
- int i= 0;
- for (int y = 0; y < slotsY; y++)
- {
- for (int x = 0; x < slotsX; x++)
- {
- Rect slotRect = new Rect(x * 80, y * 80, 80, 80);
- GUI.Box(slotRect, "",skin.GetStyle("Slot"));
- slots[i] = inventory[i];
- if (slots[i].itemName != null)
- {
- GUI.DrawTexture(slotRect,slots[i].itemIcon);
- if (slotRect.Contains(Event.current.mousePosition))
- {
- if (currentEvent.isMouse && currentEvent.type == EventType.mouseDown && currentEvent.button == 1)
- {
- if (inventory[i].itemType==Item.ItemType.Consumable)
- {
- UseConsumable(slots[i], i, true);
- print("use consumable");
- //consumable
- }
- print("clicked right" + i);
- }
- tooltip = CreateTooltip(slots[i]);
- showTooltip = true;
- if(currentEvent.button==0 && currentEvent.type == EventType.MouseDrag && !draggingItem)
- {
- draggingItem = true;
- previousDraggedIndex = i;
- draggedItem = slots[i];
- inventory[i] = new Item();
- }
- if (currentEvent.type==EventType.MouseUp &&draggingItem)
- {
- inventory[previousDraggedIndex] = inventory[i];
- inventory[i] = draggedItem;
- draggingItem = false;
- draggedItem = null;
- }
- }
- }else
- {
- if (slotRect.Contains(Event.current.mousePosition)&& currentEvent.type == EventType.MouseUp && draggingItem)
- {
- inventory[i] = draggedItem;
- draggingItem = false;
- draggedItem = null;
- }
- }
- if(tooltip=="")
- {
- showTooltip=false;
- }
- i++;
- }
- }
- }
- string CreateTooltip(Item item)
- {
- tooltip = "<color=#C821FF>" + item.itemName +"</color>\n\n" + "<color=#ffffff>"+item.itemDescription +"</color>" ; // kolor zalezny od poziomu broni . colorpicker.com
- return tooltip;
- }
- void Update()
- {
- // print(wizard.attackDamage);
- // print(wizard.currentPlayerHealth);
- if (Input.GetButtonDown("Inventory"))
- {
- showInventory = !showInventory;
- }
- }
- void AddItem(int id)
- {
- for (int i = 0; i < inventory.Count; i++)
- {
- if(inventory[i].itemName == null)
- {
- for (int j = 0; j < database.items.Count; j++)
- {
- if (database.items[j].itemID == id)
- {
- inventory[i] = database.items[j];
- }
- }
- break;
- }
- }
- }
- void RemoveItem(int id)
- {
- for (int i = 0; i < inventory.Count; i++)
- {
- if(inventory[i].itemID== id)
- {
- inventory[i] = new Item();
- break;
- }
- }
- }
- bool InventoryContains(int id)
- {
- foreach (Item item in inventory)
- {
- if (item.itemID == id) return true;
- }
- return false;
- }
- private void UseConsumable(Item item, int slot, bool deleteItem)
- {
- print("use consumable called");
- switch (item.itemID)
- {
- case 5: // case X , X = item id
- {
- //hp potion
- print("USED CONSUMABLE: " + item.itemName);
- break;
- }
- case 6:
- {
- //mana potion
- print("USED CONSUMABLE: " + item.itemName);
- break;
- }
- case 7:
- {
- //power potion
- wizard.IncreaseStats(6, 6, 6); // doesnt get called
- print("USED CONSUMABLE: " + item.itemName);
- item.itemPower = 3;
- print(item.itemPower);
- // wizard.attackDamage += item.itemPower; //tried this too , doesnt work
- break;
- }
- }
- if (deleteItem)
- {
- inventory[slot] = new Item();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement