using System;
namespace Core.Economy
{
///
/// A basic model for in game currency
///
public class Currency
{
///
/// How much currency there currently is
///
public int currentCurrency { get; private set; }
///
/// Occurs when currency changed.
///
public event Action currencyChanged;
///
/// Initializes a new instance of the class.
///
public Currency(int startingCurrency)
{
ChangeCurrency(startingCurrency);
}
///
/// Adds the currency.
///
/// the change in currency
public void AddCurrency(int increment)
{
ChangeCurrency(increment);
}
///
/// Method for trying to purchase, returns false for insufficient funds
///
/// true, if purchase was successful i.e. enough currency false otherwise.
public bool TryPurchase(int cost)
{
// Cannot afford this item
if (!CanAfford(cost))
{
return false;
}
ChangeCurrency(-cost);
return true;
}
///
/// Determines if the specified cost is affordable.
///
/// true if this cost is affordable; otherwise, false.
public bool CanAfford(int cost)
{
return currentCurrency >= cost;
}
///
/// Changes the currency.
///
/// the change in currency
protected void ChangeCurrency(int increment)
{
if (increment != 0)
{
currentCurrency += increment;
if (currencyChanged != null)
{
currencyChanged();
}
}
}
}
}