private int currentFurnitureIndex = 0; // The current index of the furniture we're looking at on the panel private void Start() { player = GameObject.FindGameObjectWithTag("Player").GetComponent <PlayerController> (); for (int i = 0; i < furnitures.Length; i++) { furnitures [i].SetActive(GameManager.gameData.unlockedFurniture [i]); } // Temp reference to avoid calling GetComponent a dozen times FurnitureProperties currentProduct = furnitures [currentFurnitureIndex].GetComponent <FurnitureProperties> (); imgPurchaseIcon.sprite = currentProduct.purchaseIcon; txtFurnitureName.text = currentProduct.displayName; txtPrice.text = "x " + currentProduct.price; // If the player doesn't have enough money or if the product is unlocked already, disable the purchase button if (GameManager.gameData.unlockedFurniture [currentFurnitureIndex]) { btnPurchase.GetComponentInChildren <Text> ().text = LocalizationManager.localizedDictionary ["purchased"]; // The text is changed to 'Purchased' btnPurchase.interactable = false; } else if (GameManager.gameData.coinCount < currentProduct.price) { btnPurchase.GetComponentInChildren <Text> ().text = LocalizationManager.localizedDictionary ["purchase"]; // The text is changed to 'Purchased' btnPurchase.interactable = false; } else { btnPurchase.GetComponentInChildren <Text> ().text = LocalizationManager.localizedDictionary ["purchase"]; // The text is changed to 'Purchased' btnPurchase.interactable = true; } if (btnPurchase.IsInteractable()) { myEventSystem.SetSelectedGameObject(btnPurchase.gameObject); } else { myEventSystem.SetSelectedGameObject(btnBack.gameObject); } }
// This is called when the player presses the left or right buttons on the furniture menu public void ShiftSelection(string direction) { switch (direction) { case "left": if (currentFurnitureIndex - 1 < 0) // If 1 less the index makes an Out of Bounds exception, loop to the top of the array { currentFurnitureIndex = 15; } else { currentFurnitureIndex -= 1; } break; case "right": if (currentFurnitureIndex + 1 > 15) // If 1 plus the index makes an Out of Bounds exception, loop to the bottom of the array { currentFurnitureIndex = 0; } else { currentFurnitureIndex += 1; } break; default: break; } // Temp reference to avoid calling GetComponent a dozen times currentProduct = furnitures [currentFurnitureIndex].GetComponent <FurnitureProperties> (); imgPurchaseIcon.sprite = currentProduct.purchaseIcon; txtFurnitureName.text = currentProduct.displayName; txtPrice.text = "x " + currentProduct.price; // If the player doesn't have enough money or if the product is unlocked already, disable the purchase button if (GameManager.gameData.unlockedFurniture [currentFurnitureIndex]) { btnPurchase.GetComponentInChildren <Text> ().text = LocalizationManager.localizedDictionary ["purchased"]; // The text is changed to 'Purchased' btnPurchase.interactable = false; } else if (player.numberOfCoins < currentProduct.price) { btnPurchase.GetComponentInChildren <Text> ().text = LocalizationManager.localizedDictionary ["purchase"]; btnPurchase.interactable = false; } else { btnPurchase.GetComponentInChildren <Text> ().text = LocalizationManager.localizedDictionary ["purchase"]; btnPurchase.interactable = true; } if (btnPurchase.IsInteractable()) { myEventSystem.SetSelectedGameObject(btnPurchase.gameObject); } else { myEventSystem.SetSelectedGameObject(btnBack.gameObject); } }