Komak57

InventoryManager

Feb 25th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.96 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class InventoryManager : MonoBehaviour {
  6.     static InventoryManager instance;
  7.     public bool ShowBag = true;
  8.     public int MaxInventorySpace = 19;
  9.     public Texture QuickSlotBG = new Texture();
  10.     GUIStyle toggle = new GUIStyle();
  11.     public Texture SwitchOn = new Texture();
  12.     public Texture SwitchOff = new Texture();
  13.     public BagItem[] Items = new BagItem[0];
  14.     public BagItem[] QuickSlots = new BagItem[10];
  15.     public class SourceItem {
  16.         public int Slot = -1;
  17.         public BagItem Item = null;
  18.         public enum SourceLoc { NULL = -1, Inventory = 1, Quickslot = 2, Equipment = 3, Vanity = 4, Loot = 5 }
  19.         public SourceLoc From = SourceLoc.NULL;
  20.  
  21.         public SourceItem() {
  22.             Slot = -1;
  23.             Item = null;
  24.             From = SourceLoc.NULL;
  25.         }
  26.         public SourceItem(int Slot, BagItem Item, SourceLoc From) {
  27.             this.Slot = Slot;
  28.             this.Item = Item;
  29.             this.From = From;
  30.         }
  31.     }
  32.     //private int SelectedSlot = -1;
  33.     //private BagItem SelectedItem = null;
  34.     private SourceItem Selected = new SourceItem();
  35.     public Dictionary<int, Texture> textures = new Dictionary<int, Texture>();
  36.     bool ManageGear = true;
  37.     static class Bag {
  38.         public const int All = 0;
  39.         public const int Resources = 1;
  40.         public const int Equipment = 2;
  41.     }
  42.    
  43.     //bool _ShowMenu = false;
  44.     Rect window;
  45.     void Awake() {
  46.         instance = this;
  47.     }
  48.     // Use this for initialization
  49.     void Start () {
  50.         instance = this;
  51.         Items = new BagItem[MaxInventorySpace];
  52.         Items[0] = new BagItem(1000, 1);
  53.         Items[1] = new BagItem(3, 200);
  54.     }
  55.    
  56.     // Update is called once per frame
  57.     void Update () {
  58.         if (MainMenu.myInput.Inventory.isPressed()) {
  59.             ShowBag = !ShowBag;
  60.            
  61.             if (ShowBag) {
  62.                 //Debug.Log(Items.Count+" items in inventory.");
  63.                 textures.Clear();
  64.                 foreach(BagItem item in Items) {
  65.                     if (item == null)
  66.                         continue;
  67.                     if (!textures.ContainsKey(item.ItemID))
  68.                         textures.Add(item.ItemID, ItemList.GetItem(item.ItemID).Texture);
  69.                 }
  70.                 Screen.lockCursor = false;
  71.                 Screen.showCursor = true;
  72.                 GameObject.Find("Player").GetComponent<Movement>().Pause();
  73.             } else {
  74.                 Screen.lockCursor = true;
  75.                 Screen.showCursor = false;
  76.                 GameObject.Find("Player").GetComponent<Movement>().UnPause();
  77.             }
  78.         }
  79.         if (Input.GetKeyDown(KeyCode.Escape) && ShowBag) {
  80.             ShowBag = false;
  81.            
  82.             if (ShowBag) {
  83.                 Screen.lockCursor = false;
  84.                 Screen.showCursor = true;
  85.                 GameObject.Find("Player").GetComponent<Movement>().Pause();
  86.             } else {
  87.                 Screen.lockCursor = true;
  88.                 Screen.showCursor = false;
  89.                 GameObject.Find("Player").GetComponent<Movement>().UnPause();
  90.             }
  91.         }
  92.     }
  93.    
  94.     Vector2 scrollPos = Vector2.zero;
  95.     void OnGUI() {
  96.         if (ShowBag) {
  97.             window = new Rect((Adjust.orig.width - 640) / 2, (Adjust.orig.height -
  98.                                                        480) /2, 640, 480);
  99.             // render equip slots
  100.            
  101.             window = new Rect((Adjust.orig.width - 640) / 2, (Adjust.orig.height -
  102.                                                        480) /2, 230, 480);
  103.             GUI.Box(Adjust.FitToScreen(window), new Texture());
  104.             // Equip / Vanity toggle
  105.             ManageGear = GUI.Toggle(Adjust.FitToScreen(new Rect(window.x+window.width-55,window.y+5,50,30)), ManageGear, (ManageGear?SwitchOn:SwitchOff), new GUIStyle());
  106.             if (ManageGear) {
  107.                 // helmet
  108.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+30,50,64)), new Texture());
  109.                 // back
  110.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+65,window.y+30,50,64)), new Texture());
  111.                 // chest
  112.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+100,50,64)), new Texture());
  113.                 // hands
  114.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+65,window.y+100,50,64)), new Texture());
  115.                 // legs
  116.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+170,50,64)), new Texture());
  117.                 // chest
  118.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+240,50,64)), new Texture());
  119.             } else {
  120.                 // helmet
  121.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+30,50,64)), new Texture());
  122.                 // back
  123.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+65,window.y+30,50,64)), new Texture());
  124.                 // chest
  125.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+100,50,64)), new Texture());
  126.                 // hands
  127.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+65,window.y+100,50,64)), new Texture());
  128.                 // legs
  129.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+170,50,64)), new Texture());
  130.                 // chest
  131.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+240,50,64)), new Texture());
  132.             }
  133.             // render bag slots
  134.            
  135.             window = new Rect((Adjust.orig.width - 640) / 2 + 230, (Adjust.orig.height -
  136.                                                        480) /2, 410, 480);
  137.             GUI.Box(Adjust.FitToScreen(window), new Texture());
  138.            
  139.             int rows = ((int)(MaxInventorySpace / 8 + (MaxInventorySpace % 8 > 0? 1:0)));
  140.             scrollPos = GUI.BeginScrollView(Adjust.FitToScreen(new Rect(window.x,window.y+50,50*8+20,64*6+5)), scrollPos, new Rect(0,0,50*8,64*rows));
  141.  
  142.             for (int r = 0; r < rows; r++){
  143.                 int x = r*8;
  144.                 //Debug.Log("Starting on Row "+r+" with item slot "+x);
  145.                 for (int c = 0; c < 8; c++) {
  146.                     x = r*8+c;
  147.                     if (Items[x] != null)
  148.                         GUI.Box(Adjust.FitToScreen(new Rect(5+(50*c),5+(r*64),50,64)), textures[Items[x].ItemID]);
  149.                     else
  150.                         GUI.Box(Adjust.FitToScreen(new Rect(5+(50*c),5+(r*64),50,64)), new Texture());
  151.  
  152.                     if( Event.current.type == EventType.MouseDown && Adjust.FitToScreen(new Rect(5+(50*c),5+(r*64),50,64)).Contains(Event.current.mousePosition) )
  153.                     {
  154.                         if (Items[x] != null) {
  155.                             Selected.Slot = x;
  156.                             Selected.Item = Items[x];
  157.                             Selected.From = SourceItem.SourceLoc.Inventory;
  158.                         }
  159.                     }
  160.                     else if( Event.current.type == EventType.MouseUp)
  161.                     {
  162.                         if (Adjust.FitToScreen(new Rect(5+(50*c),5+(r*64),50,64)).Contains(Event.current.mousePosition)) {
  163.                             if (Items[x] != null)
  164.                                 Items[Selected.Slot] = Items[x];
  165.                             else
  166.                                 Items[Selected.Slot] = null;
  167.                             Items[x] = Selected.Item;
  168.                             Selected = new SourceItem(); // null out
  169.                         }
  170.                         //currentlyDragged = null;
  171.                     }
  172.                     x++;
  173.                     if (x > MaxInventorySpace)
  174.                         break;
  175.                 }
  176.                 if (x > MaxInventorySpace)
  177.                     break;
  178.             }
  179.             GUI.EndScrollView();
  180.         }
  181.         window = new Rect((Adjust.orig.width - 555) / 2, (Adjust.orig.height) - 79 , 555, 74);
  182.         // QuickSlots
  183.         GUI.Box(Adjust.FitToScreen(window), new Texture());
  184.         for (int q = 0; q < 10; q++) {
  185.             if (QuickSlots[q] != null)
  186.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+5+(55*q),window.y+5,50,64)), QuickSlots[q].Texture);
  187.             else
  188.                 GUI.Box(Adjust.FitToScreen(new Rect(window.x+5+(55*q),window.y+5,50,64)), QuickSlotBG);
  189.  
  190.             if( Event.current.type == EventType.MouseDown && Adjust.FitToScreen(new Rect(window.x+5+(55*q),window.y+5,50,64)).Contains(Event.current.mousePosition) )
  191.             {
  192.                 // store copy of item
  193.                 if (QuickSlots[q] != null) {
  194.                     Selected.Slot = q;
  195.                     Selected.Item = QuickSlots[q];
  196.                     Selected.From = SourceItem.SourceLoc.Quickslot;
  197.                 }
  198.             }
  199.             else if( Event.current.type == EventType.MouseUp && Adjust.FitToScreen(new Rect(window.x+5+(55*q),window.y+5,50,64)).Contains(Event.current.mousePosition))
  200.             {
  201.                 // swap items around
  202.                 //SwapItem(Selected, new SourceItem(q, QuickSlots[q], SourceItem.SourceLoc.Quickslot));
  203.                 if (QuickSlots[q] != null)
  204.                     QuickSlots[Selected.Slot] = QuickSlots[q];
  205.                 else
  206.                     QuickSlots[Selected.Slot] = null;
  207.                    
  208.                 QuickSlots[q] = Selected.Item;
  209.                 Selected.Item = null;
  210.                 //currentlyDragged = null;
  211.             }
  212.         }
  213.  
  214.         // render floating image
  215.         if( Event.current.type == EventType.MouseDrag && Selected.Item != null )
  216.         {
  217.             Rect selected = Adjust.FitToScreen(new Rect(Event.current.mousePosition.x,Event.current.mousePosition.y,50,64));
  218.             selected.x = Event.current.mousePosition.x;
  219.             selected.y = Event.current.mousePosition.y;
  220.             GUI.Box(selected, textures[Selected.Item.ItemID]);
  221.         }
  222.     }
  223.  
  224.     public bool AddItem(BagItem item) {
  225.         int count = 0;
  226.         int first = -1;
  227.         for(int i = 0; i < MaxInventorySpace; i++) {
  228.             if (Items[i] != null)
  229.                 count++;
  230.             else
  231.                 if (first == -1)
  232.                     first = i;
  233.         }
  234.         if (count >= MaxInventorySpace)
  235.             return false;
  236.         Items[first] = item;
  237.         return true;
  238.     }
  239.     public bool RemoveItem(string name) {
  240.         for(int i = 0; i < MaxInventorySpace; i++) {
  241.             if (Items[i] == null)
  242.                 continue;
  243.             if (Items[i].Name == name) {
  244.                 //Items.Remove(Items[i]);
  245.                 Items[i] = null;
  246.                 return true;
  247.             }
  248.         }
  249.         return false;
  250.     }
  251.     // WORK IN PROGRESS
  252.     public bool SwapItem(SourceItem source, SourceItem dest) {
  253.         if (dest.Item != null) {
  254.             switch(source.From) {
  255.             case SourceItem.SourceLoc.Quickslot:
  256.                 QuickSlots[source.Slot] = dest.Item;
  257.                 break;
  258.             default:
  259.                 break;
  260.             }
  261.         }
  262.             /*QuickSlots[Selected.Slot] = QuickSlots[q];
  263.             else
  264.                 QuickSlots[Selected.Slot] = null;*/
  265.  
  266.         //QuickSlots[q] = Selected.Item;
  267.         //Selected.Item = null;
  268.         return true;
  269.     }
  270.     public class BagItem {
  271.         public int Type { get { return ItemList.GetItem(ItemID).Type; } }
  272.         public int ItemID;
  273.         public int UniqueKEY;
  274.         public int Count;
  275.         public string Name { get { return ItemList.GetItem(ItemID).Name; } }
  276.         public Texture Texture { get { return ItemList.GetItem(ItemID).Texture; } }
  277.         public string Description { get { return ItemList.GetItem(ItemID).Description; } }
  278.         public GameObject handle { get { return ItemList.GetItem(ItemID).GameObject; } }
  279.  
  280.         public BagItem() {
  281.             ItemID = -1;
  282.             // TODO: replace with structured embedded data
  283.             UniqueKEY = 0;
  284.             this.Count = 1;
  285.         }
  286.         public BagItem(int ItemID, int Count) {
  287.             this.ItemID = ItemID;
  288.             this.Count = Count;
  289.         }
  290.     }
  291. }
  292. static class ItemType {
  293.     public const int Unfilterred = 0;
  294.     public const int Resource = 1;
  295.     public const int Usable = 2;
  296.     public const int Equippable = 3;
  297. }
  298. public static class ItemList {
  299.     public class Item {
  300.         public int Type;
  301.         public string Name;
  302.         public string Description;
  303.         public Texture Texture;
  304.         public int StackCount;
  305.         public GameObject GameObject;
  306.  
  307.         public Item(int Type, int StackCount, string Name, string Description, Texture Texture, GameObject gameObject) {
  308.             this.Type = Type;
  309.             this.StackCount = StackCount;
  310.             this.Name = Name;
  311.             this.Description = Description;
  312.             this.Texture = Texture;
  313.             this.GameObject = gameObject;
  314.         }
  315.     }
  316.     public static Item GetItem(int ID) {
  317.         switch(ID) {
  318.         case 0: // Wood
  319.             return new Item(ItemType.Resource, 250,"Wood", "Resource obtained from a tree.", new Texture(), null);
  320.         case 1: // Stone
  321.             return new Item(ItemType.Resource, 250,"Stone", "Congrats, you've got a rock.", new Texture(), null);
  322.         case 3: // LR Ammo
  323.             return new Item(ItemType.Resource, 250,"LR Ammo", "Long Rifle ammunition.", Resources.Load<Texture>("LRAmmo"), null);
  324.         case 1000: // MK2 Pistol
  325.             return new Item(ItemType.Equippable, 1,"Pistol MK2", "Relatively light-weight firearm. Uses Long Rifle rounds.", Resources.Load<Texture>("Pistol_MK2"), GameObject.Find("Pistol_MK2"));
  326.         default:
  327.             return null;
  328.         }
  329.     }
  330. }
Advertisement
Add Comment
Please, Sign In to add comment