diff --git a/S1API/Money/Money.cs b/S1API/Money/Money.cs new file mode 100644 index 00000000..77ab3f3f --- /dev/null +++ b/S1API/Money/Money.cs @@ -0,0 +1,132 @@ +#if IL2CPP +using Il2CppScheduleOne; +using S1ItemFramework = Il2CppScheduleOne.ItemFramework; +using S1MoneyFramework = Il2CppScheduleOne.Money; +#else +using ScheduleOne; +using S1ItemFramework = ScheduleOne.ItemFramework; +using S1MoneyFramework = ScheduleOne.Money; + +#endif + +using S1API.Internal.Utils; +using System; +using UnityEngine; + +namespace S1API.Money +{ + /// + /// Provides static access to financial operations, including methods for managing cash balance, + /// creating online transactions, and calculating net worth. + /// + public static class Money + { + /// + /// Provides internal access to the underlying financial operations manager. + /// This property is utilized for interacting with the core financial functionality. + /// + private static S1MoneyFramework.MoneyManager Internal => S1MoneyFramework.MoneyManager.Instance; + + /// + /// Event triggered whenever there is a change in the balance, + /// including cash balance or online transactions. + /// + public static event Action? OnBalanceChanged; + + /// + /// Adjusts the cash balance by the specified amount. + /// + /// The amount to modify the cash balance by. Positive values increase the balance, and negative values decrease it. + /// Indicates whether the cash change should be visualized on the HUD. + /// Indicates whether a sound effect should be played to signify the cash adjustment. + public static void ChangeCashBalance(float amount, bool visualizeChange = true, bool playCashSound = false) + { + Internal?.ChangeCashBalance(amount, visualizeChange, playCashSound); + OnBalanceChanged?.Invoke(); + } + + /// + /// Creates an online transaction. + /// + /// The name of the transaction. + /// The monetary amount per unit involved in the transaction. + /// The number of units in the transaction. + /// An optional note or description for the transaction. + public static void CreateOnlineTransaction(string transactionName, float unitAmount, float quantity, + string transactionNote) + { + Internal?.CreateOnlineTransaction(transactionName, unitAmount, quantity, transactionNote); + OnBalanceChanged?.Invoke(); + } + + /// + /// Retrieves the total net worth, including all cash and online balances combined. + /// + /// The total net worth as a floating-point value. + public static float GetNetWorth() + { + return Internal != null ? Internal.GetNetWorth() : 0f; + } + + /// + /// Retrieves the current cash balance. + /// + /// The current cash balance as a floating-point value. + public static float GetCashBalance() + { + return Internal != null ? Internal.cashBalance : 0f; + } + + /// + /// Retrieves the current online balance. + /// + /// The current amount of online balance. + public static float GetOnlineBalance() + { + return Internal != null ? Internal.sync___get_value_onlineBalance() : 0f; + } + + /// + /// Registers a callback to be invoked during net worth calculation. + /// + /// The callback to be executed when net worth is calculated. It receives an object as its parameter. + public static void AddNetworthCalculation(System.Action callback) + { + if (Internal != null) + Internal.onNetworthCalculation += callback; + } + + /// + /// Removes a previously registered networth calculation callback. + /// + /// The callback to be removed from the networth calculation updates. + public static void RemoveNetworthCalculation(System.Action callback) + { + if (Internal != null) + Internal.onNetworthCalculation -= callback; + } + + /// + /// Creates a new cash instance with the specified balance. + /// + /// The initial amount of cash to set in the instance. + /// A newly created instance of cash with the specified balance. + public static CashInstance CreateCashInstance(float amount) + { +#if IL2CPP + var cashItem = Registry.GetItem("cash"); + var instance = CrossType.As(cashItem.GetDefaultInstance(1)); + var cashInstance = new CashInstance(instance); + cashInstance.SetQuantity(amount); + return cashInstance; +#else + var cashItem = Registry.GetItem("cash"); + var instance = CrossType.As(cashItem.GetDefaultInstance(1)); + var cashInstance = new CashInstance(instance); + cashInstance.SetQuantity(amount); + return cashInstance; + +#endif + } + } +}