Esempio n. 1
0
        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();
                    }
                }
            }
        }
Esempio n. 2
0
        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;
                }
            }
        }
Esempio n. 3
0
        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;
                    }
                }
            }
        }