Advertisement
kasru

Icon Based Inventory

Feb 4th, 2013
2,695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //****** Donations are greatly appreciated.  ******
  2. //****** You can donate directly to Jesse through paypal at  https://www.paypal.me/JEtzler   ******
  3.  
  4. //Our inventory
  5. var inventory : Array;
  6.  
  7. //This will be drawn when a slot is empty
  8. public var emptyTex : Texture;
  9.  
  10. //the size of the inventory in x and y dimension
  11. public var inventorySizeX = 8;
  12. public var inventorySizeY = 5;
  13.  
  14. //The pixel size (height and width) of an inventory slot
  15. var iconWidthHeight = 20;
  16.  
  17. //Space between slots (in x and y)
  18. var spacing = 4;
  19.  
  20. //set the position of the inventory
  21. public var offSet = Vector2( 100, 100 );
  22.  
  23. // TEST VARIABLES
  24. // Assign these to test adding Items with mouse clicks (see Update())
  25. public var testTex : Texture;
  26. public var testTex2 : Texture;
  27. static var hideInventory:boolean = false;
  28.  
  29. //Our Representation of an InventoryItem
  30. class InventoryItem
  31. {
  32.     //GameObject this item refers to
  33.     var worldObject : GameObject;
  34.     //What the item will look like in the inventory
  35.     var texRepresentation : Texture;
  36. }
  37.  
  38. // Create the Inventory
  39. function Awake()
  40. {
  41.     inventory = new Array(inventorySizeX);
  42.    
  43.         for( var i = 0; i < inventory.length; i ++ ) {
  44.             inventory[i] = new Array(inventorySizeY);
  45.         }
  46. }
  47.  
  48. function OnGUI()
  49. {
  50.     var texToUse : Texture;
  51.     var currentInventoryItem : InventoryItem;
  52.    
  53. if(hideInventory == false) {
  54.    
  55.     //Go through each row
  56.     for( var i = 0; i < inventory.length; i ++ )
  57.     {
  58.         // and each column
  59.         for( var k = 0; k < inventory[i].length; k ++ )
  60.         {
  61.             texToUse = emptyTex;
  62.             currentInventoryItem = inventory[i][k];
  63.            
  64.             //if there is an item in the i-th row and the k-th column, draw it
  65.             if( inventory[i][k] != null )
  66.             {
  67.                 texToUse = currentInventoryItem.texRepresentation;
  68.             }
  69.            
  70.             GUI.DrawTexture( new Rect( offSet.x+k*(iconWidthHeight+spacing), offSet.y+i*(iconWidthHeight+spacing), iconWidthHeight, iconWidthHeight ), texToUse );
  71.         }
  72.     }  
  73.     }
  74. }
  75.  
  76.  function AddItem( item : InventoryItem )
  77. {
  78.      //Go through each row
  79.     for( var i = 0; i < inventory.length; i ++ )
  80.     {
  81.         // and each column
  82.         for( var k = 0; k < inventory[i].length; k ++ )
  83.         {
  84.             //If the position is empty, add the new item and exit the function
  85.             if( inventory[i][k] == null )
  86.             {
  87.                  inventory[i][k] = item;
  88.                  return;
  89.             }
  90.         }
  91.     }  
  92.    
  93.     //If we got this far, the inventory is full, do somethign appropriate here 
  94. }
  95.  
  96. function AddItem( worldObject : GameObject, texRep : Texture ) {
  97.     var newItem = new InventoryItem(); 
  98.     newItem.worldObject = worldObject;
  99.     newItem.texRepresentation = texRep;
  100.     AddItem( newItem );
  101. }
  102.  
  103. function Update() {
  104.  
  105.     if(Input.GetKeyDown(KeyCode.K)) {
  106.         AddItem( gameObject, testTex );
  107.     }
  108.    
  109.     if(Input.GetKeyDown(KeyCode.L)) {
  110.         AddItem( gameObject, testTex2 );   
  111.     }  
  112. }
  113.  
  114. function buyShopItem1 () {
  115.     AddItem( gameObject, testTex );
  116. }
  117.  
  118. function buyShopItem2() {
  119.  
  120.     AddItem( gameObject, testTex2 );   
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement