You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.9 KiB
102 lines
2.9 KiB
2 years ago
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
|
||
|
// The item can do one of two things, increase the power of the
|
||
|
// player's click on the main button, or increase the amount of
|
||
|
// resources generated each second during the game loop.
|
||
|
public enum ItemType
|
||
|
{
|
||
|
ClickPower,
|
||
|
PerSecondIncrease
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// This class is attached to each store button in the store menu. It represents the
|
||
|
/// behavior of each upgrade that the player may purchase in the game.
|
||
|
/// </summary>
|
||
|
public class StoreItem : MonoBehaviour {
|
||
|
|
||
|
#region Properties
|
||
|
// What does this upgrade affect? Power per second or power per click
|
||
|
public ItemType itemType;
|
||
|
|
||
|
[Tooltip("How much will this upgrade cost")]
|
||
|
public int cost;
|
||
|
|
||
|
[Tooltip("If purchased, how much will it increase this")]
|
||
|
public float increaseAmount;
|
||
|
|
||
|
// In the Hierarchy panel, select the button for this upgrade,
|
||
|
// drag and drop each game object onto these properties in the inspector panel.
|
||
|
[Header("Object References")]
|
||
|
public Text costText;
|
||
|
public Text qtyText;
|
||
|
#endregion
|
||
|
|
||
|
#region Local Properties
|
||
|
// Total number of this item purchased by the player.
|
||
|
private int qty;
|
||
|
// A reference to the game controller script
|
||
|
private GameController controller;
|
||
|
// A reference to the button that this script is attached to
|
||
|
private Button button;
|
||
|
#endregion
|
||
|
|
||
|
#region Methods
|
||
|
/*****
|
||
|
* This is a button event handler for the button that this script is attached to.
|
||
|
* There is no need to wire this handler to the button in the Inspector panel,
|
||
|
* rather this method is assigned in the Start method using AddListener().
|
||
|
* The purpose of this method is to apply the upgrade values to the properties
|
||
|
* of the Game Controller, and to update the properties(statistics) of this script.
|
||
|
*****/
|
||
|
public void ButtonClicked()
|
||
|
{
|
||
|
// Remove the cost of this upgrade from the player's pool
|
||
|
controller.Pool -= cost;
|
||
|
// Increment the Game Controller value based on the type of this upgrade.
|
||
|
switch (itemType)
|
||
|
{
|
||
|
case ItemType.ClickPower:
|
||
|
controller.powerPerClick += increaseAmount;
|
||
|
break;
|
||
|
case ItemType.PerSecondIncrease:
|
||
|
controller.IncreasePerSecond += increaseAmount;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
// increase the number of times this upgrade was purchased
|
||
|
qty++;
|
||
|
qtyText.text = qty.ToString();
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
#region MonoBehavior
|
||
|
// Use this for initialization
|
||
|
void Start()
|
||
|
{
|
||
|
// Set the initial properties of this upgrade
|
||
|
qty = 0;
|
||
|
qtyText.text = qty.ToString();
|
||
|
costText.text = cost.ToString();
|
||
|
|
||
|
// Get a reference to the button that this script is attached to
|
||
|
// and assign and event listener
|
||
|
button = transform.GetComponent<Button>();
|
||
|
button.onClick.AddListener(this.ButtonClicked);
|
||
|
|
||
|
// Get a reference to the Game Controller
|
||
|
controller = GameObject.FindObjectOfType<GameController>();
|
||
|
}
|
||
|
|
||
|
// Update is called once per frame
|
||
|
void Update()
|
||
|
{
|
||
|
// Make the button disabled until the player has enough money to pay
|
||
|
// for the upgrade.
|
||
|
button.interactable = (controller.Pool >= cost);
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
}
|