Ejemplo n.º 1
0
    /// <summary>
    /// Load inventory data.
    /// </summary>
    public void Load()
    {
        if (PlayerPrefs.HasKey("DaD Inventory") == true)         // If any stored data exist
        {
            DadCell[]   dadCells   = GetComponentsInChildren <DadCell>(true);
            QuickCell[] quickCells = GetComponentsInChildren <QuickCell>(true);

            if (PlayerPrefs.GetInt("Cells count") != dadCells.Length && PlayerPrefs.GetInt("Quick count") != quickCells.Length)             // If cells amount were changed
            {
                DeleteSavedData();
            }
            else             // If cells amount the same as in stored data
            {
                if (priceGroup != null)
                {
                    int cash = PlayerPrefs.GetInt("Cash amount");
                    if (cash < 0)
                    {
                        cash = 0;
                    }
                    priceGroup.SetCash(cash);                     // Set player's cash from stored data
                }

                ClearInventory();                 // Clear all current items from inventory

                // Add items from prefabs by it's stored names
                for (int i = 0; i < dadCells.Length; ++i)
                {
                    string itemName = PlayerPrefs.GetString("Cell_" + i + " item");
                    if (itemName != null && itemName != "" && itemName != "null")
                    {
                        int stack = PlayerPrefs.GetInt("Cell_" + i + " stack");
                        if (stack > 0)
                        {
                            GameObject itemPrefab = Resources.Load <GameObject>("Items/" + itemName);
                            if (itemPrefab != null)
                            {
                                GameObject item = Instantiate(itemPrefab);
                                if (item != null)
                                {
                                    item.name = itemName;
                                    dadCells[i].AddItem(item);
                                    StackItem stackItem = item.GetComponent <StackItem>();
                                    if (stackItem != null)
                                    {
                                        stackItem.SetStack(stack);                                         // Set item's stack
                                    }
                                }
                            }
                        }
                        else
                        {
                            GameObject skillPrefab = Resources.Load <GameObject>("Skills/" + itemName);
                            if (skillPrefab != null)
                            {
                                GameObject skill = Instantiate(skillPrefab);
                                if (skill != null)
                                {
                                    skill.name = itemName;
                                    dadCells[i].AddItem(skill);
                                }
                            }
                        }
                    }
                }

                for (int i = 0; i < quickCells.Length; ++i)
                {
                    int link = PlayerPrefs.GetInt("Quick_" + i + " link");
                    if (link >= 0)
                    {
                        quickCells[i].CreateQuickItem(dadCells[link].GetItem());
                    }
                }

                Debug.Log("Inventory loaded");
            }
        }
    }