using UnityEngine; using UnityEngine.UI; /// /// The Game Controller manages the general play of the game. It handles tracking /// and management of the player's pool of resources and the automatic increase of /// the pool over time. /// /// Attach this class to an empty game object in your Hierarchy panel. /// /// This idle game is a clone of the Cookie Clicker application at http://orteil.dashnet.org/cookieclicker/ /// public class GameController : MonoBehaviour { #region Properties /// /// How much is in the player's pool /// private float _pool; public float Pool { get { return _pool; } set { _pool = value; poolText.text = PoolTextPrefix + _pool.ToString("0.00"); } } /// /// How much the pool is automatically incremented per second /// private float _increasePerSecond; public float IncreasePerSecond { get { return _increasePerSecond; } set { _increasePerSecond = value; rateText.text = "per second: " + _increasePerSecond.ToString("0.0"); } } /// /// The pool text prefix. /// public string PoolTextPrefix; /// /// Represents the click power of each button click. Each button click /// will add this much to the player's pool. /// [Tooltip("How many reasources the player will make when they hit the button.")] public float powerPerClick = 1; // In the Hierarchy panel, select the empty game object that holds this controller, // drag and drop each game object onto these properties in the inspector panel. [Header("Object References")] public Text poolText; public Text rateText; #endregion #region Methods /**** * Button click event handler for the main game button. Select the game button in * the hierarchy. In the inspector find the On Click() section and click the + button * drag the game controller from the hierarchy onto the object reference in the click * section. Then select GameController.ClickedButton from the method drop down. *****/ public void ClickedButton() { // Each button click will increment the pool by the click power value. Pool += powerPerClick; } #endregion #region MonoBehavior // Use this for initialization void Start() { Pool = 0; IncreasePerSecond = 0; } // Update is called once per frame void Update() { // Each second the game is running, increment the pool by the Increase per second // value. Increase per second is increased by purchasing upgrades from the store. Pool += IncreasePerSecond * Time.deltaTime; } #endregion }