-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrencySystem.cs
More file actions
172 lines (155 loc) · 5.16 KB
/
CurrencySystem.cs
File metadata and controls
172 lines (155 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using UnityEngine;
// Define currencies
public enum CurrencyType
{
CurrencyA,
CurrencyB,
CurrencyC,
}
public class CurrencySystem : MonoBehaviour
{
public static CurrencySystem instance;
// Dictionary to store currency amounts
private Dictionary<CurrencyType, int> currencyAmounts = new Dictionary<CurrencyType, int>();
// Events to notify listeners when currency changes
public event Action<CurrencyType, int> OnCurrencyChanged;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
InitializeCurrencyAmounts();
LoadCurrency();
GiveInitialCurrency();
UpdateUI();
}
private void InitializeCurrencyAmounts()
{
foreach (CurrencyType currencyType in Enum.GetValues(typeof(CurrencyType)))
{
if (!currencyAmounts.ContainsKey(currencyType))
{
currencyAmounts.Add(currencyType, 0);
}
}
}
private void GiveInitialCurrency()
{
if (PlayerPrefs.GetInt("initial_reward", 0) == 0)
{
SetCurrencyAmount(CurrencyType.CurrencyA, 10);
SetCurrencyAmount(CurrencyType.CurrencyB, 10);
SetCurrencyAmount(CurrencyType.CurrencyC, 5);
PlayerPrefs.SetInt("initial_reward", 1);
SaveCurrency();
}
}
private void UpdateUI()
{
foreach (var currency in currencyAmounts)
{
OnCurrencyChanged?.Invoke(currency.Key, currency.Value);
}
}
// Method to charge currency with callbacks
public void ChargeCurrency(CurrencyType currencyType, int amountToCharge, Action enoughCurrencyCallback, Action notEnoughCurrencyCallback)
{
if (currencyAmounts[currencyType] >= amountToCharge)
{
enoughCurrencyCallback?.Invoke();
currencyAmounts[currencyType] -= amountToCharge;
OnCurrencyChanged?.Invoke(currencyType, currencyAmounts[currencyType]);
UpdateUI();
SaveCurrency();
}
else
{
notEnoughCurrencyCallback?.Invoke();
}
}
// Method to charge currency with item name and callbacks
public bool ChargeCurrency(CurrencyType currencyType, int amountToCharge, string itemName, Action<string> enoughCurrencyCallback, Action notEnoughCurrencyCallback)
{
if (currencyAmounts[currencyType] >= amountToCharge)
{
enoughCurrencyCallback?.Invoke(itemName);
currencyAmounts[currencyType] -= amountToCharge;
OnCurrencyChanged?.Invoke(currencyType, currencyAmounts[currencyType]);
UpdateUI();
SaveCurrency();
return true; // Transaction successful
}
else
{
notEnoughCurrencyCallback?.Invoke();
return false; // Transaction failed due to insufficient funds
}
}
// Method to reward currency
public void RewardCurrency(CurrencyType currencyType, int amount)
{
currencyAmounts[currencyType] += amount;
OnCurrencyChanged?.Invoke(currencyType, currencyAmounts[currencyType]);
UpdateUI();
SaveCurrency();
}
// Method to reward currency with callback
public void RewardCurrency(CurrencyType currencyType, int amount, Action increaseCallback)
{
increaseCallback?.Invoke();
currencyAmounts[currencyType] += amount;
OnCurrencyChanged?.Invoke(currencyType, currencyAmounts[currencyType]);
UpdateUI();
SaveCurrency();
}
// Method to set currency amount
public void SetCurrencyAmount(CurrencyType currencyType, int amount)
{
currencyAmounts[currencyType] = amount;
OnCurrencyChanged?.Invoke(currencyType, currencyAmounts[currencyType]);
UpdateUI();
SaveCurrency();
}
// Method to get currency amount
public int GetCurrencyAmount(CurrencyType currencyType)
{
if (currencyAmounts.ContainsKey(currencyType))
return currencyAmounts[currencyType];
return 0; // Default to 0 if currency type not found
}
// Save currency amounts to PlayerPrefs
private void SaveCurrency()
{
foreach (var currency in currencyAmounts)
{
PlayerPrefs.SetInt(currency.Key.ToString(), currency.Value);
}
PlayerPrefs.Save();
}
// Load currency amounts from PlayerPrefs
private void LoadCurrency()
{
foreach (CurrencyType currencyType in Enum.GetValues(typeof(CurrencyType)))
{
if (PlayerPrefs.HasKey(currencyType.ToString()))
{
currencyAmounts[currencyType] = PlayerPrefs.GetInt(currencyType.ToString());
}
else
{
currencyAmounts[currencyType] = 0;
}
}
}
}