-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.cs
More file actions
348 lines (285 loc) · 11.9 KB
/
Main.cs
File metadata and controls
348 lines (285 loc) · 11.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using BloonsClicker;
using BTD_Mod_Helper.Api;
using BTD_Mod_Helper.Api.Components;
using BTD_Mod_Helper.Api.Helpers;
using Il2CppAssets.Scripts.Models.GenericBehaviors;
using Il2CppAssets.Scripts.Models.Profile;
using Il2CppAssets.Scripts.Simulation.Bloons;
using Il2CppAssets.Scripts.Simulation.Input;
using Il2CppAssets.Scripts.Simulation.Towers;
using Il2CppAssets.Scripts.Simulation.Towers.Projectiles;
using Il2CppAssets.Scripts.Simulation.Track;
using Il2CppAssets.Scripts.Unity.Display;
using Il2CppAssets.Scripts.Unity.UI_New.InGame;
using Il2CppAssets.Scripts.Unity.UI_New.InGame.BloonMenu;
using Il2CppAssets.Scripts.Unity.UI_New.InGame.RightMenu;
using Newtonsoft.Json;
using UnityEngine;
using Action = System.Action;
using Main = BloonsClicker.Main;
using Vector2 = Il2CppAssets.Scripts.Simulation.SMath.Vector2;
// ReSharper disable InconsistentNaming
[assembly: MelonInfo(typeof(Main), ModHelperData.Name, ModHelperData.Version, ModHelperData.RepoOwner)]
[assembly: MelonGame("Ninja Kiwi", "BloonsTD6")]
namespace BloonsClicker;
[HarmonyPatch]
public class Main : BloonsTD6Mod
{
public static IEnumerable<Path> Paths { get; } = Enum.GetValues(typeof(Path)).Cast<Path>();
public static SortedSet<CursorUpgrade> CurrentUpgrades { get; } = new(Comparer<CursorUpgrade>.Create((a, b) => a.Tier == b.Tier ? a.Path.CompareTo(b.Path) : a.Tier.CompareTo(b.Tier)));
public static readonly HashSet<int> ProjectileHitBloon = [];
public static float TimeSinceLastAttack { get; set; } = float.MaxValue;
public static float TimeMouseHeld { get; private set; }
private static void ResetCursor()
{
UpgradeMenu.PurchasedUpgrades = Paths.ToDictionary(path => path, _ => UpgradeMenu.UnPurchased);
CurrentUpgrades.Clear();
SavedCursorPops = 0;
TimeSinceLastAttack = float.MaxValue;
TimeMouseHeld = 0;
}
public override void OnMainMenu()
{
ResetCursor();
}
public override void OnRestart()
{
ResetCursor();
}
#if DEBUG
/// <inheritdoc />
public override void OnMatchStart()
{
ModGameMenu.Open<UpgradeMenu>(); //todo: remove this
}
#endif
internal static readonly Dictionary<Projectile, LifeSpan> ProjectileAge = new();
internal static readonly HashSet<string> ProjectileNameCache = [];
public class LifeSpan(float destroyAfter) : IComparable<LifeSpan>
{
public float Time { get; set; }
public readonly float DestroyAfter = destroyAfter;
public static LifeSpan NormalClick => new(.25f);
public static LifeSpan StickyClicks => new(2);
public static LifeSpan PermaClicks => new(15);
public static bool operator >(LifeSpan a, LifeSpan b)
{
return a.DestroyAfter > b.DestroyAfter;
}
public static bool operator <(LifeSpan a, LifeSpan b)
{
return a.DestroyAfter < b.DestroyAfter;
}
/// <inheritdoc />
public int CompareTo(LifeSpan? other)
{
if (ReferenceEquals(this, other)) return 0;
if (ReferenceEquals(null, other)) return 1;
int destroyAfterComparison = DestroyAfter.CompareTo(other.DestroyAfter);
if (destroyAfterComparison != 0) return destroyAfterComparison;
return Time.CompareTo(other.Time);
}
public static object operator >=(LifeSpan a, LifeSpan b)
{
return a.DestroyAfter >= b.DestroyAfter;
}
public static object operator <=(LifeSpan a, LifeSpan b)
{
return a.DestroyAfter <= b.DestroyAfter;
}
public static object operator ==(LifeSpan a, LifeSpan b)
{
return a.DestroyAfter == b.DestroyAfter;
}
public static object operator !=(LifeSpan a, LifeSpan b)
{
return a.DestroyAfter != b.DestroyAfter;
}
}
[HarmonyPatch(typeof(TowerInventory), nameof(TowerInventory.GetTowerInventoryCount))]
[HarmonyPrefix]
static void TowerInventory_GetTowerInventoryCount(TowerInventory __instance, TowerModel def)
{
if (def.baseId == ModContent.GetInstance<ClickerTower>().Id && !__instance.towerCounts.TryGetValue(def.baseId, out _))
{
__instance.towerCounts[def.baseId] = 0;
}
}
/// <inheritdoc />
public override void OnTowerLoaded(Tower tower, TowerSaveDataModel saveData)
{
if(tower.towerModel.baseId == ModContent.GetInstance<ClickerTower>().Id)
{
tower.towerModel.GetAttackModel().weapons[0].projectile = CursorUpgrade.GetProjectileModel();
tower.towerModel.GetAttackModel().weapons[0].rate = CursorUpgrade.GetRawRate();
CursorUpgrade.CursorTower = tower;
}
}
public static long SavedCursorPops { get; set; }
public override void OnUpdate()
{
// ReSharper disable once Unity.NoNullPropagation
if (InGame.instance?.GetSimulation() is null)
return;
var delta = Time.deltaTime;
if (InGame.instance.inputManager.cursorDown)
TimeMouseHeld += delta;
else
TimeMouseHeld = 0;
TimeSinceLastAttack += delta;
foreach (var (projectile, lifeSpan) in ProjectileAge)
{
lifeSpan.Time += delta;
if (lifeSpan.Time > lifeSpan.DestroyAfter)
{
projectile.Expire();
ProjectileAge.Remove(projectile);
}
}
HandleCursorUpgradeRoller();
if(CurrentUpgrades.Count == 0)
return;
if (CursorUpgrade.CursorTower is { IsDestroyed: false })
{
var position = new Vector2(InGame.instance.inputManager.cursorPositionWorld);
CursorUpgrade.CursorTower.PositionTower(position);
}
foreach (var upgrade in CurrentUpgrades.OrderBy(x=>x.Tier))
{
upgrade.OnUpdate();
}
if (!InGame.instance.inputManager.cursorInWorld ||
!InGame.instance.inputManager.cursorDown)
return;
CursorUpgrade.TryCreateProjectile();
}
private static void HandleCursorUpgradeRoller()
{
if (_cursorUpgradeImage == null)
{
if (ShopMenu.instance == null)
return;
if (ShopMenu.instance.powersButton.transform.parent.FindChild("CursorUpgradePanel") != null)
return;
var panel =
ShopMenu.instance.powersButton.transform.parent.gameObject.AddModHelperPanel(new Info("CursorUpgradePanel"));
panel.transform.localPosition = Vector3.zero;
var cursorUpgradeButton = panel.AddButton(new Info("CursorUpgradeButton", 275),
ModContent.GetSpriteReference<Main>("CursorUpgrade").guidRef,
new Action(() => ModGameMenu.Open<UpgradeMenu>()));
var position = ShopMenu.instance.powersButton.transform.localPosition;
cursorUpgradeButton.transform.localPosition = position with { x = position.x - 300 };
_cursorUpgradeImage = cursorUpgradeButton.AddImage(new Info("CursorUpgradeImage", 350),
VanillaSprites.SmallSquareGlowOutline);
_cursorUpgradeImage.gameObject.AddComponent<Roller>();
_cursorUpgradeImage.gameObject.SetActive(false);
}
var nextUpgrades = new HashSet<CursorUpgrade>();
foreach (var path in Paths)
{
var tier = UpgradeMenu.PurchasedUpgrades[path];
if (tier == UpgradeMenu.UnPurchased)
{
tier = path == Path.Clicker ? 0 : 1;
}
else
{
tier++;
}
if (CursorUpgrade.Cache[path].TryGetValue(tier, out var upgrade))
{
nextUpgrades.Add(upgrade);
}
}
_cursorUpgradeImage.gameObject.SetActive(nextUpgrades.Any(upgrade => InGame.instance.GetCash() >= CostHelper.CostForDifficulty(upgrade.Cost, InGame.instance.GetGameModel())));
}
[HarmonyPatch(typeof(Projectile), nameof(Projectile.OnDestroy))]
[HarmonyPrefix]
static void Projectile_Destroy(Projectile __instance)
{
if (ProjectileNameCache.Contains(__instance.model.name))
{
foreach (var upgrade in CurrentUpgrades.OrderBy(x => x.Tier))
{
upgrade.OnDestroy(__instance);
}
}
ProjectileHitBloon.Remove(__instance.Id.Id);
}
[HarmonyPatch(typeof(Projectile), nameof(Projectile.CollideBloon))]
[HarmonyPrefix]
static void Projectile_CollideBloon(Projectile __instance)
{
ProjectileHitBloon.Add(__instance.Id.Id);
}
[HarmonyPatch(typeof(BloonMenu), nameof(BloonMenu.OnClickedResetDamage))]
[HarmonyPostfix]
static void BloonMenu_OnClickedResetDamage() => SavedCursorPops = 0;
private static ModHelperImage? _cursorUpgradeImage;
[HarmonyPatch(typeof(ShopMenu), nameof(ShopMenu.Initialise))]
[HarmonyPostfix]
static void ShopMenu_Initialise(ShopMenu __instance)
{
if (__instance.powersButton.transform.parent.FindChild("CursorUpgradePanel") != null)
return;
var panel =
__instance.powersButton.transform.parent.gameObject.AddModHelperPanel(new Info("CursorUpgradePanel"));
panel.transform.localPosition = Vector3.zero;
var cursorUpgradeButton = panel.AddButton(new Info("CursorUpgradeButton", 275),
ModContent.GetSpriteReference<Main>("CursorUpgrade").guidRef,
new Action(() => ModGameMenu.Open<UpgradeMenu>()));
var position = __instance.powersButton.transform.localPosition;
cursorUpgradeButton.transform.localPosition = position with { x = position.x - 300 };
_cursorUpgradeImage = cursorUpgradeButton.AddImage(new Info("CursorUpgradeImage", 350),
VanillaSprites.SmallSquareGlowOutline);
_cursorUpgradeImage.gameObject.AddComponent<Roller>();
_cursorUpgradeImage.gameObject.SetActive(false);
}
#region Saving
[HarmonyPatch(typeof(Map), nameof(Map.GetSaveData))]
[HarmonyPostfix]
static void OnMapSaved(MapSaveDataModel mapData)
{
var json = JsonConvert.SerializeObject(UpgradeMenu.PurchasedUpgrades);
mapData.metaData["CursorUpgrade"] = json;
mapData.metaData["CursorPops"] = SavedCursorPops.ToString(CultureInfo.InvariantCulture);
foreach (var upgrade in CurrentUpgrades.OrderBy(x => x.Tier))
{
upgrade.OnMapSaved(mapData);
}
}
[HarmonyPatch(typeof(Map), nameof(Map.SetSaveData))]
[HarmonyPostfix]
static void OnMapLoaded(MapSaveDataModel mapData)
{
CurrentUpgrades.Clear();
if (mapData.metaData.TryGetValue("CursorUpgrade", out var data))
{
UpgradeMenu.PurchasedUpgrades = JsonConvert.DeserializeObject<Dictionary<Path, int>>(data) ?? new Dictionary<Path, int>();
foreach (var (path, tier) in UpgradeMenu.PurchasedUpgrades)
{
for (var i = 0; i <= tier; i++)
{
if (!CursorUpgrade.Cache[path].TryGetValue(i, out var upgrade))
continue;
CurrentUpgrades.Add(upgrade);
}
}
}
else
{
UpgradeMenu.PurchasedUpgrades = Paths.ToDictionary(path => path, _ => UpgradeMenu.UnPurchased);
}
SavedCursorPops = mapData.metaData.TryGetValue("CursorPops", out var cursorPops) ? long.Parse(cursorPops, CultureInfo.InvariantCulture) : 0;
foreach (var upgrade in CurrentUpgrades.OrderBy(x => x.Tier))
{
upgrade.OnMapLoaded(mapData);
}
CursorUpgrade.UpdateTower();
}
#endregion
}