UnityGameProjectsCode / RelianceGame / Inventory Control / ItemPool.cs
ItemPool.cs
Raw
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ItemPool : MonoBehaviour //this script will hold all different objects that are available to be purchased or picked up in missions.
{
    public List<GameObject> armoryObjects = new List<GameObject>();
    public List<GameObject> junkObjects = new List<GameObject>();
    public List<GameObject> weaponsObjects = new List<GameObject>();
    public List<GameObject> specialObjects = new List<GameObject>();
    public GameObject itemPrefab;
    private GameObject armoryPanel;
    private GameObject junkPanel;
    private GameObject weaponsPanel;
    private GameObject specialPanel;

    public List<int> armoryItemIDs = new List<int>();
    public List<int> junkItemIDs = new List<int>();
    public List<int> weaponItemIDs = new List<int>();
    public List<int> specialItemIDs = new List<int>();

    private SaveFileManager saveMgr;


    private void Awake()
    {
        armoryPanel = GameObject.Find("HUBUI/Inventory Panel/Shop Panels/Armory Shop");
        junkPanel = GameObject.Find("HUBUI/Inventory Panel/Shop Panels/Junk Shop");
        weaponsPanel = GameObject.Find("HUBUI/Inventory Panel/Shop Panels/Weapons Shop");
        specialPanel = GameObject.Find("HUBUI/Inventory Panel/Shop Panels/Specialty Shop");
        saveMgr = GameObject.Find("Persistent Object").GetComponent<SaveFileManager>();
    }

    void Start()
    {
        if (LoadShopInfo() == false)
        {
            Debug.Log("No shop data was loaded, the shop will generate new items");
            GenerateShopLists();
        }
        else
        {
            Debug.Log("Shop save data was found, generating items based on data");
            GenerateShopListsWithSaveData();
        }

    }

    public void GenerateShopLists()
    {
        for (int y = 0; y < 4; y++) //for each shop
        {
            int itemNumber = Random.Range(0, 3);
            GameObject targetPanel = null;
            List<GameObject> targetList = new List<GameObject>();
            List<int> targetItemList = new List<int>();

            //use the y value to determine what shop is being setup for the current loop
            if (y == 0)
            {
                targetPanel = armoryPanel;
                targetList = armoryObjects;
                targetItemList = armoryItemIDs;
            }
            else if (y == 1)
            {
                targetPanel = junkPanel;
                targetList = junkObjects;
                targetItemList = junkItemIDs;
            }
            else if (y == 2)
            {
                targetPanel = weaponsPanel;
                targetList = weaponsObjects;
                targetItemList = weaponItemIDs;
            }
            else if (y == 3)
            {
                targetPanel = specialPanel;
                targetList = specialObjects;
                targetItemList = specialItemIDs;
            }

            List<GameObject> tempItemObjects = new List<GameObject>();
            List<Item> tempItems = new List<Item>();

            if (itemNumber == 0)
                Debug.Log("item number was 0 for the " + targetPanel.name + " so no items were spawned");

            for (int i = 0; i < itemNumber; i++) //use a random number to decide how many items should appear
            {
                int randItem = Random.Range(0, targetList.Count); //use a random variable to decide what item is being spawned from the list

                var currentPanel = Instantiate(itemPrefab as GameObject, targetPanel.transform, false); //use the target panel variable to give the transform for the current shop
                currentPanel.GetComponent<ItemBuy>().attachedItem = Instantiate(targetList[randItem] as GameObject, targetPanel.transform, false);

                targetItemList.Add(currentPanel.GetComponent<ItemBuy>().attachedItem.GetComponent<Item>().itemStats.ID);

                foreach (Transform tempChild in targetPanel.GetComponentsInChildren<Transform>()) //get the children objects under the panel
                {
                    if (tempChild.name.Contains("Panel"))
                    {
                        tempItemObjects.Add(tempChild.gameObject);
                        tempItemObjects = tempItemObjects.Distinct().ToList();
                    }


                    if (tempChild.GetComponent<Item>())
                    {
                        tempItems.Add(tempChild.GetComponent<Item>());
                        tempItems = tempItems.Distinct().ToList();
                    }

                }

                string tempName = "";

                foreach (Transform childVar in tempItemObjects[i].GetComponentsInChildren<Transform>()) //for each child of the current panel prefab, set each text field
                {

                    if (childVar.name == "Item Name Text") // set the name of the prefab to the itemname field of the item
                    {
                        childVar.GetComponent<TextMeshProUGUI>().text = targetList[randItem].GetComponent<Item>().itemStats.displayName;

                        tempName = childVar.GetComponent<TextMeshProUGUI>().text;

                        if (childVar.GetComponent<TextMeshProUGUI>().text.Contains("Super"))
                        {
                            childVar.GetComponent<TextMeshProUGUI>().color = Color.yellow;
                        }

                        if (childVar.GetComponent<TextMeshProUGUI>().text.Contains("Altered"))
                        {
                            childVar.GetComponent<TextMeshProUGUI>().color = new Color(0, 229, 202, 255);
                        }
                    }

                    if (childVar.name == "Item Image")
                    {
                        childVar.GetComponent<Image>().sprite = targetList[randItem].GetComponent<Item>().itemStats.icon;
                    }

                    if (childVar.name == "Item Cost") //set the cost of the prefab using the random number
                    {
                        childVar.GetComponent<TextMeshProUGUI>().text = targetList[randItem].GetComponent<Item>().itemStats.buyPrice.ToString();
                    }

                    if (childVar.name == "Item Currency Type")
                    {
                        if (tempName.Contains("Altered"))
                        {
                            childVar.GetComponent<TextMeshProUGUI>().text = "Datum";
                        }
                        else
                        {
                            childVar.GetComponent<TextMeshProUGUI>().text = "Credits";
                        }
                    }
                }
            }
        }

        saveMgr.SendDataToSaveSystem();
    }

    public void GenerateShopListsWithSaveData()
    {
        for (int y = 0; y < 4; y++) //for each shop
        {
            GameObject targetPanel = null;
            List<GameObject> targetList = new List<GameObject>();
            List<int> targetItemIDs = new List<int>();

            //use the y value to determine what shop is being setup for the current loop
            if (y == 0)
            {
                targetPanel = armoryPanel;
                targetList = armoryObjects;
                targetItemIDs = armoryItemIDs;
            }
            else if (y == 1)
            {
                targetPanel = junkPanel;
                targetList = junkObjects;
                targetItemIDs = junkItemIDs;
            }
            else if (y == 2)
            {
                targetPanel = weaponsPanel;
                targetList = weaponsObjects;
                targetItemIDs = weaponItemIDs;
            }
            else if (y == 3)
            {
                targetPanel = specialPanel;
                targetList = specialObjects;
                targetItemIDs = specialItemIDs;
            }

            List<GameObject> tempItemObjects = new List<GameObject>();
            List<Item> tempItems = new List<Item>();
            GameObject targetObject = null;

            for (int i = 0; i < targetItemIDs.Count; i++) //for each item id
            {
                int currentItem = targetItemIDs[i];

                var currentPanel = Instantiate(itemPrefab as GameObject, targetPanel.transform, false); //use the target panel variable to give the transform for the current shop

                for (int x = 0; x < targetList.Count; x++)
                {
                    if (targetList[x].GetComponent<Item>().itemStats.ID == currentItem)
                    {
                        targetObject = targetList[x];
                        currentPanel.GetComponent<ItemBuy>().attachedItem = Instantiate(targetObject as GameObject, targetPanel.transform, false);
                        break;
                    }
                }

                foreach (Transform tempChild in targetPanel.GetComponentsInChildren<Transform>()) //get the children objects under the panel
                {
                    if (tempChild.name.Contains("Panel"))
                    {
                        tempItemObjects.Add(tempChild.gameObject);
                        tempItemObjects = tempItemObjects.Distinct().ToList();
                    }


                    if (tempChild.GetComponent<Item>())
                    {
                        tempItems.Add(tempChild.GetComponent<Item>());
                        tempItems = tempItems.Distinct().ToList();
                    }

                }

                string tempName = "";

                foreach (Transform childVar in tempItemObjects[i].GetComponentsInChildren<Transform>()) //for each child of the current panel prefab, set each text field
                {

                    if (childVar.name == "Item Name Text") // set the name of the prefab to the itemname field of the item
                    {
                        childVar.GetComponent<TextMeshProUGUI>().text = targetObject.GetComponent<Item>().itemStats.displayName;

                        tempName = childVar.GetComponent<TextMeshProUGUI>().text;

                        if (childVar.GetComponent<TextMeshProUGUI>().text.Contains("Super"))
                        {
                            childVar.GetComponent<TextMeshProUGUI>().color = Color.yellow;
                        }

                        if (childVar.GetComponent<TextMeshProUGUI>().text.Contains("Altered"))
                        {
                            childVar.GetComponent<TextMeshProUGUI>().color = new Color(0, 229, 202, 255);
                        }
                    }

                    if (childVar.name == "Item Image")
                    {
                        childVar.GetComponent<Image>().sprite = targetObject.GetComponent<Item>().itemStats.icon;
                    }

                    if (childVar.name == "Item Cost") //set the cost of the prefab using the random number
                    {
                        childVar.GetComponent<TextMeshProUGUI>().text = targetObject.GetComponent<Item>().itemStats.buyPrice.ToString();
                    }

                    if (childVar.name == "Item Currency Type")
                    {
                        if (tempName.Contains("Altered"))
                        {
                            childVar.GetComponent<TextMeshProUGUI>().text = "Datum";
                        }
                        else
                        {
                            childVar.GetComponent<TextMeshProUGUI>().text = "Credits";
                        }
                    }
                }
            }
        }

        saveMgr.SendDataToSaveSystem();
    }

    public bool LoadShopInfo()
    {
        int index = saveMgr.saveFileIndex;
        SaveGameData saveInfo = saveMgr.GetSaveData(index);

        Debug.Log("Loaded ID numbers: " + saveInfo.armoryItemIDs.Count + " " + saveInfo.junkItemIDs.Count + " " + saveInfo.weaponItemIDs.Count + " " + saveInfo.specialItemIDs.Count);

        if (saveInfo.armoryItemIDs.Count == 0 && saveInfo.junkItemIDs.Count == 0 && saveInfo.weaponItemIDs.Count == 0 && saveInfo.specialItemIDs.Count == 0)
        {
            return false;
        }
        else
        {
            armoryItemIDs = saveInfo.armoryItemIDs;
            junkItemIDs = saveInfo.junkItemIDs;
            weaponItemIDs = saveInfo.weaponItemIDs;
            specialItemIDs = saveInfo.specialItemIDs;

            return true;
        }
    }

    public List<int> GetArmoryInfo()
    {
        return armoryItemIDs;
    }

    public List<int> GetJunkInfo()
    {
        return junkItemIDs;
    }

    public List<int> GetWeaponInfo()
    {
        return weaponItemIDs;
    }

    public List<int> GetSpecialInfo()
    {
        return specialItemIDs;
    }

    public void RemoveItemID(int ID, string listName)
    {
        int index = saveMgr.saveFileIndex;
        SaveGameData saveInfo = saveMgr.GetSaveData(index);


        if (listName == "Armory Shop")
        {
            armoryItemIDs.Remove(ID);
            saveInfo.armoryItemIDs.Remove(ID);
        }
        else if (listName == "Junk Shop")
        {
            junkItemIDs.Remove(ID);
            saveInfo.junkItemIDs.Remove(ID);
        }
        else if (listName == "Weapons Shop")
        {
            weaponItemIDs.Remove(ID);
            saveInfo.weaponItemIDs.Remove(ID);
        }
        else if (listName == "Specialty Shop")
        {
            specialItemIDs.Remove(ID);
            saveInfo.specialItemIDs.Remove(ID);
        }
    }
}