Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.75 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class Inventory : MonoBehaviour {
  6.     private bool draggingItem;
  7.     private Item draggedItem;
  8.     private int previousDraggedIndex;
  9.     public int slotsX, slotsY;
  10.     public List<Item> inventory = new List<Item>();
  11.     public List<Item> slots = new List<Item>();
  12.     private ItemDatabase database;
  13.     private bool showInventory;
  14.     public GUISkin skin;
  15.     private bool showTooltip;
  16.     private string tooltip;
  17.     public Wizard wizard;
  18.     private int itemlistLength = 7;
  19.     // Use this for initialization
  20.     void Start () {
  21.         wizard = GetComponent<Wizard>();
  22.         for (int i = 0; i < (slotsX*slotsY); i++)
  23.         {
  24.             slots.Add(new Item());
  25.             inventory.Add(new Item());
  26.          
  27.         }
  28.         database = GameObject.FindGameObjectWithTag("Item Database").GetComponent<ItemDatabase>();
  29.  
  30.         for (int i = 0; i <= itemlistLength; i++)
  31.         {
  32.             AddItem(i);
  33.         }
  34.      
  35.     }
  36.    
  37.     void OnGUI()
  38.     {
  39.         tooltip = "";
  40.         GUI.skin = skin;
  41.         if (showInventory)
  42.         {
  43.             DrawInventory();
  44.         }
  45.      
  46.         if (showTooltip)
  47.         {
  48.             GUI.Box(new Rect(Event.current.mousePosition.x  +20f,Event.current.mousePosition.y,200,200), tooltip,skin.GetStyle("Tooltip"));
  49.         }
  50.         if (draggingItem)
  51.         {
  52.             GUI.DrawTexture((new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50)),draggedItem.itemIcon);
  53.         }
  54.     }
  55.     void DrawInventory()
  56.     {
  57.         Event currentEvent = Event.current;
  58.         int i= 0;
  59.         for (int y = 0; y < slotsY; y++)
  60.             {
  61.             for (int x = 0; x < slotsX; x++)
  62.             {
  63.                 Rect slotRect = new Rect(x * 80, y * 80, 80, 80);
  64.                 GUI.Box(slotRect, "",skin.GetStyle("Slot"));
  65.                 slots[i] = inventory[i];
  66.                 if (slots[i].itemName != null)
  67.                 {
  68.  
  69.                     GUI.DrawTexture(slotRect,slots[i].itemIcon);
  70.                    
  71.                     if (slotRect.Contains(Event.current.mousePosition))
  72.                     {
  73.                         if (currentEvent.isMouse && currentEvent.type == EventType.mouseDown && currentEvent.button == 1)
  74.                         {
  75.                             if (inventory[i].itemType==Item.ItemType.Consumable)
  76.                             {
  77.                                 UseConsumable(slots[i], i, true);
  78.                                 print("use consumable");
  79.                                 //consumable
  80.                             }
  81.                             print("clicked right" + i);
  82.                         }
  83.                         tooltip =   CreateTooltip(slots[i]);
  84.                         showTooltip = true;
  85.                         if(currentEvent.button==0 && currentEvent.type == EventType.MouseDrag && !draggingItem)
  86.                         {
  87.                             draggingItem = true;
  88.                             previousDraggedIndex = i;
  89.                             draggedItem = slots[i];
  90.                             inventory[i] = new Item();
  91.                         }
  92.                         if (currentEvent.type==EventType.MouseUp &&draggingItem)
  93.                         {
  94.                             inventory[previousDraggedIndex] = inventory[i];
  95.                             inventory[i] = draggedItem;
  96.                             draggingItem = false;
  97.                             draggedItem = null;
  98.                         }
  99.                     }
  100.                 }else
  101.                 {
  102.                     if (slotRect.Contains(Event.current.mousePosition)&& currentEvent.type == EventType.MouseUp && draggingItem)
  103.                     {
  104.                        
  105.                             inventory[i] = draggedItem;
  106.                             draggingItem = false;
  107.                             draggedItem = null;
  108.                      
  109.                     }
  110.                 }
  111.                 if(tooltip=="")
  112.                 {
  113.                     showTooltip=false;
  114.                 }
  115.                 i++;
  116.                
  117.             }
  118.         }
  119.     }
  120.     string CreateTooltip(Item item)
  121.     {
  122.         tooltip = "<color=#C821FF>" + item.itemName +"</color>\n\n" + "<color=#ffffff>"+item.itemDescription +"</color>" ; // kolor zalezny od poziomu broni . colorpicker.com
  123.         return tooltip;
  124.     }
  125.     void Update()
  126.     {
  127.        // print(wizard.attackDamage);
  128.        // print(wizard.currentPlayerHealth);
  129.         if (Input.GetButtonDown("Inventory"))
  130.         {
  131.             showInventory = !showInventory;
  132.         }
  133.     }
  134.  
  135.    
  136.     void AddItem(int id)
  137.     {
  138.         for (int i = 0; i < inventory.Count; i++)
  139.         {
  140.             if(inventory[i].itemName == null)
  141.             {
  142.                 for (int j = 0; j < database.items.Count; j++)
  143.                 {
  144.                     if (database.items[j].itemID == id)
  145.                     {
  146.                         inventory[i] = database.items[j];
  147.                     }
  148.                 }
  149.                 break;
  150.             }
  151.         }
  152.     }
  153.     void RemoveItem(int id)
  154.     {
  155.         for (int i = 0; i < inventory.Count; i++)
  156.         {
  157.             if(inventory[i].itemID== id)
  158.             {
  159.                 inventory[i] = new Item();
  160.                 break;
  161.             }
  162.            
  163.         }
  164.     }
  165.     bool InventoryContains(int id)
  166.     {
  167.         foreach (Item item in inventory)
  168.         {
  169.             if (item.itemID == id) return true;
  170.         }
  171.         return false;
  172.     }
  173.  
  174.     private void UseConsumable(Item item, int slot, bool deleteItem)
  175.     {
  176.         print("use consumable called");
  177.         switch (item.itemID)
  178.         {
  179.             case 5: // case X , X = item id
  180.                 {
  181.                    
  182.                     //hp potion
  183.                     print("USED CONSUMABLE: " + item.itemName);
  184.                  
  185.                     break;
  186.                 }
  187.             case 6:
  188.                 {
  189.                     //mana potion
  190.                     print("USED CONSUMABLE: " + item.itemName);
  191.            
  192.                     break;
  193.                 }
  194.             case 7:
  195.                 {
  196.  
  197.                     //power potion
  198.                     wizard.IncreaseStats(6, 6, 6); // doesnt get called
  199.                     print("USED CONSUMABLE: " + item.itemName);
  200.                     item.itemPower = 3;
  201.                     print(item.itemPower);
  202.                   // wizard.attackDamage += item.itemPower; //tried this too ,  doesnt work
  203.            
  204.                     break;
  205.                 }
  206.                
  207.         }
  208.         if (deleteItem)
  209.         {
  210.             inventory[slot] = new Item();
  211.         }
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement