-
Notifications
You must be signed in to change notification settings - Fork 1
/
UpgradeScreenPlusPlus.cs
407 lines (335 loc) · 15 KB
/
UpgradeScreenPlusPlus.cs
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
using System;
using System.Linq;
using BTD_Mod_Helper;
using BTD_Mod_Helper.Api;
using BTD_Mod_Helper.Api.Components;
using BTD_Mod_Helper.Api.Enums;
using BTD_Mod_Helper.Extensions;
using HarmonyLib;
using Il2CppAssets.Scripts.Models.Towers;
using Il2CppAssets.Scripts.Models.Towers.Behaviors.Abilities;
using Il2CppAssets.Scripts.Unity.Menu;
using Il2CppAssets.Scripts.Unity.UI_New.InGame;
using Il2CppAssets.Scripts.Unity.UI_New.Upgrade;
using Il2CppInterop.Runtime.Attributes;
using Il2CppSystem.Collections.Generic;
using MelonLoader;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace PathsPlusPlus;
[RegisterTypeInIl2Cpp(false)]
internal class UpgradeScreenPlusPlus : MonoBehaviour
{
private const int TopBarHeight = 345;
private const int LeftSideWidth16X9 = 1240;
private const int LeftSideWidth16X10 = 1010;
private const int LeftSideWidth4X3 = 849;
private const int PathsSpacing = 525;
private const int UpgradeSpacing = 490;
public UpgradeScreen upgradeScreen = null!;
public ModHelperScrollPanel scrollPanel = null!;
public RectTransform upgradePaths = null!;
public RectTransform scrollContent = null!;
public readonly List<GameObject> createdUpgradePaths;
public readonly List<UpgradeDetails>[] extraUpgradeDetails;
private float paragonOffset;
public UpgradeScreenPlusPlus(IntPtr ptr) : base(ptr)
{
createdUpgradePaths = new List<GameObject>();
extraUpgradeDetails =
[
new List<UpgradeDetails>(),
new List<UpgradeDetails>(),
new List<UpgradeDetails>()
];
}
public void UpdateUi(string towerId)
{
scrollPanel.ScrollRect.normalizedPosition = new Vector2(0, 1);
foreach (var createdUpgradePath in createdUpgradePaths)
{
createdUpgradePath.gameObject.SetActive(false);
}
foreach (var upgradeDetails in extraUpgradeDetails.SelectMany(list => list.ToArray()))
{
upgradeDetails.gameObject.SetActive(false);
}
var extraPaths = PathsPlusPlusMod.PathsByTower.TryGetValue(towerId, out var somePaths)
? somePaths.Where(path => path is {ShowInMenu: true}).ToList()
: [];
var extendedPaths = PathsPlusPlusMod.ExtendedPathsByTower.TryGetValue(towerId, out var morePaths)
? morePaths.Where(path => path is {ShowInMenu: true}).ToList()!
: [];
var allPaths = extraPaths.Concat(extendedPaths).ToArray();
var paragonPanel = upgradeScreen.paragonPanel.transform;
if (allPaths.Length == 0)
{
scrollPanel.ScrollRect.enabled = false;
paragonPanel.localPosition = Vector3.zero;
paragonOffset = 0;
return;
}
var maxUpgrades = allPaths.Max(path => path!.UpgradeCount);
var minForScrolling = upgradeScreen.ShowParagonPanel() ? 6 : 7;
scrollPanel.ScrollRect.enabled = true;
scrollPanel.ScrollRect.horizontal = maxUpgrades >= minForScrolling;
scrollPanel.ScrollRect.vertical = extraPaths.Any();
var before = upgradePaths.localPosition;
var deltaY = 0f;
var deltaX = 0f;
for (var path = 0; path < extraPaths.Count; path++)
{
var pathPlusPlus = extraPaths[path];
deltaY += PathsSpacing;
deltaX = Math.Max(deltaX, (pathPlusPlus.UpgradeCount - 5) * UpgradeSpacing);
GameObject upgradePath;
if (path >= createdUpgradePaths.Count)
{
upgradePath = Instantiate(upgradeScreen.path3Container, upgradePaths);
upgradePath.name = $"Path {pathPlusPlus.Path + 1} Upgrades";
upgradePath.transform.TranslateScaled(new Vector3(0, -deltaY, 0));
for (var i = 0; i < upgradePath.transform.childCount; i++)
{
var animator = upgradePath.transform.GetChild(i).gameObject.AddComponent<Animator>();
animator.runtimeAnimatorController = Animations.PopupAnim;
animator.updateMode = AnimatorUpdateMode.UnscaledTime;
}
createdUpgradePaths.Add(upgradePath);
}
else
{
upgradePath = createdUpgradePaths.Get(path);
}
upgradePath.SetActive(true);
var upgradeDetails = upgradePath.GetComponentsInChildren<UpgradeDetails>();
for (var i = 0; i < pathPlusPlus.Upgrades.Count; i++)
{
try
{
if (i >= upgradeDetails.Length)
{
var newUpgradeDetails = Instantiate(upgradeDetails[i - 1], upgradePath.transform);
newUpgradeDetails.gameObject.name = "Upgrade(Clone)";
upgradeDetails = upgradePath.GetComponentsInChildren<UpgradeDetails>();
}
var upgrade = upgradeDetails[i];
var upgradePlusPlus = pathPlusPlus.Upgrades[i];
if (upgradePlusPlus == null) continue;
SetUpgrade(upgrade, upgradePlusPlus);
}
catch (Exception e)
{
ModHelper.Warning<PathsPlusPlusMod>(e);
}
}
for (var i = pathPlusPlus.Upgrades.Count; i < upgradeDetails.Length; i++)
{
upgradeDetails[i].gameObject.SetActive(false);
}
upgradeScreen.ResetUpgradeUnlocks(
upgradeDetails.Take(pathPlusPlus.Upgrades.Count).ToIl2CppReferenceArray(), null);
foreach (var upgradeDetail in upgradeDetails)
{
upgradeDetail.SetUpgradeScreen(upgradeScreen);
}
}
foreach (var extendedPath in extendedPaths)
{
deltaX = Math.Max(deltaX, (extendedPath!.UpgradeCount - 5) * UpgradeSpacing);
var multiple = ModContent.GetContent<PathPlusPlus>().Any(path =>
path != extendedPath && path.Tower == extendedPath.Tower && path.Path == extendedPath.Path);
var extraUpgrades = extraUpgradeDetails[extendedPath.ExtendVanillaPath];
var upgrades = extendedPath.ExtendVanillaPath switch
{
PathPlusPlus.Top => upgradeScreen.path1Upgrades,
PathPlusPlus.Middle => upgradeScreen.path3Upgrades,
PathPlusPlus.Bottom => upgradeScreen.path3Upgrades,
_ => throw new IndexOutOfRangeException()
};
var container = extendedPath.ExtendVanillaPath switch
{
PathPlusPlus.Top => upgradeScreen.path1Container,
PathPlusPlus.Middle => upgradeScreen.path2Container,
PathPlusPlus.Bottom => upgradeScreen.path3Container,
_ => throw new IndexOutOfRangeException()
};
foreach (var upgradePlusPlus in extendedPath.Upgrades.Values)
{
try
{
var index = upgradePlusPlus.Tier - 6;
if (index >= extraUpgrades.Count)
{
var newUpgradeDetails = Instantiate(upgrades.Last(), container.transform);
newUpgradeDetails.gameObject.name = "Upgrade(Clone)";
extraUpgrades.Add(newUpgradeDetails);
}
var upgrade = extraUpgrades.Get(index);
SetUpgrade(upgrade, upgradePlusPlus);
var cycle = upgrade.transform.GetComponentsInChildren<Button>(true)
.FirstOrDefault(button => button.name == "Cycle");
if (multiple && InGame.instance == null)
{
if (cycle == null)
{
var button = ModHelperButton.Create(new Info("Cycle", 100, 100, new Vector2(0, 0.5f)),
VanillaSprites.RetryIcon, null);
button.SetParent(upgrade.transform);
cycle = button.Button;
}
cycle.gameObject.SetActive(true);
cycle.SetOnClick(() => CycleExtendedPath(extendedPath, upgrade));
multiple = false;
}
else if (cycle != null)
{
cycle.gameObject.SetActive(false);
}
}
catch (Exception e)
{
ModHelper.Warning<PathsPlusPlusMod>(e);
}
}
upgradeScreen.ResetUpgradeUnlocks(
extraUpgrades.Where(details => details.gameObject.active).ToIl2CppReferenceArray(), null);
foreach (var upgradeDetails in extraUpgrades)
{
upgradeDetails.SetUpgradeScreen(upgradeScreen);
}
}
paragonOffset = deltaX;
scrollContent.sizeDelta = new Vector2(deltaX, deltaY);
upgradePaths.localPosition = before;
}
private static void SetUpgrade(UpgradeDetails upgrade, UpgradePlusPlus upgradePlusPlus)
{
upgrade.gameObject.SetActive(true);
upgrade.SetUpgrade(upgrade.baseTowerID, upgradePlusPlus.GetUpgradeModel(),
new List<AbilityModel>().Cast<ICollection<AbilityModel>>(), upgradePlusPlus.Path.Path,
upgradePlusPlus.PortraitReference ?? ModContent.CreateSpriteReference(
VanillaSprites.ByName.TryGetValue(upgrade.baseTowerID.Replace(TowerType.WizardMonkey, "Wizard") + "000",
out var guid)
? guid
: ""));
upgrade.abilityObject.SetActive(upgradePlusPlus.Ability);
var theme = upgradePlusPlus.Tier >= 5 ? upgrade.tier5Theme : upgrade.standardTheme;
upgrade.theme = theme.MemberwiseClone().Cast<UpgradeDetails.UpgradeDetailsTheme>();
if (upgradePlusPlus.ContainerReference is not null)
{
upgrade.theme.owned = upgrade.theme.paragonPurchased = upgradePlusPlus.ContainerReference;
}
}
public static UpgradeScreenPlusPlus Setup(UpgradeScreen upgradeScreen)
{
var upgradeScreenPlusPlus = upgradeScreen.gameObject.AddComponent<UpgradeScreenPlusPlus>();
upgradeScreenPlusPlus.upgradeScreen = upgradeScreen;
var scrollPanel = upgradeScreenPlusPlus.scrollPanel =
upgradeScreen.gameObject.AddModHelperScrollPanel(new Info("UpgradesScroll", InfoPreset.FillParent), null);
scrollPanel.GetComponent<Mask>().showMaskGraphic = false;
scrollPanel.AddComponent<Image>();
scrollPanel.ScrollRect.horizontal = false;
scrollPanel.ScrollContent.AddComponent<Text>();
var scrollContent = upgradeScreenPlusPlus.scrollContent =
scrollPanel.ScrollContent.transform.Cast<RectTransform>();
scrollContent.anchorMin = Vector2.zero;
scrollContent.anchorMax = Vector2.one;
var upgradePaths = upgradeScreenPlusPlus.upgradePaths = upgradeScreen
.GetComponentFromChildrenByName<LayoutElement>("UpgradePaths").transform
.Cast<RectTransform>();
upgradePaths.SetParent(scrollPanel.ScrollContent.transform);
upgradePaths.sizeDelta = Vector2.zero;
scrollContent.pivot = new Vector2(0, 1);
var rect = upgradeScreen.transform.Cast<RectTransform>().rect;
var aspectRatio = rect.width / rect.height;
var leftSideWidth = aspectRatio switch
{
< 1.5f => LeftSideWidth4X3,
< 1.7f => LeftSideWidth16X10,
_ => LeftSideWidth16X9
};
var before = upgradePaths.localPosition;
scrollPanel.RectTransform.pivot = new Vector2(1, 0);
scrollPanel.RectTransform.sizeDelta = new Vector2(-leftSideWidth, -TopBarHeight);
upgradePaths.localPosition = before + new Vector3(-leftSideWidth, TopBarHeight, 0);
scrollPanel.transform.SetSiblingIndex(4);
return upgradeScreenPlusPlus;
}
private void LateUpdate()
{
var paragonPanel = upgradeScreen.paragonPanel.transform.Cast<RectTransform>();
paragonPanel.anchoredPosition = paragonPanel.anchoredPosition with
{
x = scrollContent.anchoredPosition.x + paragonOffset
};
}
[HideFromIl2Cpp]
private void CycleExtendedPath(PathPlusPlus extendedPath, UpgradeDetails upgradeDetails)
{
MenuManager.instance.buttonClick3Sound.Play();
var allPaths = ModContent.GetContent<PathPlusPlus>()
.Where(path => path.Tower == extendedPath.Tower && path.Path == extendedPath.Path)
.ToList();
var currentIndex = allPaths.IndexOf(extendedPath);
var nextPath = allPaths[(currentIndex + 1) % allPaths.Count];
foreach (var path in allPaths)
{
path.Override.Value = path == nextPath;
}
nextPath.AssignPath();
PathsPlusPlusMod.Preferences.SaveToFile(false);
var before = scrollPanel.ScrollContent.transform.localPosition;
UpdateUi(extendedPath.Tower);
scrollPanel.ScrollContent.transform.localPosition = before;
upgradeDetails.OnPointerExit(null);
upgradeDetails.OnPointerEnter(null);
}
[HarmonyPatch(typeof(UpgradeScreen), nameof(UpgradeScreen.UpdateUi))]
internal static class UpgradeScreen_UpdateUi
{
[HarmonyPostfix]
private static void Postfix(UpgradeScreen __instance)
{
var upgradeScreenPlusPlus = __instance.GetComponentInChildren<UpgradeScreenPlusPlus>();
if (upgradeScreenPlusPlus == null)
{
upgradeScreenPlusPlus = Setup(__instance);
}
upgradeScreenPlusPlus.UpdateUi(__instance.currTowerId);
}
}
[HarmonyPatch(typeof(UpgradeScreen), nameof(UpgradeScreen.Open))]
internal static class UpgradeScreen_Open
{
[HarmonyPostfix]
private static void Postfix(UpgradeScreen __instance)
{
foreach (var upgradeDetails in __instance.GetComponentsInChildren<UpgradeDetails>())
{
if (upgradeDetails.gameObject.HasComponent(out Animator animator))
{
animator.speed = .75f;
animator.Play("PopupScaleIn");
}
upgradeDetails.GetComponentInChildren<EventTrigger>(true).enabled = false;
}
}
}
[HarmonyPatch(typeof(UpgradeScreen), nameof(UpgradeScreen.Close))]
internal static class UpgradeScreen_Close
{
[HarmonyPostfix]
private static void Postfix(UpgradeScreen __instance)
{
foreach (var upgradeDetails in __instance.GetComponentsInChildren<UpgradeDetails>())
{
if (upgradeDetails.gameObject.HasComponent(out Animator animator))
{
animator.speed = 1f;
animator.Play("PopupScaleOut");
}
}
}
}
}