forked from SamsonAllen13/ClickerClass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClickerSystem.cs
560 lines (510 loc) · 21.7 KB
/
ClickerSystem.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
using ClickerClass.Items;
using ClickerClass.Items.Misc;
using ClickerClass.Projectiles;
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using Terraria;
using Terraria.DataStructures;
using Terraria.ID;
using Terraria.Localization;
using Terraria.ModLoader;
namespace ClickerClass
{
/// <summary>
/// Manages registering clicker class related content and provides basic methods to check for content being clicker class related
/// </summary>
public class ClickerSystem : ModSystem
{
//Clientside only hence why player instance to play the sound at is not necessary
/// <summary>
/// To prevent certain methods being called when they shouldn't
/// </summary>
internal static bool FinalizedRegisterCompat { get; private set; }
internal static LocalizedText UnknownText { get; private set; }
internal static LocalizedText DefaultClickerWeaponTooltipText { get; private set; }
private static HashSet<int> ClickerItems { get; set; }
private static Dictionary<int, string> ClickerWeaponBorderTexture { get; set; }
private static HashSet<int> ClickerWeapons { get; set; }
private static Dictionary<int, Action<int>> SFXButtons { get; set; }
private static HashSet<int> ClickerWeaponProjectiles { get; set; }
private static HashSet<int> ClickerProjectiles { get; set; }
/// <summary>
/// A dictionary containing registered (!) ClickEffects. When "creating" new ones to assign to something, it clones it from this
/// </summary>
private static Dictionary<string, ClickEffect> ClickEffectsByName { get; set; }
/// <summary>
/// A dictionary containing <see cref="ClickEffect.DisplayName"/>.
/// </summary>
internal static Dictionary<string, LocalizedText> DisplayNamesByName { get; private set; }
/// <summary>
/// A dictionary containing <see cref="ClickEffect.Description"/>.
/// </summary>
internal static Dictionary<string, LocalizedText> DescriptionsByName { get; private set; }
public override void OnModLoad()
{
FinalizedRegisterCompat = false;
UnknownText = Language.GetOrRegister(Mod.GetLocalizationKey("Common.Unknown"));
DefaultClickerWeaponTooltipText = Language.GetOrRegister(Mod.GetLocalizationKey("Common.Tooltips.Clicker"));
ClickerItems = new HashSet<int>();
ClickerWeaponBorderTexture = new Dictionary<int, string>();
ClickerWeapons = new HashSet<int>();
SFXButtons = new Dictionary<int, Action<int>>();
ClickerProjectiles = new HashSet<int>();
ClickerWeaponProjectiles = new HashSet<int>();
ClickEffectsByName = new Dictionary<string, ClickEffect>();
DisplayNamesByName = new Dictionary<string, LocalizedText>();
DescriptionsByName = new Dictionary<string, LocalizedText>();
}
public override void OnModUnload()
{
FinalizedRegisterCompat = false;
ClickerItems = null;
ClickerWeaponBorderTexture?.Clear();
ClickerWeaponBorderTexture = null;
ClickerWeapons = null;
SFXButtons = null;
ClickerProjectiles = null;
ClickerWeaponProjectiles = null;
ClickEffectsByName?.Clear();
ClickEffectsByName = null;
DisplayNamesByName?.Clear();
DisplayNamesByName = null;
DescriptionsByName?.Clear();
DescriptionsByName = null;
}
public override void SetStaticDefaults()
{
ClickEffect.LoadMiscEffects();
}
public override void PostAddRecipes()
{
FinalizedRegisterCompat = true;
}
public static string UniqueEffectName(Mod mod, string internalName) => $"{mod.Name}:{internalName}";
/// <summary>
/// Returns the effect dictionary
/// </summary>
/// <returns>IReadOnlyDictionary[string, ClickEffect]</returns>
public static IReadOnlyDictionary<string, ClickEffect> GetAllEffects()
{
return ClickEffectsByName;
}
/// <summary>
/// Returns all existing effects' internal names
/// </summary>
/// <returns>List[string]</returns>
public static List<string> GetAllEffectNames()
{
//Mod compat version of GetAllEffects() since ClickEffect is an unknown type
return GetAllEffects().Keys.ToList();
}
/// <summary>
/// Mod Compat way of accessing an effect's stats. <see cref="null"/> if not found.
/// "Mod": The mod the effect belongs to (Mod).
/// | "InternalName": The internal name (string).
/// | "UniqueName": The unique name (string) (should match the input string).
/// | "DisplayName": The displayed name (LocalizedText).
/// | "Description": The description (LocalizedText).
/// | "Amount": The amount of clicks to trigger the effect (int).
/// | "ColorFunc": The color (Color) if invoked.
/// | "Action": The method ran when triggered (Action[Player, EntitySource_ItemUse_WithAmmo, Vector2, int, int, float]).
/// | "PreHardMode": Belongs to something available pre-hardmode (bool).
/// </summary>
/// <param name="name">The unique name</param>
/// <returns>Dictionary[string, object]</returns>
internal static Dictionary<string, object> GetClickEffectAsDict(string name)
{
if (IsClickEffect(name, out ClickEffect effect))
{
return effect.ToDictionary();
}
return null;
}
/// <summary>
/// Checks if an effect of this name exists
/// </summary>
/// <param name="name">The unique name</param>
/// <returns><see langword="true"/> if valid</returns>
public static bool IsClickEffect(string name)
{
return ClickEffectsByName.TryGetValue(name, out _);
}
/// <summary>
/// Checks if an effect of this name exists, and assigns it
/// </summary>
/// <param name="name">The unique name</param>
/// <param name="effect">The <see cref="ClickEffect"/> associated with this name</param>
/// <returns><see langword="true"/> if valid</returns>
public static bool IsClickEffect(string name, out ClickEffect effect)
{
effect = null;
if (ClickEffectsByName.TryGetValue(name, out ClickEffect other))
{
effect = (ClickEffect)other.Clone();
return true;
}
return false;
}
/// <summary>
/// Checks if an effect has already defined its display name, and assigns it
/// </summary>
/// <param name="name">The unique name</param>
/// <param name="displayName">The display name's <see cref="LocalizedText"/></param>
/// <returns><see langword="true"/> if already defined</returns>
public static bool TryGetClickEffectName(string name, out LocalizedText displayName)
{
displayName = null;
if (DisplayNamesByName.TryGetValue(name, out LocalizedText other))
{
displayName = other;
return true;
}
return false;
}
/// <summary>
/// Checks if an effect has already defined its description, and assigns it
/// </summary>
/// <param name="name">The unique name</param>
/// <param name="description">The description's <see cref="LocalizedText"/></param>
/// <returns><see langword="true"/> if already defined</returns>
public static bool TryGetClickEffectDescription(string name, out LocalizedText description)
{
description = null;
if (DescriptionsByName.TryGetValue(name, out LocalizedText other))
{
description = other;
return true;
}
return false;
}
/// <summary>
/// Call this in <see cref="Mod.PostSetupContent"/> or <see cref="ModType.SetStaticDefaults"/> to register this click effect
/// </summary>
/// <param name="mod">The mod this effect belongs to. ONLY USE YOUR OWN MOD INSTANCE FOR THIS!</param>
/// <param name="internalName">The internal name of the effect. Turns into the unique name combined with the associated mod</param>
/// <param name="amount">The amount of clicks required to trigger the effect</param>
/// <param name="colorFunc">The (dynamic) text color representing the effect in the tooltip</param>
/// <param name="action">The method that runs when the effect is triggered</param>
/// <param name="preHardMode">If this effect primarily belongs to something available pre-hardmode</param>
/// <param name="nameArgs">Arguments that need to be bound to the display name</param>
/// <param name="descriptionArgs">Arguments that need to be bound to the description</param>
/// <returns>The unique identifier</returns>
/// <exception cref="InvalidOperationException"/>
public static string RegisterClickEffect(Mod mod, string internalName, int amount, Func<Color> colorFunc, Action<Player, EntitySource_ItemUse_WithAmmo, Vector2, int, int, float> action, bool preHardMode = false, object[] nameArgs = null, object[] descriptionArgs = null)
{
if (FinalizedRegisterCompat)
{
throw new InvalidOperationException("Tried to register a click effect at the wrong time, do so in Mod.PostSetupContent or ModItem.SetStaticDefaults");
}
if (string.IsNullOrEmpty(internalName))
{
throw new InvalidOperationException($"internalName is either null or empty. Give it a proper value");
}
string uniqueName = UniqueEffectName(mod, internalName);
if (!IsClickEffect(uniqueName))
{
ClickEffect effect = new ClickEffect(mod, internalName, amount, colorFunc, action, preHardMode, nameArgs, descriptionArgs);
ClickEffectsByName.Add(uniqueName, effect);
DisplayNamesByName.Add(uniqueName, effect.DisplayName);
DescriptionsByName.Add(uniqueName, effect.Description);
return uniqueName;
}
else
{
throw new InvalidOperationException($"The effect '{uniqueName}' has already been registered, duplicate detected");
}
}
/// <summary>
/// Call this in <see cref="Mod.PostSetupContent"/> or <see cref="ModType.SetStaticDefaults"/> to register this click effect
/// </summary>
/// <param name="mod">The mod this effect belongs to. ONLY USE YOUR OWN MOD INSTANCE FOR THIS!</param>
/// <param name="internalName">The internal name of the effect. Turns into the unique name combined with the associated mod</param>
/// <param name="amount">The amount of clicks required to trigger the effect</param>
/// <param name="color">The text color representing the effect in the tooltip</param>
/// <param name="action">The method that runs when the effect is triggered</param>
/// <remarks>For dynamic colors, use the Func[Color] overload</remarks>
/// <param name="preHardMode">If this effect primarily belongs to something available pre-hardmode</param>
/// <param name="nameArgs">Arguments that need to be bound to the display name</param>
/// <param name="descriptionArgs">Arguments that need to be bound to the description</param>
/// <returns>The unique identifier</returns>
/// <exception cref="InvalidOperationException"/>
public static string RegisterClickEffect(Mod mod, string internalName, int amount, Color color, Action<Player, EntitySource_ItemUse_WithAmmo, Vector2, int, int, float> action, bool preHardMode = false, object[] nameArgs = null, object[] descriptionArgs = null)
{
return RegisterClickEffect(mod, internalName, amount, () => color, action, preHardMode, nameArgs, descriptionArgs);
}
/// <summary>
/// Call in <see cref="ModItem.SetDefaults"/> to set important default fields for a clicker weapon. Set fields:
/// DamageType, useTime, useAnimation, useStyle, holdStyle, noMelee, shoot, shootSpeed.
/// Only change them afterwards if you know what you are doing!
/// </summary>
/// <param name="item">The <see cref="Item"/> to set the defaults for</param>
public static void SetClickerWeaponDefaults(Item item)
{
item.DamageType = ModContent.GetInstance<ClickerDamage>();
item.useTime = 2;
item.useAnimation = 2;
item.useStyle = ItemUseStyleID.Shoot;
item.holdStyle = 3;
item.noMelee = true;
item.shoot = ModContent.ProjectileType<ClickDamage>();
item.shootSpeed = 1f;
}
/// <summary>
/// Call in <see cref="ModItem.SetDefaults"/> to set important default fields for a "sfx button". Set fields:
/// maxStack.
/// Only change them afterwards if you know what you are doing!
/// </summary>
/// <param name="item">The <see cref="Item"/> to set the defaults for</param>
public static void SetSFXButtonDefaults(Item item)
{
item.maxStack = SFXButtonBase.StackAmount;
}
/// <summary>
/// Call in <see cref="ModProjectile.SetDefaults"/> to set important default fields for a clicker projectile. Set fields:
/// DamageType.
/// Only change them afterwards if you know what you are doing!
/// </summary>
/// <param name="projectile">The <see cref="Projectile"/> to set the defaults for</param>
public static void SetClickerProjectileDefaults(Projectile projectile)
{
projectile.DamageType = ModContent.GetInstance<ClickerDamage>();
}
/// <summary>
/// Call this in <see cref="ModType.SetStaticDefaults"/> to register this projectile into the "clicker class" category
/// </summary>
/// <param name="modProj">The <see cref="ModProjectile"/> that is to be registered</param>
/// <exception cref="InvalidOperationException"/>
public static void RegisterClickerProjectile(ModProjectile modProj)
{
if (FinalizedRegisterCompat)
{
throw new InvalidOperationException("Tried to register a clicker projectile at the wrong time, do so in ModProjectile.SetStaticDefaults");
}
int type = modProj.Projectile.type;
if (!ClickerProjectiles.Contains(type))
{
ClickerProjectiles.Add(type);
}
}
/// <summary>
/// Call this in <see cref="ModType.SetStaticDefaults"/> to register this projectile into the "clicker weapon" category.
/// <br>This is only for projectiles spawned by clickers directly (Item.shoot). Clicker Class only uses one such projectile for all it's clickers. Only use this if you know what you are doing!</br>
/// <br>Various effects will only proc "on click" by checking this category instead of "all clicker class projectiles"</br>
/// </summary>
/// <param name="modProj">The <see cref="ModProjectile"/> that is to be registered</param>
/// <exception cref="InvalidOperationException"/>
public static void RegisterClickerWeaponProjectile(ModProjectile modProj)
{
if (FinalizedRegisterCompat)
{
throw new InvalidOperationException("Tried to register a clicker weapon projectile at the wrong time, do so in ModProjectile.SetStaticDefaults");
}
int type = modProj.Projectile.type;
if (!ClickerWeaponProjectiles.Contains(type))
{
ClickerWeaponProjectiles.Add(type);
}
}
/// <summary>
/// Call this in <see cref="ModType.SetStaticDefaults"/> to register this item into the "clicker class" category
/// </summary>
/// <param name="modItem">The <see cref="ModItem"/> that is to be registered</param>
/// <exception cref="InvalidOperationException"/>
public static void RegisterClickerItem(ModItem modItem)
{
if (FinalizedRegisterCompat)
{
throw new InvalidOperationException("Tried to register a clicker item at the wrong time, do so in ModItem.SetStaticDefaults");
}
int type = modItem.Item.type;
if (!ClickerItems.Contains(type))
{
ClickerItems.Add(type);
}
}
/// <summary>
/// Call this in <see cref="ModType.SetStaticDefaults"/> to register this weapon into the "clicker class" category as a "clicker".<br/>
/// Do not call <see cref="RegisterClickerItem"/> with it as this method does this already by itself
/// </summary>
/// <param name="modItem">The <see cref="ModItem"/> that is to be registered</param>
/// <param name="borderTexture">The path to the border texture (optional)</param>
/// <exception cref="InvalidOperationException"/>
public static void RegisterClickerWeapon(ModItem modItem, string borderTexture = null)
{
if (FinalizedRegisterCompat)
{
throw new InvalidOperationException("Tried to register a clicker weapon at the wrong time, do so in ModItem.SetStaticDefaults");
}
RegisterClickerItem(modItem);
int type = modItem.Item.type;
if (!ClickerWeapons.Contains(type))
{
ClickerWeapons.Add(type);
if (borderTexture != null)
{
if (ModContent.HasAsset(borderTexture))
{
if (!ClickerWeaponBorderTexture.ContainsKey(type))
{
ClickerWeaponBorderTexture.Add(type, borderTexture);
}
}
else
{
ClickerClass.mod.Logger.Info($"Border texture for {modItem.Name} not found: {borderTexture}");
}
}
}
}
/// <summary>
/// Call this in <see cref="ModType.SetStaticDefaults"/> to register this item into the "sfx button" category.<br/>
/// It will automatically contribute to the active "sfx buttons" when in the inventory<br/>
/// Do not call <see cref="RegisterClickerItem"/> with it as this method does this already by itself
/// </summary>
/// <param name="modItem">The <see cref="ModItem"/> that is to be registered</param>
/// <param name="playSoundAction">The method that runs that will play the sound</param>
/// <exception cref="InvalidOperationException"/>
public static void RegisterSFXButton(ModItem modItem, Action<int> playSoundAction)
{
if (FinalizedRegisterCompat)
{
throw new InvalidOperationException("Tried to register an sfx button at the wrong time, do so in ModItem.SetStaticDefaults");
}
RegisterClickerItem(modItem);
int type = modItem.Item.type;
if (!SFXButtons.ContainsKey(type))
{
SFXButtons.Add(type, playSoundAction);
}
}
/// <summary>
/// Returns the border texture of the item of this type
/// </summary>
/// <param name="type">The item type</param>
/// <returns>The path to the border texture, null if not found</returns>
public static string GetPathToBorderTexture(int type)
{
if (ClickerWeaponBorderTexture.TryGetValue(type, out string borderTexture))
{
return borderTexture;
}
return null;
}
/// <summary>
/// Call this to check if a projectile type belongs to the "clicker class" category
/// </summary>
/// <param name="type">The projectile type to be checked</param>
/// <returns><see langword="true"/> if that category</returns>
public static bool IsClickerProj(int type)
{
return ClickerProjectiles.Contains(type);
}
/// <summary>
/// Call this to check if a projectile belongs to the "clicker class" category
/// </summary>
/// <param name="proj">The <see cref="Projectile"/> to be checked</param>
/// <returns><see langword="true"/> if that category</returns>
public static bool IsClickerProj(Projectile proj)
{
return IsClickerProj(proj.type);
}
/// <summary>
/// Call this to check if a projectile type belongs to the "clicker weapon" category.
/// <br>Various effects will only proc "on click" by checking this category instead of "all clicker class projectiles"</br>
/// </summary>
/// <param name="type">The projectile type to be checked</param>
/// <returns><see langword="true"/> if that category</returns>
public static bool IsClickerWeaponProj(int type)
{
return ClickerWeaponProjectiles.Contains(type);
}
/// <summary>
/// Call this to check if a projectile belongs to the "clicker weapon" category.
/// <br>Various effects will only proc "on click" by checking this category instead of "all clicker class projectiles"</br>
/// </summary>
/// <param name="proj">The <see cref="Projectile"/> to be checked</param>
/// <returns><see langword="true"/> if that category</returns>
public static bool IsClickerWeaponProj(Projectile proj)
{
return IsClickerWeaponProj(proj.type);
}
/// <summary>
/// Call this to check if an item type belongs to the "clicker class" category
/// </summary>
/// <param name="type">The item type to be checked</param>
/// <returns><see langword="true"/> if that category</returns>
public static bool IsClickerItem(int type)
{
return ClickerItems.Contains(type);
}
/// <summary>
/// Call this to check if an item belongs to the "clicker class" category
/// </summary>
/// <param name="item">The <see cref="Item"/> to be checked</param>
/// <returns><see langword="true"/> if a "clicker class" item</returns>
public static bool IsClickerItem(Item item)
{
return IsClickerItem(item.type);
}
/// <summary>
/// Call this to check if an item is an "sfx button"
/// </summary>
/// <param name="item">The item to be checked</param>
/// <returns><see langword="true"/> if an "sfx button"</returns>
public static bool IsSFXButton(Item item)
{
return SFXButtons.ContainsKey(item.type);
}
/// <summary>
/// Call this to check if an item type is an "sfx button"
/// </summary>
/// <param name="type">The item type to be checked</param>
/// <returns><see langword="true"/> if an "sfx button"</returns>
public static bool IsSFXButton(int type)
{
return SFXButtons.ContainsKey(type);
}
/// <summary>
/// Call this to check if an item type is an "sfx button"
/// </summary>
/// <param name="type">The item type to be checked</param>
/// <param name="playSoundAction">The <see cref="Action<int>"/> of this item for convenience, only assigned if method returns true</param>
/// <returns><see langword="true"/> if an "sfx button"</returns>
public static bool IsSFXButton(int type, out Action<int> playSoundAction)
{
return SFXButtons.TryGetValue(type, out playSoundAction);
}
/// <summary>
/// Call this to check if an item type is a "clicker"
/// </summary>
/// <param name="type">The item type to be checked</param>
/// <returns><see langword="true"/> if a "clicker"</returns>
public static bool IsClickerWeapon(int type)
{
return ClickerWeapons.Contains(type);
}
/// <summary>
/// Call this to check if an item is a "clicker"
/// </summary>
/// <param name="item">The <see cref="Item"/> to be checked</param>
/// <returns><see langword="true"/> if a "clicker"</returns>
public static bool IsClickerWeapon(Item item)
{
return IsClickerWeapon(item.type);
}
/// <summary>
/// Call this to check if an item is a "clicker"
/// </summary>
/// <param name="item">The <see cref="Item"/> to be checked</param>
/// <param name="clickerItem">The <see cref="ClickerItemCore"/> of this item for convenience, only assigned if method returns true</param>
/// <returns><see langword="true"/> if a "clicker"</returns>
public static bool IsClickerWeapon(Item item, out ClickerItemCore clickerItem)
{
clickerItem = null;
_ = IsClickerWeapon(item) && item.TryGetGlobalItem(out clickerItem);
return clickerItem != null;
}
}
}