/// <summary> /// Returns the amount of an item in the inventory. /// </summary> public int TotalAmountOfItemInInventory(int itemID) { // New Counter. int counter = 0; // Loop through all the item slots. for (int i = 0; i < items.Count; i++) { // IF this item slot contains the same item id as itemID. if (items[i].ID == itemID) { // Get the Item_Data component. Item_Data data = slots [i].GetComponentInChildren <Item_Data> (); // IF there isnt a Item_Data component. if (data == null) { // Go to the next iteration. continue; } // Add to our counter. counter += data.amount; } } // Return what we have tallied up. return(counter); }
private void Load() { // Grab the information on what is in the inventory. string inventoryJson = PlayerPrefs.GetString("Inventory"); // IF there is nothing in this string. if (String.IsNullOrEmpty(inventoryJson)) { // Create the slots on a default level. CreateSlots(); // GTFO of here we done son! return; } // Turn the json data to the data to represent Saved_Inventory. Saved_Inventory data = JsonUtility.FromJson <Saved_Inventory> (inventoryJson); // Set our extra inventory slots before we create our inventory. extraInventorySlots = data.extraInventorySlots; // Create the default slots. CreateSlots(); // Loop through however many inventory slots we have. for (int i = 0; i < defaultSlotAmount + extraInventorySlots; i++) { // Fetch the item based on the saved ID for this slot. Item fetchedItem = Grid.itemDataBase.FetchItemByID(data.slotID [i]); // IF we have an actual item. if (fetchedItem != null) { // Assign the item to the slot. items [i] = fetchedItem; // Create the item. GameObject itemObj = Instantiate(inventoryItem); // Get the Item_Data component. Item_Data iData = itemObj.GetComponentInChildren <Item_Data> (); // Set the Item_Data item and slot number. iData.item = fetchedItem; iData.slotNumber = i; // Set the transform. itemObj.transform.SetParent(slots [i].transform); itemObj.transform.localScale = Vector2.one; itemObj.transform.localPosition = Vector2.zero; // Set the itemObj sprite, sprite color and name of the image. itemObj.GetComponent <Image> ().sprite = fetchedItem.SpriteImage; itemObj.GetComponent <Image> ().color = new Color(fetchedItem.R, fetchedItem.G, fetchedItem.B, fetchedItem.A); itemObj.name = "Item Slot " + i + " - " + fetchedItem.Title; // IF this fetched item is stackable. if (fetchedItem.Stackable) { // Set the Item_Data amount to what we had saved. iData.amount += data.slotAmounts [i]; // Set the text to display how much we have. iData.GetComponentInChildren <Text> ().text = iData.amount.ToString(); } } } }
public void OnDrop(PointerEventData data) { if (data.button == PointerEventData.InputButton.Left) { // Get the Item_Data component from the GameObject that was being dragged and now being dropped via the mouse. Item_Data droppedItem = data.pointerDrag.GetComponent <Item_Data> (); // IF we are dropping an item on an empty inventory slot. // ELSE IF we are dropping an item on a inventory slot that is occupied. if (inv.items [slotNumber].ID == -1) { // Clear the old slot. inv.items [droppedItem.slotNumber] = new Item(); // Set the item to the new slot. inv.items [slotNumber] = droppedItem.item; // Set this slot number to the new slot number. droppedItem.slotNumber = slotNumber; // Rename to the appropriate slot number based on the slot it was moved to. droppedItem.name = "Item Slot " + slotNumber + " - " + inv.items[slotNumber].Title; } else if (droppedItem.slotNumber != slotNumber) { // Get the Transform of the child, which is the Item currently in this slot. Transform item = transform.GetChild(0); // Set the slot number of the item in this current slot to be set to the slot of the dragged item. item.GetComponent <Item_Data> ().slotNumber = droppedItem.slotNumber; // Set the parent of this item to the slot number of the dragged item. item.transform.SetParent(inv.slots [droppedItem.slotNumber].transform); // Set the scale to 1. item.transform.localScale = Vector2.one; // Set the position of the item to where the dragged item was. item.transform.position = inv.slots [droppedItem.slotNumber].transform.position; // Set the dragged items slot number to this current slot number. droppedItem.slotNumber = slotNumber; // Set the parent of the dragged item to the transform of this gameobject. droppedItem.transform.SetParent(transform); // Set the scale to 1. droppedItem.transform.localScale = Vector2.one; // Set the position of the dragged item to the position of this gameobject. droppedItem.transform.position = transform.position; // Swap the items in the inventory array. inv.items [droppedItem.slotNumber] = droppedItem.item; inv.items [item.GetComponent <Item_Data> ().slotNumber] = item.GetComponent <Item_Data> ().item; // Change the text of the Item_Data GameObject. item.name = "Item Slot " + item.GetComponent <Item_Data> ().slotNumber + " - " + item.GetComponent <Item_Data> ().item.Title; droppedItem.name = "Item Slot " + slotNumber + " - " + inv.items[slotNumber].Title; } } }
/// <summary> /// Adds the stackable item in the inventory. /// </summary> public void AddStackableItemInInventory(int _id, int _amount) { // Loop though the items list. for (int i = 0; i < items.Count; i++) { // IF we already have this item in our inventory. if (items [i].ID == _id) { // Get the Item_Data component. Item_Data data = slots [i].GetComponentInChildren <Item_Data> (); // Add the amount we picked up to what we already have. data.amount += _amount; // GTFO. return; } } }
/// <summary> /// Adds the item to inventory. /// </summary> public void AddItemToInventory(Item itemToAdd, int _amount) { // How ever many times we need to add this item. for (int j = 0; j < _amount; j++) { // Loop through all the items. for (int i = 0; i < items.Count; i++) { // IF this item slot is empty. if (items [i].ID == -1) { // Set the item we have been given to this spot in the items list. items [i] = itemToAdd; // Create the GameObject. GameObject itemObj = Instantiate(inventoryItem); // Get the Item_Data Component. Item_Data idComp = itemObj.GetComponent <Item_Data> (); // Set the item in the idComp. idComp.item = itemToAdd; // Set the slot number of this item. idComp.slotNumber = i; // Set the transform of itemObj. itemObj.transform.SetParent(slots [i].transform); itemObj.transform.localScale = Vector2.one; itemObj.transform.localPosition = Vector2.zero; // Adjust the sprite and color of the Image. itemObj.GetComponent <UnityEngine.UI.Image> ().sprite = itemToAdd.SpriteImage; itemObj.GetComponent <UnityEngine.UI.Image> ().color = new Color(itemToAdd.R, itemToAdd.G, itemToAdd.B, itemToAdd.A); // Change the name of the itemObj GameObject to the name of the item. itemObj.name = "Item Slot " + i + " - " + itemToAdd.Title; // IF this item is stackable. if (itemToAdd.Stackable) { // Set the amount for the idComp. idComp.amount = _amount; return; } // If we come here then we have an item that isn't stackable so we just set the amount to 1. idComp.amount = 1; break; } } } }
public void OnDrop(PointerEventData data) { // IF it is a left click AND we we're actually dragging an item AND whatever we are dragging can be in our inventory (Skill icons to be dragged to inventory = NO. Actionbar icons to be dragged to the inventory = NO). if (data.button == PointerEventData.InputButton.Left && Grid_Helper.inventory.GetCurrentlyDraggedItem() != null && Grid_Helper.inventory.GetAllowDragNDrop()) { // Get the Item_Data component from the GameObject that was being dragged and now being dropped via the mouse. Item_Data droppedItem = data.pointerDrag.GetComponent <Item_Data> (); // IF we are dropping an item on an empty inventory slot, // ELSE IF we are dropping an item on a inventory slot that is occupied. if (Grid_Helper.inventory.items [slotNumber].ID == -1) { // Clear the old slot. Grid_Helper.inventory.items [droppedItem.slotNumber] = new Item(); // Set the item to the new slot. Grid_Helper.inventory.items [slotNumber] = droppedItem.item; // Set this slot number to the new slot number. droppedItem.slotNumber = slotNumber; // Rename to the appropriate slot number based on the slot it was moved to. droppedItem.name = "Item Slot " + slotNumber + " - " + Grid_Helper.inventory.items[slotNumber].Title; } } }
/// <summary> /// Removes the inventory from the inventory based on the itemAmountToRemove. /// </summary> private void RemoveInventorySlotItem(int itemID, int itemAmountToRemove) { // Loop the amount of slots we have. for (int i = 0; i < slots.Count; i++) { // IF this item has the same ID as our itemID. if (items [i].ID == itemID) { // Get the Item_Data component. Item_Data data = slots [i].GetComponentInChildren <Item_Data> (); // IF itemAmountToRemove is equal to the amount of the item we have in our inventory, // ELSE IF itemAmountToRemove is greater than the amount of the item we have in our inventory, // ELSE itemAmountToRemove is less than the amount of the item we have in our inventory. if (itemAmountToRemove == data.amount) { // Clear the inventory slot. ClearSlotInInventory(i, data.gameObject); // itemAmountToRemove is 0 so we leave. return; } else if (itemAmountToRemove > data.amount) { // Clear the inventory slot. ClearSlotInInventory(i, data.gameObject); // Deduct the amount. itemAmountToRemove -= data.amount; } else { // Remove the remaining amount. data.amount -= itemAmountToRemove; // itemAmountToRemove is 0 so we leave. return; } } } }
public void SetItemData(Item_Data _itemData) { itemData = _itemData; }
public void AddItem(int id, int amount) { // Get the item based on the id. Item itemToAdd = Grid.itemDataBase.FetchItemByID(id); // IF our items list contains itemToAdd AND this item is stackable. if (items.Contains(itemToAdd) && itemToAdd.Stackable) { // Loop though the items list. for (int i = 0; i < items.Count; i++) { // IF we already have this item in our inventory. if (items [i].ID == id) { // Get the Item_Data component. Item_Data data = slots [i].GetComponentInChildren <Item_Data> (); // Add the amount we picked up to what we already have. data.amount += amount; // Set the text to show our new amount we have. data.GetComponentInChildren <Text> ().text = data.amount.ToString(); // GTFO return; } } } // How ever many times we need to add this item. for (int j = 0; j < amount; j++) { // Loop through all the items. for (int i = 0; i < items.Count; i++) { // IF this item slot is empty. if (items [i].ID == -1) { // Set the item we have been given to this spot in the items list. items [i] = itemToAdd; // Create the GameObject. GameObject itemObj = Instantiate(inventoryItem); // Get the Item_Data Component. Item_Data idComp = itemObj.GetComponent <Item_Data> (); // Set the item in the idComp. idComp.item = itemToAdd; // Set the slot number of this item. idComp.slotNumber = i; // Set the transform of itemObj. itemObj.transform.SetParent(slots [i].transform); itemObj.transform.localScale = Vector2.one; itemObj.transform.localPosition = Vector2.zero; // Adjust the sprite and color of the Image. itemObj.GetComponent <Image> ().sprite = itemToAdd.SpriteImage; itemObj.GetComponent <Image> ().color = new Color(itemToAdd.R, itemToAdd.G, itemToAdd.B, itemToAdd.A); // Change the name of the itemObj GameObject to the name of the item. itemObj.name = "Item Slot " + i + " - " + itemToAdd.Title; // IF this item is stackable. if (itemToAdd.Stackable) { // Set the amount for the idComp. idComp.amount = amount; // Set the text to show our new amount we have. itemObj.GetComponentInChildren <Text> ().text = idComp.amount.ToString(); return; } // If we come here then we have an item that isn't stackable so we just set the amount to 1. idComp.amount = 1; break; } } } }
public void OnDrop(PointerEventData data) { // IF it is a left click AND // IF we we're actually dragging an item AND // IF whatever we are dragging can be in our inventory (Skill icons to be dragged to inventory = NO. // actionbar icons to be dragged to the inventory = NO). if (data.button == PointerEventData.InputButton.Left && Grid_Helper.inventory.GetCurrentlyDraggedItem() != null && Grid_Helper.inventory.GetAllowDragNDrop()) { // Get the Item_Data component from the GameObject that was being dragged and now being dropped via the mouse. Item_Data droppedItem = data.pointerDrag.GetComponent <Item_Data> (); // IF we are dropping this item on the same item in the inventory SO WE STACK THEM, // ELSE we are dropping this item on a different type of item other than itself SO WE SWAP THEM. if (droppedItem.item.ID == item.ID) { // IF the item is stackable, if (item.Stackable) { // Now we want to add the amount we are dragging to our item in this slot. amount += droppedItem.amount; // Clear the old slot from where we originally started dragging. Grid_Helper.inventory.items [droppedItem.slotNumber] = new Item(); // Destroy the Item Data. Destroy(droppedItem.gameObject); } } else { // Placeholder for this Item_Data's slot number. int tempSlotNumber = slotNumber; // Set the slot number of the item in this current slot to be set to the slot of the dragged item. slotNumber = droppedItem.slotNumber; // Set the parent of this item to the slot number of the dragged item. transform.SetParent(Grid_Helper.inventory.slots [droppedItem.slotNumber].transform); // Set the scale to 1. transform.localScale = Vector2.one; // Set the position of the item to where the dragged item was. transform.position = Grid_Helper.inventory.slots [droppedItem.slotNumber].transform.position; // Set the dragged items slot number to our tempSlotNumber. droppedItem.slotNumber = tempSlotNumber; // Set the parent of the dragged item to the transform of this gameobject. droppedItem.transform.SetParent(Grid_Helper.inventory.slots [tempSlotNumber].transform); // Set the scale to 1. droppedItem.transform.localScale = Vector2.one; // Set the position of the dragged item to the position of this gameobject. droppedItem.transform.position = Grid_Helper.inventory.slots [tempSlotNumber].transform.position; // Swap the items in the inventory array. Grid_Helper.inventory.items [droppedItem.slotNumber] = droppedItem.item; Grid_Helper.inventory.items [slotNumber] = item; // Change the text of the Item_Data GameObject. gameObject.name = "Item Slot " + slotNumber + " - " + item.Title; droppedItem.name = "Item Slot " + droppedItem.slotNumber + " - " + Grid_Helper.inventory.items [droppedItem.slotNumber].Title; // Grid_Helper.inventory.PrintInventoryItems (); } } }