-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUpgradeMenu.cs
More file actions
412 lines (318 loc) · 16.9 KB
/
UpgradeMenu.cs
File metadata and controls
412 lines (318 loc) · 16.9 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
using System;
using System.Collections;
using System.Collections.Generic;
using BTD_Mod_Helper.Api;
using BTD_Mod_Helper.Api.Components;
using BTD_Mod_Helper.Api.Helpers;
using Il2CppAssets.Scripts.Simulation.Towers;
using Il2CppAssets.Scripts.Unity.UI_New;
using Il2CppAssets.Scripts.Unity.UI_New.InGame;
using Il2CppAssets.Scripts.Unity.UI_New.Popups;
using Il2CppAssets.Scripts.Unity.UI_New.Settings;
using Il2CppTMPro;
using UnityEngine;
using UnityEngine.Events;
using Object = Il2CppSystem.Object;
namespace BloonsClicker;
public class UpgradeMenu : ModGameMenu<HotkeysScreen>
{
private static ModHelperScrollPanel MainPanel { get; set; } = null!;
private static InfoPanel InfoPanel { get; set; } = null!;
private CanvasGroup CanvasGroup { get; set; } = null!;
private Animator? Animator { get; set; }
private static CursorUpgrade SelectedUpgrade { get; set; } = null!;
public const int UnPurchased = int.MinValue;
public static Dictionary<Path, int> PurchasedUpgrades { get; set; } = Main.Paths.ToDictionary(path => path, _ => UnPurchased);
public static Dictionary<CursorUpgrade, CursorUpgradeButton> UpgradeButtons { get; } = new();
private const int MainWidth = 3600;
private const int TierSpacing = 1000;
public override void OnMenuUpdate()
{
if (Closing)
{
CanvasGroup.alpha -= .07f;
}
else if ((Input.GetKeyDown(KeyCode.Comma) || Input.GetKeyDown(KeyCode.Period) || Input.GetKeyDown(KeyCode.Slash)) && InfoPanel.BuyButton != null)
{
InfoPanel.BuyButton.Button.onClick.Invoke();
}
}
public override void OnMenuClosed()
{
if(Animator != null)
Animator.Play("PopupSlideOut");
UpgradeButtons.Clear();
}
public override bool OnMenuOpened(Object data)
{
var gameObject = GameMenu.gameObject;
gameObject.DestroyAllChildren();
GameMenu.saved = true;
var basePanel = gameObject.AddModHelperPanel(new Info("BasePanel", InfoPreset.FillParent));
MainPanel = basePanel.AddScrollPanel(new Info("UpgradesPanel", MainWidth, 1700, new Vector2(.5f, .6f)),
RectTransform.Axis.Vertical, VanillaSprites.BlueInsertPanel, 75, 150);
MainPanel.Mask.showMaskGraphic = false;
Animator = basePanel.AddComponent<Animator>();
Animator.updateMode = AnimatorUpdateMode.UnscaledTime;
Animator.runtimeAnimatorController = Animations.PopupAnim;
Animator.speed = .75f;
Animator.Rebind();
CanvasGroup = basePanel.AddComponent<CanvasGroup>();
var infoPanel = basePanel.AddPanel(new Info("InfoPanel", 2600, 600, new Vector2(.5f, .175f)),
VanillaSprites.MainBgPanel, RectTransform.Axis.Horizontal, 75, 150);
InfoPanel = infoPanel.AddComponent<InfoPanel>();
InfoPanel.panel = infoPanel;
CreateInfoPanel();
MelonCoroutines.Start(CreateUpgrades());
CommonForegroundScreen.instance.Hide();
CommonForegroundScreen.instance.Show(true, "Clicker Upgrades", false, false, false, false, false, false, false);
foreach (var animator in basePanel.GetComponentsInChildren<Animator>())
animator.updateMode = AnimatorUpdateMode.UnscaledTime;
return false;
}
private static void CreateInfoPanel()
{
var nameIconPanel = InfoPanel.panel.AddPanel(new Info("NameIconPanel", 500), VanillaSprites.MainBgPanel,
RectTransform.Axis.Vertical, 50, 50);
nameIconPanel.Background.color = new Color(0.3f, 0.05f, 0f, 0.5f);
InfoPanel.NameText = nameIconPanel.AddText(new Info("NameText", 500, 50), "");
InfoPanel.IconImage = nameIconPanel.AddImage(new Info("IconImage", 300), "");
var descriptionPanel =
InfoPanel.panel.AddPanel(new Info("DescriptionPanel", 1000, 350), VanillaSprites.MainBgPanel, null);
descriptionPanel.Background.color = new Color(0.3f, 0.05f, 0f, 0.5f);
InfoPanel.DescriptionText = descriptionPanel.AddText(new Info("DescriptionText", 900, 350), "");
var buyPanel = InfoPanel.panel.AddPanel(new Info("BuyPanel")
{
Width = 500,
Height = 550,
}, VanillaSprites.MainBgPanel, RectTransform.Axis.Vertical, 35, 30);
buyPanel.Background.color = new Color(0.3f, 0.05f, 0f, 0.5f);
var popsPanel = buyPanel.AddPanel(new Info("PopsPanel")
{
Width = 500,
Height = 25,
}, null);
popsPanel.AddImage(new Info("PopsIcon", -150, 0, 50, 50), VanillaSprites.PopIcon);
InfoPanel.PopsText = popsPanel.AddText(new Info("PopsText", 125, 0, 450, 50), $"{(CursorUpgrade.CursorTower?.damageDealt ?? Main.SavedCursorPops):N0}");
InfoPanel.PopsText.Text.alignment = TextAlignmentOptions.MidlineLeft;
InfoPanel.BuyButton = buyPanel.AddButton(new Info("BuyButton")
{
FlexWidth = 1,
Height = 200,
}, VanillaSprites.GreenBtnLong, new Action(
() =>
{
var cost = CostHelper.CostForDifficulty(SelectedUpgrade.Cost, InGame.instance.GetGameModel());
if (!CanBuy(SelectedUpgrade))
{
if (!IsAvailable(SelectedUpgrade))
{
/*PopupScreen.instance.ShowPopup(PopupScreen.Placement.menuCenter, "Not Available",
"This upgrade is not available for purchase. You either have already purchased this upgrade, or you need to purchase the previous upgrade(s) in this path.", null,
"Ok", null, null, Popup.TransitionAnim.Scale);*/
// i got annoyed with it lmao
return;
}
if (InGame.instance.GetCash() < cost)
{
PopupScreen.instance.ShowPopup(PopupScreen.Placement.menuCenter, "Not Enough Cash",
$"You need ${cost:n0} to purchase this upgrade.", null, "Ok", null, null,
Popup.TransitionAnim.Scale);
return;
}
PopupScreen.instance.ShowPopup(PopupScreen.Placement.menuCenter, "Cannot Purchase",
"It appears something went wrong, try reloading your save or restarting your game. Contact GrahamKracker if this happens repeatedly.", null, "Ok", null, null,
Popup.TransitionAnim.Scale);
return;
}
InGame.instance.AddCash(-cost);
InfoPanel.CashText.SetText($"{InGame.instance.GetCash():n0}");
PurchasedUpgrades[SelectedUpgrade.Path] = SelectedUpgrade.Tier;
Main.CurrentUpgrades.Add(SelectedUpgrade);
SelectedUpgrade.OnBuy();
foreach (var button in UpgradeButtons.Values)
{
button.UpdateLockState();
}
var sortedUpgrades = GetContent<CursorUpgrade>().Where(x => x.Path == SelectedUpgrade.Path)
.OrderBy(x => x.Tier).ToArray();
var paragonUpgrade = GetContent<CursorUpgrade>().First(x => x.Path == Path.Paragon);
var index = Array.IndexOf(sortedUpgrades, SelectedUpgrade) + 1;
if (index >= sortedUpgrades.Length)
{
UpdateSelectionPanel(IsAvailable(paragonUpgrade) ? paragonUpgrade : sortedUpgrades[^1]);
}
else
{
UpdateSelectionPanel(sortedUpgrades[index]);
}
CursorUpgrade.UpdateTower();
}));
InfoPanel.PurchaseText = InfoPanel.BuyButton.AddText(new Info("PurchaseText", 500, 250, new Vector2(.5f, .65f)), "Purchase", 70);
InfoPanel.CostText = InfoPanel.BuyButton.AddText(new Info("CostText", 500, 50, new Vector2(.5f, .35f)), "");
var cashPanel = buyPanel.AddPanel(new Info("CashPanel")
{
Width = 500,
Height = 25,
}, null);
cashPanel.AddImage(new Info("CashIcon", -150, 0, 50, 50), VanillaSprites.CoinIcon);
InfoPanel.CashText = cashPanel.AddText(new Info("CashText", 125, 0, 450, 50),
$"{InGame.instance.GetCash():n0}");
InfoPanel.CashText.Text.alignment = TextAlignmentOptions.MidlineLeft;
InfoPanel.SellButton = buyPanel.AddButton(new Info("SellButton")
{
FlexWidth = 1,
Height = 150,
}, VanillaSprites.RedBtnLong, new Action(
() =>
{
PopupScreen.instance.SafelyQueue(screen =>
{
float totalCost = PurchasedUpgrades.Aggregate<KeyValuePair<Path, int>, float>(0, (current, keyValuePair) =>
{
if (keyValuePair.Value < 0)
return current;
return current + CostHelper.CostForDifficulty(
CursorUpgrade.Cache[keyValuePair.Key][keyValuePair.Value].Cost,
InGame.instance.GetGameModel());
});
screen.ShowPopup(PopupScreen.Placement.menuCenter, "Sell Cursor Upgrades",
$"Would you like to sell ALL your cursor upgrades? You will get ${totalCost * .75f} out of your ${totalCost} back.",
new Action(() =>
{
InGame.instance.AddCash(totalCost * .75f);
InfoPanel.CashText.SetText($"{InGame.instance.GetCash():n0}");
foreach (var upgrade in Main.CurrentUpgrades.OrderBy(x => x.Tier))
{
upgrade.OnSell();
}
PurchasedUpgrades = Main.Paths.ToDictionary(path => path, _ => UnPurchased);
Main.CurrentUpgrades.Clear();
foreach (var button in UpgradeButtons.Values)
{
button.UpdateLockState();
}
Main.SavedCursorPops = CursorUpgrade.CursorTower?.damageDealt ?? Main.SavedCursorPops;
if (CursorUpgrade.CursorTower != null && !CursorUpgrade.CursorTower.IsDestroyed)
CursorUpgrade.CursorTower.SellTower();
CursorUpgrade.CursorTower = null;
UpdateSelectionPanel(GetContent<CursorUpgrade>().First(x => x.Path == Path.Clicker));
}), "Yes", null, "No", Popup.TransitionAnim.Scale);
});
}));
InfoPanel.SellText = InfoPanel.SellButton.AddText(new Info("SellText", 500, 150), "Sell", 70);
}
private static void CreateClickerButton()
{
var clickerPanel = MainPanel.AddPanel(new Info("Clicker Panel", 0, 0,
MainWidth, 450, new Vector2(.5f, .5f)), VanillaSprites.BlueInsertPanel);
clickerPanel.Background.enabled = false;
MainPanel.AddScrollContent(clickerPanel);
CursorUpgradeButton.Create(GetContent<CursorUpgrade>().First(x => x.Path == Path.Clicker), clickerPanel).ModHelperButton.Button.onClick.AddListener(new Action(() =>
{
TaskScheduler.ScheduleTask(() =>
{
CursorUpgrade.CursorTower!.damageDealt = Main.SavedCursorPops;
}, () => CursorUpgrade.CursorTower is {IsDestroyed:false});
}));
}
private static void CreateParagonButton()
{
var paragonPanel = MainPanel.AddPanel(new Info("Paragon Panel", 0, 0,
MainWidth, 450, new Vector2(.5f, .5f)), VanillaSprites.BlueInsertPanel);
paragonPanel.Background.enabled = false;
MainPanel.AddScrollContent(paragonPanel);
var paragonUpgrade = GetContent<CursorUpgrade>().Find(x => x.Path == Path.Paragon);
if (paragonUpgrade is null)
{
return;
}
var upgradeButton = CursorUpgradeButton.Create(paragonUpgrade,
paragonPanel);
upgradeButton.ModHelperButton.Image.SetSprite(VanillaSprites.UpgradeContainerParagon);
}
private static IEnumerator CreateUpgrades()
{
var maxTier = GetContent<CursorUpgrade>().Max(x => x.Tier);
UpgradeButtons.Clear();
CreateClickerButton();
SelectedUpgrade = GetContent<CursorUpgrade>().First(x => x.Path == Path.Clicker);
UpdateSelectionPanel(SelectedUpgrade);
for (int tier = 1; tier <= maxTier; tier++)
{
var tierPanel = MainPanel.AddPanel(new Info($"Tier{tier} Panel", 0, 0,
MainWidth, 450, new Vector2(.5f, .5f)), VanillaSprites.BlueInsertPanel);
tierPanel.Background.enabled = false;
MainPanel.AddScrollContent(tierPanel);
yield return null;
foreach (var upgrade in GetContent<CursorUpgrade>().Where(x => x.Tier == tier).OrderBy(x => x.Path))
{
var button = CursorUpgradeButton.Create(upgrade, tierPanel);
var paths = Main.Paths.Where(x => (int)x > 0).ToArray();
paths = paths.OrderBy(x => x).ToArray();
var upgradePath = (int)(upgrade.Path - 1);
float x;
if (paths.Length % 2 == 0)
{
x = (upgradePath - (int)(paths.Length / 2f) + .5f) * TierSpacing;
}
else
{
x = (upgradePath - (int)(paths.Length / 2f)) * TierSpacing;
}
var transform = button.ModHelperButton.transform;
var position = transform.localPosition;
transform.localPosition = position with { x = x };
yield return null;
}
}
CreateParagonButton();
}
public static void UpdateSelectionPanel(CursorUpgrade upgrade)
{
InfoPanel.NameText.SetText(upgrade.DisplayName);
InfoPanel.IconImage.Image.SetSprite(upgrade.IconReference);
InfoPanel.DescriptionText.SetText(upgrade.Description);
InfoPanel.CostText.SetText(
$"${CostHelper.CostForDifficulty(upgrade.Cost, InGame.instance.GetGameModel()):n0}");
SelectedUpgrade = upgrade;
var selectedButton = UpgradeButtons[upgrade];
foreach (var button in UpgradeButtons.Values)
{
button.transform.FindChild("SelectedGlow").gameObject.SetActive(false);
}
selectedButton.transform.FindChild("SelectedGlow").gameObject.SetActive(true);
var canBuy = CanBuy(upgrade);
InfoPanel.BuyButton.Button.interactable = canBuy;
InfoPanel.BuyButton.Image.color = canBuy ? InfoPanel.BuyButton.Button.colors.normalColor : InfoPanel.BuyButton.Button.colors.disabledColor;
InfoPanel.PurchaseText.Text.color = canBuy
? InfoPanel.BuyButton.Button.colors.normalColor
: InfoPanel.BuyButton.Button.colors.disabledColor;
InfoPanel.CostText.Text.color = canBuy
? InfoPanel.BuyButton.Button.colors.normalColor
: InfoPanel.BuyButton.Button.colors.disabledColor;
var canSell = PurchasedUpgrades.Any(x => x.Value >= 0);
InfoPanel.SellButton.Button.interactable = canSell;
InfoPanel.SellButton.Image.color = canSell ? InfoPanel.SellButton.Button.colors.normalColor : InfoPanel.SellButton.Button.colors.disabledColor;
InfoPanel.SellText.Text.color = canSell
? InfoPanel.SellButton.Button.colors.normalColor
: InfoPanel.SellButton.Button.colors.disabledColor;
}
private static bool CanBuy(CursorUpgrade upgrade) =>
IsAvailable(upgrade) && InGame.instance.GetCash() >=
CostHelper.CostForDifficulty(upgrade.Cost, InGame.instance.GetGameModel());
public static bool IsAvailable(CursorUpgrade upgrade)
{
if (upgrade.Path == Path.Clicker && PurchasedUpgrades[Path.Clicker] == UnPurchased)
return true;
if(PurchasedUpgrades[Path.Clicker] == UnPurchased)
return false;
if (upgrade.Path == Path.Paragon && PurchasedUpgrades[Path.Paragon] != UnPurchased)
return false;
if (upgrade.Path == Path.Paragon && PurchasedUpgrades[Path.Paragon] == UnPurchased)
{
return Main.Paths.Where(path => path != Path.Paragon).All(path => PurchasedUpgrades[path] >= CursorUpgrade.Cache[path].Values.Max(x => x.Tier));
}
return (PurchasedUpgrades[upgrade.Path] + 1 == upgrade.Tier && PurchasedUpgrades[Path.Clicker] != UnPurchased) || (PurchasedUpgrades[upgrade.Path] == UnPurchased && upgrade.Tier == 1);
}
}