Advertisement
Guest User

Inventory

a guest
May 10th, 2014
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.60 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. [AddComponentMenu ("Inventory/Inventory")]
  5. public class Inventory : MonoBehaviour {
  6.  
  7.     //This is the central piece of the Inventory System.
  8.     public Transform[] Contents; //The content of the Inventory
  9.     public int MaxContent = 12; //The maximum number of items the Player can carry.
  10.    
  11.     bool DebugMode = false; //If this is turned on the Inventory script will output the base of what it's doing to the Console window.
  12.    
  13.     private InventoryDisplay playersInvDisplay; //Keep track of the InventoryDisplay script.
  14.    
  15.     public Transform itemHolderObject; //The object the unactive items are going to be parented to. In most cases this is going to be the Inventory object itself.
  16.        
  17.     //Handle components and assign the itemHolderObject.
  18.     void Awake ()
  19.     {
  20.         itemHolderObject = gameObject.transform;
  21.        
  22.         playersInvDisplay = GetComponent<InventoryDisplay>();
  23.         if (playersInvDisplay == null)
  24.         {
  25.             Debug.LogError("No Inventory Display script was found on " + transform.name + " but an Inventory script was.");
  26.             Debug.LogError("Unless a Inventory Display script is added the Inventory won't show. Add it to the same gameobject as the Inventory for maximum performance");
  27.         }
  28.     }
  29.    
  30.     //Add an item to the inventory.
  31.     public void AddItem(Transform Item)
  32.     {
  33.         ArrayList newContents = new ArrayList(Contents);
  34.         newContents.Add(Item);
  35.         newContents.CopyTo(Contents); //Array to unity builtin array
  36.        
  37.         if (DebugMode)
  38.         {
  39.             Debug.Log(Item.name+" has been added to inventroy");
  40.         }
  41.        
  42.         //Tell the InventoryDisplay to update the list.
  43.         if (playersInvDisplay != null)
  44.         {
  45.             playersInvDisplay.UpdateInventoryList();
  46.         }
  47.     }
  48.    
  49.     //Removed an item from the inventory (IT DOESN'T DROP IT).
  50.     public void RemoveItem(Transform Item)
  51.     {
  52.         ArrayList newContents = new ArrayList();
  53.         int index = 0;
  54.         bool shouldend = false;
  55.         foreach(Transform i in newContents) //Loop through the Items in the Inventory:
  56.         {
  57.             if(i == Item) //When a match is found, remove the Item.
  58.             {
  59.                 newContents.RemoveAt(index);
  60.                 shouldend=true;
  61.                 //No need to continue running through the loop since we found our item.
  62.             }
  63.             index++;
  64.            
  65.             if(shouldend) //Exit the loop
  66.             {
  67.                 Contents=newContents.ToArray(typeof (Transform)) as Transform[];
  68.                 if (DebugMode)
  69.                 {
  70.                     Debug.Log(Item.name+" has been removed from inventroy");
  71.                 }
  72.                 if (playersInvDisplay != null)
  73.                 {
  74.                     playersInvDisplay.UpdateInventoryList();
  75.                 }
  76.                 return;
  77.             }
  78.         }
  79.     }
  80.    
  81.     //Dropping an Item from the Inventory
  82.     public void DropItem(Item item)
  83.     {
  84.         gameObject.SendMessage ("PlayDropItemSound", SendMessageOptions.DontRequireReceiver); //Play sound
  85.        
  86.         bool makeDuplicate = false;
  87.         if (item.stack == 1) //Drop item
  88.         {
  89.             RemoveItem(item.transform);
  90.         }
  91.         else //Drop from stack
  92.         {
  93.             item.stack -= 1;
  94.             makeDuplicate = true;
  95.         }
  96.        
  97.         item.DropMeFromThePlayer(makeDuplicate); //Calling the drop function + telling it if the object is stacked or not.
  98.        
  99.         if (DebugMode)
  100.         {
  101.             Debug.Log(item.name + " has been dropped");
  102.         }
  103.     }
  104.    
  105.     //This will tell you everything that is in the inventory.
  106.     void DebugInfo()
  107.     {
  108.         Debug.Log("Inventory Debug - Contents");
  109.         int items = 0;
  110.         foreach(Transform i in Contents){
  111.             items++;
  112.             Debug.Log(i.name);
  113.         }
  114.         Debug.Log("Inventory contains "+items+" Item(s)");
  115.     }
  116.    
  117.     //Drawing an 'S' in the scene view on top of the object the Inventory is attached to stay organized.
  118.     void OnDrawGizmos ()
  119.     {
  120.         Gizmos.DrawIcon (new Vector3(transform.position.x, transform.position.y + 2.3f, transform.position.z), "InventoryGizmo.png", true);
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement