forked from HasteModding/HasteEffects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
133 lines (111 loc) · 4.99 KB
/
Main.cs
File metadata and controls
133 lines (111 loc) · 4.99 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
namespace HasteEffects;
[Landfall.Modding.LandfallPlugin]
public class Main
{
// The mod's unique identifier (GUID) for internal use and patching.
public static string GUID = "com.github.ignoredsoul.hastyeffects";
// The name of the mod, used for the config menu.
public static string NAME = "HastyEffects";
// The Harmony instance
private static HarmonyLib.Harmony harmony;
// List to hold the UI stats that will be displayed.
private static List<UIStats> stats = new List<UIStats>();
static Main()
{
harmony = new(GUID);
harmony.PatchAll(typeof(Patching));
}
// Accessor property
public static Values Values { get; set; }
/// <summary>
/// Randomizes stats and updates the UI accordingly.
/// </summary>
internal static void Randomize()
{
// Get all available stats from the Stat enum and convert to a list.
List<Stat> availableStats = Enum.GetValues(typeof(Stat)).Cast<Stat>().ToList();
List<Stat> selectedStats = new();
// Pick a random stat and add it to the selected stats.
Stat gstat = availableStats[NumberUtils.random.Next(0, availableStats.Count)];
availableStats.Remove(gstat);
selectedStats.Add(gstat);
// Add up to 5 more stats, with a random chance to include each one.
for (int i = 0; (i < 5 && availableStats.Any()); i++)
{
Stat stat = availableStats[NumberUtils.random.Next(0, availableStats.Count)];
availableStats.Remove(stat);
// The chance of adding a stat decreases as we add more.
if (NumberUtils.NextD() > (i * 0.25)) selectedStats.Add(stat);
}
// Destroy all previous stats and clear the list.
stats.ForEach(stat => stat.Destroy());
stats.Clear();
// Check if the game is in endless mode. Probs a better way but it works
bool isEndless = UnityEngine.GameObject.Find("GAME/UI_Gameplay/LeftCorner/Distance").activeSelf;
if (isEndless) stats.Add(new("", 0));
// Randomize and display the selected stats in the UI.
selectedStats.ForEach((stat) =>
{
// Apply a randomized value to the selected stat
Manager.RandomizeStat(stat);
// Add the stat to the UI
UIStats newStat = new(
stat.ToString() + ": " + Manager.GetStat(stat).multiplier.ToString("0.0") + "x",
(stats.Count + 1)
);
// Then add statui to the list
stats.Add(newStat);
});
// Hide the first stat in endless mode since it's a blank stat and is used for making space.
if (isEndless) stats[0].InfoObject.SetActive(false);
}
}
[HarmonyLib.HarmonyPatch]
public class Patching
{
// Think I can use 'On.Player.ResetPlayer' but honestly, don't care to try.
/// <summary>
/// Patches the "RestartPlayer_Launch" method to randomize stats.
/// </summary>
/// <param name="spawnPoint">The new spawn point for the player. Redundant</param>
/// <param name="minVel">Optional minimum velocity for the restart. Redundant</param>
[HarmonyLib.HarmonyPatch(typeof(PlayerCharacter), "RestartPlayer_Launch", [typeof(UnityEngine.Transform), typeof(float)])]
[HarmonyLib.HarmonyPostfix]
private static void OnRestartPlayer(UnityEngine.Transform spawnPoint, float minVel = 0f)
{ if (Manager.IsRun) Main.Randomize(); }
/// <summary>
/// Patches the "RestartPlayer_Still" method to randomize stats.
/// </summary>
/// <param name="spawnPoint">The new spawn point for the player. Redundant</param>
[HarmonyLib.HarmonyPatch(typeof(PlayerCharacter), "RestartPlayer_Still", [typeof(UnityEngine.Transform)])]
[HarmonyLib.HarmonyPostfix]
private static void OnRestartPlayer(UnityEngine.Transform spawnPoint)
{ if (Manager.IsRun) Main.Randomize(); }
/// <summary>
/// Patches the "RestartPlayerWithAnim" method to randomize stats.
/// </summary>
/// <param name="spawnPoint">The new spawn point for the player. Redundant</param>
/// <param name="animId">Optional animation ID. Redundant</param>
[HarmonyLib.HarmonyPatch(typeof(PlayerCharacter), "RestartPlayerWithAnim", [typeof(UnityEngine.Transform), typeof(int)])]
[HarmonyLib.HarmonyPostfix]
private static void OnRestartPlayer(UnityEngine.Transform spawnPoint, int animId = 1)
{ if (Manager.IsRun) Main.Randomize(); }
/// <summary>
/// Patches the "OnStringChanged" method to update the localized text if it belongs to this mod's GUID.
/// </summary>
/// <param name="__instance">The instance of the LocalizeUIText being patched.</param>
[HarmonyLib.HarmonyPatch(typeof(Zorro.Localization.LocalizeUIText), "OnStringChanged")]
[HarmonyLib.HarmonyPostfix]
private static void OnStringChangedPostfix(Zorro.Localization.LocalizeUIText __instance)
{
if (__instance.String?.TableReference.TableCollectionName != Main.GUID) return;
__instance.Text.text = __instance.String.TableEntryReference.Key;
}
/// <summary>
/// Patches the "RegisterPage" method to initialize the Values object when the settings page is registered.
/// </summary>
/// <param name="__instance">The instance of HasteSettingsHandler being patched.</param>
[HarmonyLib.HarmonyPatch(typeof(HasteSettingsHandler), "RegisterPage")]
[HarmonyLib.HarmonyPrefix]
private static void RegisterPagePrefix(HasteSettingsHandler __instance) => Main.Values = new Values();
}