Exemplo 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();
                    }
                }
            }
        }
Exemplo n.º 2
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;
                    }
                }
            }
        }