Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // InventoryManager.js
- // Manages all the inventory items, looting and other item related systems for the player
- // Attach to your Inventory Manager object
- // C# doesn't support Array. You could use ArrayList instead.
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class InventoryManager : MonoBehaviour {
- #region Variables
- public UIButton emptyInventorySlot;
- public UIButtonDetails bagSlots;
- public UIButtonDetails packSlots;
- public UIButtonDetails equippedSlots;
- // public UIButton[] equippedSlotUIButton; // The array with button refs WHAT?? Bad Idea. Fixed. Walk away.
- public EquipmentSlot[] equippedSlot; // Our list to keep track of our Equipment Slots
- public ListPanel lootPanel;
- // public UIListItemContainer[] lootPanelContainers;
- public UIListButton[] lootPanelItems;
- private InventoryItem[] inventory; // Our master inventory (private)
- private InventoryItem[] equipped; // Our equipped inventory (private)
- private InventoryItem[] currentLootArray; // The array to contain the loot being passed to and from the LootableObject
- private CurrentBag currentBag;
- // Combat System // Do we want to pass on the target icon for the lootable object?
- // private GameObject currentTarget; // The Current Target for the player
- // public GameObject currentTargetIcon;
- private bool openInventoryPanel; // Controls OnGUI and opens/closes the Inventory window
- private bool openCharacterPanel; // Controls OnGUI and opens/closes the Character window - assigned but its value is never used
- private bool openLootPanel; // Controls OnGUI and opens/closes the Loot window
- private LootableObject currentLootableObject; // The pointer to the current lootable object being processed
- private LootableObject newLootableObject; // The pointer to a new lootable object to be processed
- private MessageManager messageManager;
- private PanelManager panelManager;
- private QuestManager questManager;
- private GameManager gameManager;
- static private InventoryManager inventoryManager;
- #endregion
- // Singleton Support
- // This can be cached on start as private InventoryManager inventoryManager;
- // & in Start() inventoryManager = InventoryManager.Instance();
- // Once cached, requires function calls as: inventoryManager.SomeFunction ();
- public static InventoryManager Instance () {
- if (! inventoryManager) {
- inventoryManager = FindObjectOfType(typeof (InventoryManager)) as InventoryManager;
- if (! inventoryManager)
- Debug.LogError ("There needs to be one active InventoryManager script on a GameObject in your scene.");
- }
- return inventoryManager;
- }
- void Awake () {
- currentLootableObject = null;
- currentBag = CurrentBag.Bag1;
- openInventoryPanel = false; // Move these to PanelManager?
- openCharacterPanel = false; // Move these to PanelManager?
- openLootPanel = false; // Move these to PanelManager?
- inventory = new InventoryItem [80]; // Create & init the array to hold the inventory. 4 bags of 20 slots = 80 items.
- for (int i = 0; i < inventory.Length; i++) {
- inventory[i] = null;
- }
- equipped = new InventoryItem [12]; // Create & init the array to hold the equipped items. 12 set slots.
- for (int j = 0; j < equipped.Length; j++) {
- equipped[j] = null;
- }
- }
- void Start () {
- messageManager = MessageManager.Instance();
- panelManager = PanelManager.Instance();
- questManager = QuestManager.Instance();
- gameManager = GameManager.Instance();
- CreateInventorySlots ();
- CreateCharacterSlots ();
- CreateLootPanelItems ();
- }
- void CreateInventorySlots () {
- // int inventoryLength = inventorySlots.arrayLength;
- // inventorySlots.array = new UIButton [inventoryLength];
- bagSlots.array = new UIButton [bagSlots.arrayLength]; // Refactor inventorySlots as this is not correct. It's really bagSlots or something
- int j;
- int k;
- Vector3 offset;
- // for (int i = 0; i < inventoryLength; i ++) {
- for (int i = 0; i < bagSlots.array.Length; i ++) {
- bagSlots.array[i] = (UIButton)Instantiate(bagSlots.slot, bagSlots.parentTransform.position, Quaternion.identity);
- offset = bagSlots.array[i].transform.localPosition;
- offset.z = offset.z -12;
- bagSlots.array[i].transform.localPosition = offset;
- bagSlots.array[i].transform.parent = bagSlots.parentTransform;
- bagSlots.array[i].renderCamera = bagSlots.renderCamera;
- bagSlots.array[i].SetValueChangedDelegate(UseAttempt);
- j = i % bagSlots.gridWidth;
- k = i / bagSlots.gridWidth;
- bagSlots.array[i].offset.x = bagSlots.startPositon.x + j * (bagSlots.slotSize + bagSlots.slotSpacing);
- bagSlots.array[i].offset.y = bagSlots.startPositon.y - k * (bagSlots.slotSize + bagSlots.slotSpacing);
- }
- int bagsLength = packSlots.arrayLength;
- packSlots.array = new UIButton [bagsLength];
- for (int i = 0; i < bagsLength; i ++) {
- packSlots.array[i] = (UIButton)Instantiate(packSlots.slot, packSlots.parentTransform.position, Quaternion.identity);
- offset = packSlots.array[i].transform.localPosition;
- offset.z = offset.z -12;
- packSlots.array[i].transform.localPosition = offset;
- packSlots.array[i].transform.parent = packSlots.parentTransform;
- packSlots.array[i].renderCamera = packSlots.renderCamera;
- packSlots.array[i].scriptWithMethodToInvoke = packSlots.script;
- packSlots.array[i].methodToInvoke = packSlots.method;
- j = i % packSlots.gridWidth;
- k = i / packSlots.gridWidth;
- packSlots.array[i].offset.x = packSlots.startPositon.x + j * (packSlots.slotSize + packSlots.slotSpacing);
- packSlots.array[i].offset.y = packSlots.startPositon.y - k * (packSlots.slotSize + packSlots.slotSpacing);
- }
- bagSlots.parentTransform.gameObject.SetActiveRecursively(false);
- }
- void CreateCharacterSlots () {
- int equippedLength = equippedSlots.arrayLength;
- equippedSlots.array = new UIButton[equippedLength];
- for (int i = 0; i < equippedLength; i++) {
- equippedSlots.array[i] = (UIButton)Instantiate(equippedSlot[i].slotIcon, equippedSlots.parentTransform.position, Quaternion.identity);
- equippedSlots.array[i].transform.parent = equippedSlots.parentTransform;
- equippedSlots.array[i].transform.localPosition = equippedSlot[i].slotOffset;
- equippedSlots.array[i].renderCamera = equippedSlots.renderCamera;
- equippedSlots.array[i].scriptWithMethodToInvoke = equippedSlots.script;
- equippedSlots.array[i].methodToInvoke = equippedSlots.method;
- }
- equippedSlots.parentTransform.gameObject.SetActiveRecursively(false);
- }
- void CreateLootPanelItems () {
- lootPanelItems = new UIListButton[lootPanel.listLength];
- for (int i = 0; i < lootPanelItems.Length; ++i) {
- lootPanelItems[i] = (UIListButton)Instantiate(lootPanel.listButton, lootPanel.parentTransform.position, Quaternion.identity);
- lootPanelItems[i].transform.parent = lootPanel.parentTransform;
- lootPanelItems[i].SetValueChangedDelegate(LootAttempt);
- lootPanelItems[i].gameObject.SetActiveRecursively(false);
- }
- lootPanel.list.gameObject.SetActiveRecursively(false);
- }
- public void LootRequest (LootableObject newLootableObject, InventoryItem[] newLootArray) {
- if (currentLootableObject != newLootableObject) { // If the LootableObject has changed...
- if (currentLootableObject != null) { // ... and if the current Lootable is not Null ...
- currentLootableObject.LootArray = currentLootArray; // ... replace the contents with the updated contents. (Refactored from lootArray)
- // currentLootableObject.UpdateLootArray(lootArray); // ... replace the contents with the updated contents.
- }
- currentLootableObject = newLootableObject; // Set the new Lootable Item the Current Lootable Item
- currentLootArray = newLootArray; // Set the Content Array to the new Content Array
- openLootPanel = true; // Set the open trap on the Loot Window to "true", so if it's open is stays open, if not, it opens it
- openInventoryPanel = true; // Set the open trap to the Inventory Window "true", so if it's open is stays open, if not, it opens it
- } else {
- openLootPanel = !openLootPanel; // This toggles the loot window
- if (openLootPanel) // If the user clicks on the same Lootable Object, the window closes
- openInventoryPanel = true;
- }
- ToggleLootPanel(openLootPanel);
- ToggleInventoryPanel(openInventoryPanel);
- }
- void ToggleLootPanel (bool open) {
- GameObject newUIButtonObject;
- UIButton newUIButton;
- if (open) {
- lootPanel.list.gameObject.active = true;
- lootPanel.list.ClearList(false);
- lootPanel.list.ScrollListTo(0);
- for (int i = 0; i < currentLootArray.Length; i ++) {
- newUIButtonObject = (currentLootArray[i] as InventoryItem).itemIcon as GameObject;
- newUIButton = newUIButtonObject.GetComponent<UIButton>();
- if (newUIButton != null) {
- lootPanelItems[i].gameObject.SetActiveRecursively(true);
- lootPanelItems[i].Copy((SpriteRoot)newUIButton, ControlCopyFlags.Appearance | ControlCopyFlags.Data);
- lootPanelItems[i].data = currentLootArray[i];
- lootPanel.list.AddItem (lootPanelItems[i], currentLootArray[i].itemName);
- } else {
- Debug.Log ("GameManager.ToggleLootPanel is Null at " + i);
- }
- }
- } else {
- lootPanel.list.ClearList(false);
- lootPanel.list.ScrollListTo(0);
- lootPanel.list.gameObject.SetActiveRecursively(false);
- for (int j = 0; j < lootPanelItems.Length; ++j) {
- lootPanelItems[j].transform.parent = lootPanel.parentTransform;
- }
- }
- }
- public void RefreshInventoryPanel () {
- int j = ((int)currentBag) * 20; // Sets the Range of the Current Open Bag [0, 1, 2, 3, 4 => 0, 20, 40, 60, 80]
- Debug.Log ("bagSlots.array.Length: " + bagSlots.array.Length);
- for (int i = 0; i < bagSlots.array.Length; i++) {
- if (inventory[i+j] == null) {
- Debug.Log("Inventory Slot is null at: " + (i+j));
- bagSlots.array[i].Copy((SpriteRoot)emptyInventorySlot, ControlCopyFlags.Appearance | ControlCopyFlags.State);
- bagSlots.array[i].UpdateCollider();
- } else {
- Debug.Log("Inventory Slot is else at: " + (i+j));
- bagSlots.array[i].Copy((SpriteRoot)inventory[i+j].itemIconButton, ControlCopyFlags.Appearance | ControlCopyFlags.State);
- bagSlots.array[i].UpdateCollider();
- }
- }
- }
- void LootAttempt (IUIObject obj) {
- UIListButton newUILIstButton = (UIListButton)obj; // Extract the UIListButton from the IUIObject
- InventoryItem item = (InventoryItem)newUILIstButton.data; // Get the Inventory Item (This means the InventoryItem must be saved to the base .data)
- bool success = AddItem(item);
- if (success)
- lootPanel.list.RemoveItem (newUILIstButton, false);
- }
- void UseAttempt (IUIObject obj) {
- UIButton thisUIButton = (UIButton)obj; // Extract the UIButton from the IUIObject
- if (thisUIButton == null)
- Debug.Log ("No UIButton");
- else { // CONTINUE
- InventoryItem thisItem = (InventoryItem)thisUIButton.data; // Get the Inventory Item
- if (thisItem == null)
- Debug.Log ("This Button Contains No Item!");
- else { // CONTINUE
- if (thisItem.slotType == SlotType.Bag) { // Is this the BEST solution?
- bool success = UseItem(thisItem);
- if (success) {
- // remove item from the inventory array
- int thisItemsIndex = InventoryItem[].IndexOf(inventory, thisItem);
- // int myIndex = Array.IndexOf( myArray, myString );
- // int thisItemsIndex = InventoryItem[].IndexOf(inventory, thisItem);
- // Debug.Log("This item's index is " + thisItemsIndex);
- inventory[thisItemsIndex] = null;
- // remove image from the button
- thisUIButton.Copy((SpriteRoot)emptyInventorySlot, ControlCopyFlags.Appearance | ControlCopyFlags.State);
- thisUIButton.UpdateCollider();
- messageManager.Message(thisItem.itemName + " has been used.\nRemoving " + thisItem.itemName);
- }
- } else {
- bool success = EquipItem(thisItem);
- if (success) {
- // remove item from the inventory array
- // int thisItemsIndex = InventoryItem[].IndexOf(inventory, thisItem);
- // Debug.Log("This item's index is " + thisItemsIndex);
- // remove image from the button
- thisUIButton.Copy((SpriteRoot)emptyInventorySlot, ControlCopyFlags.Appearance | ControlCopyFlags.State);
- thisUIButton.UpdateCollider();
- messageManager.Message(thisItem.itemName + " has been equipped.\nRemoving " + thisItem.itemName);
- }
- }
- }
- }
- }
- bool AddItem (InventoryItem item) {
- for (int i = 0; i < inventory.Length; i ++) { // Go through each row
- if (inventory[i] == null) { // If the position is empty...
- inventory[i] = item; // ... add the new item....
- Debug.Log ("Adding Item " + item.itemName + " to the inventory");
- Debug.Log ("Item " + inventory[i].itemName + " is in the inventory at index " + i);
- bagSlots.array[i].Copy((SpriteRoot)item.itemIconButton, ControlCopyFlags.Appearance | ControlCopyFlags.Data | ControlCopyFlags.State);
- bagSlots.array[i].UpdateCollider();
- bagSlots.array[i].data = item;
- if (item.isQuestObjective) // ... check the item against the quest log...
- questManager.CheckQuests (item);
- return (true); // ... and exit the function.
- }
- }
- Debug.Log ("Inventory is full");
- messageManager.Message("Inventory is full!", Color.yellow);
- return (false);
- }
- bool EquipItem (InventoryItem item) {
- for (int i = 0; i < equipped.Length; i ++) {
- if (equippedSlot[i].slotType == item.slotType) {
- if (equipped[i] == null) {
- equipped[i] = item;
- return (true);
- } else {
- Debug.Log ("Cannot Equip this Item - Slot is full");
- messageManager.Message("Cannot Equip this Item!", Color.yellow);
- messageManager.Message("The " + equipped[i].slotType + " slot is full.", Color.yellow);
- return (false);
- }
- }
- }
- Debug.Log ("Cannot Equip this Item");
- messageManager.Message("Cannot Equip this Item!", Color.yellow);
- return (false);
- }
- bool UseItem (InventoryItem item) {
- messageManager.Message ("Using Item " + item.itemName, Color.red);
- return (true);
- }
- void CloseLootPanel () {
- ToggleLootPanel (false);
- openLootPanel = false;
- }
- void ToggleInventoryPanel (bool open) {
- panelManager.ToggleInventoryPanel (open);
- }
- public void LootableObjectOutofRange () { //close the windows, clear the scrollList,etc.
- openLootPanel = false;
- openInventoryPanel = false;
- }
- public InventoryItem[] GetInventory () {
- return inventory;
- }
- #region OldCode
- // With Containers:
- // void CreateLootPanelItems () {
- // lootPanelContainers = new UIListItemContainer[lootPanel.listLength];
- // for (int i = 0; i < lootPanelItems.Length; ++i) {
- // // With Container:
- // lootPanelContainers[i] = (UIListItemContainer)Instantiate(lootPanel.listContainer, lootPanel.parentTransform.position, Quaternion.identity);
- // lootPanelContainers[i].transform.parent = lootPanel.parentTransform;
- // lootPanelItems[i] = lootPanelContainers[i].GetComponentInChildren <UIListButton>();
- // lootPanelItems[i].scriptWithMethodToInvoke = this;
- // lootPanelItems[i].methodToInvoke = "AddItem";
- // lootPanelContainers[i].gameObject.SetActiveRecursively(false);
- // }
- // }
- //void AddItem (InventoryItem item) {
- // for (int i = 0; i < inventory.length; i ++) { // Go through each row
- // if (inventory[i] == null) { // If the position is empty...
- // inventory[i] = item; // ... add the new item....
- // if (item.isQuestObjective) // ... check the item against the quest log...
- // CheckQuests (item);
- // return (true); // ... and exit the function.
- // }
- //// itemButton.Copy(InvPalette.instance.list[inItem.btnIndex]);
- // }
- //
- // Debug.Log ("Inventory is full");
- // return (false);
- //}
- //void EquipItem (InventoryItem item) {
- // for (int i = 0; i < equipped.length; i ++) {
- // if (equippedSlot[i].slotType == item.slotType) {
- // if (equipped[i] == null) {
- // equipped[i] = item;
- // return (true);
- // }
- // }
- // }
- // Debug.Log ("Cannot Equip this Item");
- // return (false);
- //}
- // OLD CODE OLD CODE
- //void ToggleLootPanel ( bool open ){
- // GameObject newUIButtonObject;
- // UIButton newUIButton;
- // Debug.Log ("ToggleLootPanel");
- // if (open) {
- // lootPanel.list.gameObject.active = true;
- // lootPanel.list.ClearList(false);
- // lootPanel.list.ScrollListTo(0);
- // for (int i = 0; i < lootArray.length; i ++) {
- // Debug.Log ("Displaying Item: " + i + ". Current List Count: " + lootPanel.list.Count + ".");
- // newUIButtonObject = (lootArray[i] as InventoryItem).itemIcon as GameObject;
- // newUIButton = newUIButtonObject.GetComponent<UIButton>();
- // if (newUIButton != null) {
- //// lootPanelItems[i].gameObject.SetActiveRecursively(true);
- //// FIXME_VAR_TYPE clone= Instantiate (newUIButtonObject, transform.position + Vector3 (0 + (35 * i), 125, 0), Quaternion.identity); // Testing
- //// FIXME_VAR_TYPE clone2= Instantiate (newUIButton.gameObject, transform.position + Vector3 (0 + (35 * i), 75, 0), Quaternion.identity); // Testing
- //
- // lootPanelItems[i].Copy(newUIButton); // This is trying to copy a UIButton into a UIListButton but it doesn't work....
- //// FIXME_VAR_TYPE clone3= Instantiate (lootPanelItems[i].gameObject, transform.position + Vector3 (0 + (35 * i), 40, 0), Quaternion.identity); // Testing
- // lootPanelItems[i].data = lootArray[i];
- // Debug.Log ("LootArray[" + i + "] = " + lootArray[i]);
- // lootPanel.list.AddItem (lootPanelItems[i], lootArray[i].itemName);
- // } else {
- // Debug.Log ("GameManager.ToggleLootPanel is Null at " + i);
- // }
- // }
- // } else {
- // lootPanel.list.ClearList(false);
- // lootPanel.list.ScrollListTo(0);
- // lootPanel.list.gameObject.SetActiveRecursively(false);
- // for (int j = 0; j < lootPanelItems.length; ++j) {
- // gameController.lootPanelItems[j].transform.parent = gameController.lootPanel.parentTransform;
- // }
- // }
- //}
- //
- //
- //void AddItem ( IUIObject obj ){
- ////void AddLootItem ( UIObject obj ){
- // UIListButton btn = obj;
- // InventoryItem item = btn.data;
- // for (int i = 0; i < inventory.length; i ++) { // Go through each row
- // if (inventory[i] == null) { // If the position is empty...
- // inventory[i] = item; // ... add the new item....
- // Debug.Log ("Adding Item " + item.itemName + " to the inventory");
- // if (item.isQuestObjective) // ... check the item against the quest log...
- // CheckQuests (item);
- // return (true); // ... and exit the function.
- // }
- //// itemButton.Copy( InvPalette.instance.list[inItem.btnIndex] );
- // }
- //
- // Debug.Log ("Inventory is full");
- // return (false);
- //}
- //
- //
- //void EquipItem ( InventoryItem item ){
- // for (int i = 0; i < equipped.length; i ++) {
- // if (equippedSlot[i].slotType == item.slotType) {
- // if (equipped[i] == null) {
- // equipped[i] = item;
- // return (true);
- // }
- // }
- // }
- // Debug.Log ("Cannot Equip this Item");
- // return (false);
- //}
- //
- //
- //void ToggleInventoryPanel ( bool open ){
- // if (open) {
- // inventoryPanel.StartTransition(UIPanelManager.SHOW_MODE.BringInForward);
- // } else {
- // inventoryPanel.StartTransition(UIPanelManager.SHOW_MODE.DismissForward);
- // }
- //}
- //
- //
- //void LootableObjectOutofRange (){ //close the windows, clear the scrollList,etc.
- // openLootPanel = false;
- // openInventoryPanel = false;
- //}
- //void AddItem ( InventoryItem item ){
- // for (int i = 0; i < inventory.length; i ++) { // Go through each row
- // if (inventory[i] == null) { // If the position is empty...
- // inventory[i] = item; // ... add the new item....
- // if (item.isQuestObjective) // ... check the item against the quest log...
- // CheckQuests (item);
- // return (true); // ... and exit the function.
- // }
- //// itemButton.Copy( InvPalette.instance.list[inItem.btnIndex] );
- // }
- //
- // Debug.Log ("Inventory is full");
- // return (false);
- //}
- //void EquipItem ( InventoryItem item ){
- // for (int i = 0; i < equipped.length; i ++) {
- // if (equippedSlot[i].slotType == item.slotType) {
- // if (equipped[i] == null) {
- // equipped[i] = item;
- // return (true);
- // }
- // }
- // }
- // Debug.Log ("Cannot Equip this Item");
- // return (false);
- //}
- // void CreateCharacterSlots () {
- // // EquipmentSlot currentSlot; // This seems to be here for the equip functions
- //
- // int equippedLength = equippedSlots.arrayLength;
- // equippedSlots.array = new UIButton[equippedLength];
- //
- // UIButton currentUIButton; // 'currentUIButton' is declared but never used
- // for (int i = 0; i < equippedLength; i++) {
- // equippedSlots.array[i] = (UIButton)Instantiate(equippedSlotUIButton[i], equippedSlots.parentTransform.position, Quaternion.identity);
- // equippedSlots.array[i].transform.parent = equippedSlots.parentTransform;
- //// equippedSlots.array[i].transform.localPosition = equippedSlot[i].slotOffset;
- // // equippedSlots.array[i].transform.localPosition.z = -12;
- // // equippedSlots.array[i].transform.localRotation = Quaternion.identity;
- // equippedSlots.array[i].renderCamera = equippedSlots.renderCamera;
- // equippedSlots.array[i].scriptWithMethodToInvoke = equippedSlots.script;
- // equippedSlots.array[i].methodToInvoke = equippedSlots.method;
- // // equippedSlots.array[i].offset.x = equippedSlot[i].slotOffset.x;
- // // equippedSlots.array[i].offset.y = equippedSlot[i].slotOffset.y;
- //
- // }
- // equippedSlots.parentPanelObject.SetActiveRecursively(false);
- // currentSlot = equippedSlot[i];
- // currentRect = currentSlot.slotRect;
- // if (equipped[i] == null) {
- // GUI.DrawTexture (currentRect, currentSlot.slotIcon); // Slot Rect & Empty Slot Icon
- // } else {
- // GUI.DrawTexture (currentRect, equipped[i].itemIcon); // Slot Rect & Item Icon
- // if (GUI.Button (currentRect, "", GUIStyle ("label"))) {
- // if (Input.GetMouseButtonUp (0)) { // ... if that click is mouse button 0: Remove/Unequip it.
- // bool success = AddItem (equipped[i]); // Remove/Unequip it.
- // if (success) // If it's successfully added to the inventory, remove it here
- // equipped[i] = null;
- // }
- // }
- // }
- // GUI.DrawTexture (new Rect (64, 25, 64, 128), player.icon); // characterIcon
- // }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement