using UnityEngine; using System.Linq; using System.Collections; public class Shop : MonoBehaviour { private bool playerInZone = false; //Make sure the player is next to the shop before they can use it [SerializeField] GameObject shopPanel; //Create a gameobject for the shop GUI private bool shopActive = false; [SerializeField] GameObject promptText; private PlayerInventory playerInventory; //Get player inventory private PlayerUpgrade playerUpgrade; //Reference player upgrade script [SerializeField] GameObject notEnoughResources; private Coroutine warningCoroutine; //Create a pause to show a warning message private PlayerHealth playerHealth; // Reference to the PlayerHealth script IEnumerator ShowNotEnoughMessage() //This method runs when player tries to buy something they can't afford { notEnoughResources.SetActive(true); //Show warning yield return new WaitForSeconds(2f); //Leave warning on screen for 2s notEnoughResources.SetActive(false); //Hide warning } void Start() { shopPanel.SetActive(false); //Shop is hidden by default promptText.SetActive(false); //Prompt text is hidden by default playerInventory = GameObject.FindWithTag("Player").GetComponent(); //Access the player inventory playerUpgrade = GameObject.FindWithTag("Player").GetComponent(); //Access the player upgrade script notEnoughResources.SetActive(false); playerHealth = GameObject.FindWithTag("Player").GetComponent(); //Access the player health } void OnTriggerEnter2D() { playerInZone = true; promptText.SetActive(true); //Player is prompted to open the shop when in range } void OnTriggerExit2D() { playerInZone = false; promptText.SetActive(false); } // Update is called once per frame void Update() { if (playerInZone == true && Input.GetKeyDown(KeyCode.E)) //Shop logic { if (!shopActive) //Open shop { shopPanel.SetActive(true); shopActive = true; Debug.Log("active"); } else //Close shop { shopPanel.SetActive(false); shopActive = false; Debug.Log("not active"); } } if (!playerInZone) { shopPanel.SetActive(false); shopActive = false; } } public void BuyUpgrade() { int goldCount = playerInventory.inventory.Count(item => item == "gold"); //Logic to check how much gold the player has if (goldCount >= 3) //Remove 3 gold from the player { int removed = 0; while (removed < 3 && playerInventory.inventory.Contains("gold")) { playerInventory.inventory.Remove("gold"); removed++; } playerUpgrade.Upgrade(); } else { // If the warning is already showing, stop and restart it if (warningCoroutine != null) { StopCoroutine(warningCoroutine); } warningCoroutine = StartCoroutine(ShowNotEnoughMessage()); } } public void BuyHealth() { int goldCount = playerInventory.inventory.Count(item => item == "meat"); //Logic to check how much meat the player has if (goldCount >= 1) //Check if player has enough gold { playerInventory.inventory.Remove("meat"); //take away 1 meat playerHealth.Heal(1); //Increase player health by 1 } else { // If the warning is already showing, stop and restart it if (warningCoroutine != null) { StopCoroutine(warningCoroutine); } warningCoroutine = StartCoroutine(ShowNotEnoughMessage()); } } public void BuyMeat() { int goldCount = playerInventory.inventory.Count(item => item == "gold"); //Logic to check how much gold the player has if (goldCount >= 1) //Check if player has enough gold { playerInventory.inventory.Remove("gold"); //take away 1 gold playerInventory.inventory.Add("meat"); // Add 1 meat } else { // If the warning is already showing, stop and restart it if (warningCoroutine != null) { StopCoroutine(warningCoroutine); } warningCoroutine = StartCoroutine(ShowNotEnoughMessage()); } } }