using System; using Core.Economy; using Core.Utilities; using UnityEngine; namespace TowerDefense.Economy { /// /// A class for currency gain /// [Serializable] public class CurrencyGainer { /// /// The amount gained with the gain rate /// public int constantCurrencyAddition; /// /// The speed of currency gain in units-per-second /// [Header("The Gain Rate in additions-per-second")] public float constantCurrencyGainRate; /// /// Event for when the currency is changed /// public event Action currencyChanged; /// /// The timer for constant currency gain /// protected RepeatingTimer m_GainTimer; /// /// Gets the currency that this CurrencyGainer modifes /// public Currency currency { get; private set; } /// /// Initializes the currency gainer with new data /// /// /// The currency controller to modify with this currency gainer /// /// /// The currency gained with each addition /// /// /// The rate of gain /// public void Initialize(Currency currencyController, int gainAddition, float gainRate) { constantCurrencyAddition = gainAddition; constantCurrencyGainRate = gainRate; Initialize(currencyController); } /// /// Initializes the currency gainer /// public void Initialize(Currency currencyController) { currency = currencyController; UpdateGainRate(constantCurrencyGainRate); } /// /// For updating the gain timer /// /// /// The change in time to update the timer /// public void Tick(float deltaTime) { if (m_GainTimer == null) { return; } m_GainTimer.Tick(Time.deltaTime); } /// /// Sets the currency gain rate and activates the timer /// /// /// The amount to set the constant gain rate to /// public void UpdateGainRate(float currencyGainRate) { constantCurrencyGainRate = currencyGainRate; if (currencyGainRate < 0) { throw new ArgumentOutOfRangeException("currencyGainRate"); } if (m_GainTimer == null) { m_GainTimer = new RepeatingTimer(1 / constantCurrencyGainRate, ConstantGain); } else { m_GainTimer.SetTime(1 / constantCurrencyGainRate); } } /// /// Increase the currency by m_ConstantCurrencyAddition /// protected void ConstantGain() { int previousCurrency = currency.currentCurrency; currency.AddCurrency(constantCurrencyAddition); int currentCurrency = currency.currentCurrency; var info = new CurrencyChangeInfo(previousCurrency, currentCurrency); if (currencyChanged != null) { currencyChanged(info); } } } }