Advertisement
Guest User

Inventory.cs

a guest
Oct 14th, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Inventory : MonoBehaviour {
  5.  
  6.     // Self explanatory..
  7.     public int InventoryWidth = 400;
  8.     public int InventoryHeight = 400;
  9.     public Item StartingItem; // The item the player start with.
  10.  
  11.     private ItemStack LeftHand; // Item the player is holding in left hand, this variable isn't used for anything yet.
  12.     private ItemStack RightHand; // Item the player is holding in right hand, this variable isn't used for anything yet.
  13.    
  14.     private ItemStack[] Items = new ItemStack[26]; // Array of itemstacks. This holds the player's items.
  15.  
  16.     private ItemStack SelectedItem; // When you click on an item it becomes the selected item, and info about it is
  17.                     // shown in a box at the top.
  18.  
  19.     private bool InventoryOpen = false;
  20.     private bool SeeingItem = false; // Just a bool for if the box with info about item in it is showing or not.
  21.  
  22.     void Start() {
  23.         AddItem(StartingItem);
  24.     }
  25.  
  26.     public void AddItem(Item item) {
  27.  
  28.                 // Check if a stack is already there for us to stack the item with
  29.             for(int i = 0; i <= 25; i++) {
  30.  
  31.                     // If nothing is in this inventory slot, check the next one.
  32.                     if(Items[i] == null) {
  33.                         continue;
  34.                     }
  35.  
  36.                         // If stack is found, increase the stack size.
  37.                     if(Items[i].ContainingItem.Name == item.Name) {
  38.                         Items[i].StackSize++;
  39.                         return;
  40.                     }
  41.    
  42.             }
  43.  
  44.                 // Create a new item stack.
  45.             ItemStack st = new ItemStack(item, 1);
  46.  
  47.                 // If no stacks of the same item is found, then
  48.                 // check for space in inventory to put a new item stack into.
  49.             for(int i = 0; i <= 25; i++) {
  50.                         // If item in inventory slot is null (no itemstack inside)
  51.                     if(Items[i] == null) {
  52.                                 // Add a new item stack.
  53.                         Items[i] = st;
  54.                         return;
  55.                     }
  56.             }
  57.  
  58.             /*foreach(ItemStack i in Items) {
  59.                     Debug.Log(i.ContainingItem.Name);
  60.             }*/
  61.         }
  62.  
  63.     void Update() {
  64.         if(Input.GetKeyDown(KeyCode.E)) {
  65.             InventoryOpen = !InventoryOpen;
  66.             SeeingItem = false;
  67.         }
  68.     }
  69.  
  70.     void OnGUI() {
  71.         if(InventoryOpen) {
  72.             var pos = new Vector2(Screen.width / 2 - InventoryWidth / 2, Screen.height / 2 - InventoryHeight / 2);
  73.            
  74.  
  75.  
  76.             if(SeeingItem) {
  77.                 pos.y += 75;
  78.                 GUI.Box( new Rect(pos.x, pos.y - 110, InventoryWidth, 100), "Selected Item: " + SelectedItem.ContainingItem.Name );
  79.             }
  80.  
  81.             // Just some fancy maths for the rest of the script, doesn't have anything to do with my problem
  82.             GUI.Box( new Rect(pos.x, pos.y, InventoryWidth, InventoryHeight), "Inventory" );
  83.  
  84.             var buttonnum = 0;
  85.             for(int y = 1; y <= 5; y++) {
  86.                 for(int x = 1; x <= 5; x++) {
  87.                     buttonnum++;
  88.                     var posx = pos.x + x * 60;
  89.                     var posy = pos.y + y * 60;
  90.                     if(x == 1) {
  91.                         posx = pos.x + x * 55;
  92.                     }
  93.                     if(y == 1) {
  94.                         posy = pos.y + y * 55;
  95.                     }
  96.                     if( Items[buttonnum] == null ) {
  97.                         // NO ITEM
  98.                         if( GUI.Button(new Rect(posx, posy, 50, 50), "" ) ) {
  99.                             // DO NOTHING
  100.                         }
  101.                     }else{
  102.                         if( GUI.Button(new Rect(posx, posy, 50, 50), Items[buttonnum].ContainingItem.Icon ) ) {
  103.                             SelectedItem = Items[buttonnum];
  104.                             SeeingItem = true;
  105.                         }
  106.                     }
  107.                 }
  108.             }
  109.  
  110.            
  111.  
  112.             buttonnum = 0;
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement