Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class InventoryManager : MonoBehaviour {
- static InventoryManager instance;
- public bool ShowBag = true;
- public int MaxInventorySpace = 19;
- public Texture QuickSlotBG = new Texture();
- GUIStyle toggle = new GUIStyle();
- public Texture SwitchOn = new Texture();
- public Texture SwitchOff = new Texture();
- public BagItem[] Items = new BagItem[0];
- public BagItem[] QuickSlots = new BagItem[10];
- public class SourceItem {
- public int Slot = -1;
- public BagItem Item = null;
- public enum SourceLoc { NULL = -1, Inventory = 1, Quickslot = 2, Equipment = 3, Vanity = 4, Loot = 5 }
- public SourceLoc From = SourceLoc.NULL;
- public SourceItem() {
- Slot = -1;
- Item = null;
- From = SourceLoc.NULL;
- }
- public SourceItem(int Slot, BagItem Item, SourceLoc From) {
- this.Slot = Slot;
- this.Item = Item;
- this.From = From;
- }
- }
- //private int SelectedSlot = -1;
- //private BagItem SelectedItem = null;
- private SourceItem Selected = new SourceItem();
- public Dictionary<int, Texture> textures = new Dictionary<int, Texture>();
- bool ManageGear = true;
- static class Bag {
- public const int All = 0;
- public const int Resources = 1;
- public const int Equipment = 2;
- }
- //bool _ShowMenu = false;
- Rect window;
- void Awake() {
- instance = this;
- }
- // Use this for initialization
- void Start () {
- instance = this;
- Items = new BagItem[MaxInventorySpace];
- Items[0] = new BagItem(1000, 1);
- Items[1] = new BagItem(3, 200);
- }
- // Update is called once per frame
- void Update () {
- if (MainMenu.myInput.Inventory.isPressed()) {
- ShowBag = !ShowBag;
- if (ShowBag) {
- //Debug.Log(Items.Count+" items in inventory.");
- textures.Clear();
- foreach(BagItem item in Items) {
- if (item == null)
- continue;
- if (!textures.ContainsKey(item.ItemID))
- textures.Add(item.ItemID, ItemList.GetItem(item.ItemID).Texture);
- }
- Screen.lockCursor = false;
- Screen.showCursor = true;
- GameObject.Find("Player").GetComponent<Movement>().Pause();
- } else {
- Screen.lockCursor = true;
- Screen.showCursor = false;
- GameObject.Find("Player").GetComponent<Movement>().UnPause();
- }
- }
- if (Input.GetKeyDown(KeyCode.Escape) && ShowBag) {
- ShowBag = false;
- if (ShowBag) {
- Screen.lockCursor = false;
- Screen.showCursor = true;
- GameObject.Find("Player").GetComponent<Movement>().Pause();
- } else {
- Screen.lockCursor = true;
- Screen.showCursor = false;
- GameObject.Find("Player").GetComponent<Movement>().UnPause();
- }
- }
- }
- Vector2 scrollPos = Vector2.zero;
- void OnGUI() {
- if (ShowBag) {
- window = new Rect((Adjust.orig.width - 640) / 2, (Adjust.orig.height -
- 480) /2, 640, 480);
- // render equip slots
- window = new Rect((Adjust.orig.width - 640) / 2, (Adjust.orig.height -
- 480) /2, 230, 480);
- GUI.Box(Adjust.FitToScreen(window), new Texture());
- // Equip / Vanity toggle
- ManageGear = GUI.Toggle(Adjust.FitToScreen(new Rect(window.x+window.width-55,window.y+5,50,30)), ManageGear, (ManageGear?SwitchOn:SwitchOff), new GUIStyle());
- if (ManageGear) {
- // helmet
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+30,50,64)), new Texture());
- // back
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+65,window.y+30,50,64)), new Texture());
- // chest
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+100,50,64)), new Texture());
- // hands
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+65,window.y+100,50,64)), new Texture());
- // legs
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+170,50,64)), new Texture());
- // chest
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+240,50,64)), new Texture());
- } else {
- // helmet
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+30,50,64)), new Texture());
- // back
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+65,window.y+30,50,64)), new Texture());
- // chest
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+100,50,64)), new Texture());
- // hands
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+65,window.y+100,50,64)), new Texture());
- // legs
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+170,50,64)), new Texture());
- // chest
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+10,window.y+240,50,64)), new Texture());
- }
- // render bag slots
- window = new Rect((Adjust.orig.width - 640) / 2 + 230, (Adjust.orig.height -
- 480) /2, 410, 480);
- GUI.Box(Adjust.FitToScreen(window), new Texture());
- int rows = ((int)(MaxInventorySpace / 8 + (MaxInventorySpace % 8 > 0? 1:0)));
- 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));
- for (int r = 0; r < rows; r++){
- int x = r*8;
- //Debug.Log("Starting on Row "+r+" with item slot "+x);
- for (int c = 0; c < 8; c++) {
- x = r*8+c;
- if (Items[x] != null)
- GUI.Box(Adjust.FitToScreen(new Rect(5+(50*c),5+(r*64),50,64)), textures[Items[x].ItemID]);
- else
- GUI.Box(Adjust.FitToScreen(new Rect(5+(50*c),5+(r*64),50,64)), new Texture());
- if( Event.current.type == EventType.MouseDown && Adjust.FitToScreen(new Rect(5+(50*c),5+(r*64),50,64)).Contains(Event.current.mousePosition) )
- {
- if (Items[x] != null) {
- Selected.Slot = x;
- Selected.Item = Items[x];
- Selected.From = SourceItem.SourceLoc.Inventory;
- }
- }
- else if( Event.current.type == EventType.MouseUp)
- {
- if (Adjust.FitToScreen(new Rect(5+(50*c),5+(r*64),50,64)).Contains(Event.current.mousePosition)) {
- if (Items[x] != null)
- Items[Selected.Slot] = Items[x];
- else
- Items[Selected.Slot] = null;
- Items[x] = Selected.Item;
- Selected = new SourceItem(); // null out
- }
- //currentlyDragged = null;
- }
- x++;
- if (x > MaxInventorySpace)
- break;
- }
- if (x > MaxInventorySpace)
- break;
- }
- GUI.EndScrollView();
- }
- window = new Rect((Adjust.orig.width - 555) / 2, (Adjust.orig.height) - 79 , 555, 74);
- // QuickSlots
- GUI.Box(Adjust.FitToScreen(window), new Texture());
- for (int q = 0; q < 10; q++) {
- if (QuickSlots[q] != null)
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+5+(55*q),window.y+5,50,64)), QuickSlots[q].Texture);
- else
- GUI.Box(Adjust.FitToScreen(new Rect(window.x+5+(55*q),window.y+5,50,64)), QuickSlotBG);
- if( Event.current.type == EventType.MouseDown && Adjust.FitToScreen(new Rect(window.x+5+(55*q),window.y+5,50,64)).Contains(Event.current.mousePosition) )
- {
- // store copy of item
- if (QuickSlots[q] != null) {
- Selected.Slot = q;
- Selected.Item = QuickSlots[q];
- Selected.From = SourceItem.SourceLoc.Quickslot;
- }
- }
- 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))
- {
- // swap items around
- //SwapItem(Selected, new SourceItem(q, QuickSlots[q], SourceItem.SourceLoc.Quickslot));
- if (QuickSlots[q] != null)
- QuickSlots[Selected.Slot] = QuickSlots[q];
- else
- QuickSlots[Selected.Slot] = null;
- QuickSlots[q] = Selected.Item;
- Selected.Item = null;
- //currentlyDragged = null;
- }
- }
- // render floating image
- if( Event.current.type == EventType.MouseDrag && Selected.Item != null )
- {
- Rect selected = Adjust.FitToScreen(new Rect(Event.current.mousePosition.x,Event.current.mousePosition.y,50,64));
- selected.x = Event.current.mousePosition.x;
- selected.y = Event.current.mousePosition.y;
- GUI.Box(selected, textures[Selected.Item.ItemID]);
- }
- }
- public bool AddItem(BagItem item) {
- int count = 0;
- int first = -1;
- for(int i = 0; i < MaxInventorySpace; i++) {
- if (Items[i] != null)
- count++;
- else
- if (first == -1)
- first = i;
- }
- if (count >= MaxInventorySpace)
- return false;
- Items[first] = item;
- return true;
- }
- public bool RemoveItem(string name) {
- for(int i = 0; i < MaxInventorySpace; i++) {
- if (Items[i] == null)
- continue;
- if (Items[i].Name == name) {
- //Items.Remove(Items[i]);
- Items[i] = null;
- return true;
- }
- }
- return false;
- }
- // WORK IN PROGRESS
- public bool SwapItem(SourceItem source, SourceItem dest) {
- if (dest.Item != null) {
- switch(source.From) {
- case SourceItem.SourceLoc.Quickslot:
- QuickSlots[source.Slot] = dest.Item;
- break;
- default:
- break;
- }
- }
- /*QuickSlots[Selected.Slot] = QuickSlots[q];
- else
- QuickSlots[Selected.Slot] = null;*/
- //QuickSlots[q] = Selected.Item;
- //Selected.Item = null;
- return true;
- }
- public class BagItem {
- public int Type { get { return ItemList.GetItem(ItemID).Type; } }
- public int ItemID;
- public int UniqueKEY;
- public int Count;
- public string Name { get { return ItemList.GetItem(ItemID).Name; } }
- public Texture Texture { get { return ItemList.GetItem(ItemID).Texture; } }
- public string Description { get { return ItemList.GetItem(ItemID).Description; } }
- public GameObject handle { get { return ItemList.GetItem(ItemID).GameObject; } }
- public BagItem() {
- ItemID = -1;
- // TODO: replace with structured embedded data
- UniqueKEY = 0;
- this.Count = 1;
- }
- public BagItem(int ItemID, int Count) {
- this.ItemID = ItemID;
- this.Count = Count;
- }
- }
- }
- static class ItemType {
- public const int Unfilterred = 0;
- public const int Resource = 1;
- public const int Usable = 2;
- public const int Equippable = 3;
- }
- public static class ItemList {
- public class Item {
- public int Type;
- public string Name;
- public string Description;
- public Texture Texture;
- public int StackCount;
- public GameObject GameObject;
- public Item(int Type, int StackCount, string Name, string Description, Texture Texture, GameObject gameObject) {
- this.Type = Type;
- this.StackCount = StackCount;
- this.Name = Name;
- this.Description = Description;
- this.Texture = Texture;
- this.GameObject = gameObject;
- }
- }
- public static Item GetItem(int ID) {
- switch(ID) {
- case 0: // Wood
- return new Item(ItemType.Resource, 250,"Wood", "Resource obtained from a tree.", new Texture(), null);
- case 1: // Stone
- return new Item(ItemType.Resource, 250,"Stone", "Congrats, you've got a rock.", new Texture(), null);
- case 3: // LR Ammo
- return new Item(ItemType.Resource, 250,"LR Ammo", "Long Rifle ammunition.", Resources.Load<Texture>("LRAmmo"), null);
- case 1000: // MK2 Pistol
- 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"));
- default:
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment