/// <summary> /// Happens when we enter a store/robot detection collider and while we stay in it. /// Handles recieving UI info and interacting with both stores and robots. Potentially refactor to make easier to read. /// </summary> /// <param name="other"></param> private void OnTriggerStay(Collider other) { IInteractable subject = other.gameObject.GetComponent <IInteractable>(); if (subject != null) { // Get info from robot or store string infoString = subject.Detect(); // Prepare info to displayed on UI string[] info = infoString.Split(' '); if (info[0] == "0") { UIText.text = "Head"; } else if (info[0] == "1") { UIText.text = "Arm"; } else if (info[0] == "2") { UIText.text = "Leg"; } else { UIText.text = "Cog"; } UIText.text += " $" + info[1] + "\nPress E to "; if (other.tag == "store") { UIText.text += "buy"; } else { UIText.text += "repair"; } // Prepare info to be used later int num = Convert.ToInt32(info[1]); int index = Convert.ToInt32(info[0]); //Display/enable UI text UIText.enabled = true; if (Input.GetKeyUp(KeyCode.E)) { // Is this a store or a robot if (other.gameObject.tag == "store") { //Debug.Log(num.ToString() + " " + creditTracker.getCreditBalance().ToString()); // Do we have enough inventory space if (itemCount > 9) { badText.text = "Not enough space to carry!"; } else { // Do we have enough money if (creditTracker.GetCreditBalance() >= num) { // Remove credits and add parts to inventory subject.Interact(); creditTracker.SpendCredits(num); audioSource.Play(); inventory[index]++; itemCount++; RefreshUI(); } else { badText.text = "You need more money!"; badText.enabled = true; } } } else { // Do we have enough on the appropriate part if (inventory[index] > 0) { // Were there no errors on the robot side of things if (subject.Interact()) { // Remove parts from inventory and add credits creditTracker.AddCredits(num); audioSource.Play(); inventory[index]--; itemCount--; RefreshUI(); // Blank UI text since robot will be changing states shortly UIText.text = ""; } } else { badText.text = "You need more parts!"; badText.enabled = true; } } } } }
// Start is called before the first frame update void Awake() { scoretext = FindObjectOfType <Text>(); tracker = FindObjectOfType <CreditTracker>(); scoretext.text = "Credits: " + tracker.GetCreditBalance().ToString("D6"); }