48 lines
1.0 KiB
C#
48 lines
1.0 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace YachtDice.Economy
|
|
{
|
|
public class CurrencyBank : MonoBehaviour
|
|
{
|
|
[SerializeField] private int startingBalance = 500;
|
|
|
|
private int _balance;
|
|
|
|
public int Balance => _balance;
|
|
|
|
public event Action<int> OnBalanceChanged;
|
|
|
|
private void Awake()
|
|
{
|
|
_balance = startingBalance;
|
|
}
|
|
|
|
public void Add(int amount)
|
|
{
|
|
if (amount <= 0) return;
|
|
|
|
_balance += amount;
|
|
OnBalanceChanged?.Invoke(_balance);
|
|
}
|
|
|
|
public bool Spend(int amount)
|
|
{
|
|
if (amount <= 0) return false;
|
|
if (_balance < amount) return false;
|
|
|
|
_balance -= amount;
|
|
OnBalanceChanged?.Invoke(_balance);
|
|
return true;
|
|
}
|
|
|
|
public bool CanAfford(int amount) => _balance >= amount;
|
|
|
|
public void SetBalance(int value)
|
|
{
|
|
_balance = Mathf.Max(0, value);
|
|
OnBalanceChanged?.Invoke(_balance);
|
|
}
|
|
}
|
|
}
|