-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimerMod.cs
More file actions
215 lines (171 loc) · 6.86 KB
/
Copy pathTimerMod.cs
File metadata and controls
215 lines (171 loc) · 6.86 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
using UnityEngine;
using UnityEngine.SceneManagement;
using BepInEx;
using BepInEx.Configuration;
using GlobalEnums;
using System;
namespace TimerMod;
public class Keybinds
{
public ConfigEntry<KeyboardShortcut> SetStart;
public ConfigEntry<KeyboardShortcut> SetEnd;
public ConfigEntry<KeyboardShortcut> ToggleTriggerMethod;
public ConfigEntry<KeyboardShortcut> CancelTimer;
public ConfigEntry<KeyboardShortcut> StartTimer;
public ConfigEntry<KeyboardShortcut> EndTimer;
public ConfigEntry<KeyboardShortcut> ResetPb;
public ConfigEntry<KeyboardShortcut> ToggleTimerVisibility;
public Keybinds(ConfigFile Config)
{
SetStart = Config.Bind("Shortcuts", "SetStart", new KeyboardShortcut(KeyCode.F8), "");
SetEnd = Config.Bind("Shortcuts", "SetEnd", new KeyboardShortcut(KeyCode.F9), "");
ToggleTriggerMethod = Config.Bind("Shortcuts", "ToggleTriggerMethod", new KeyboardShortcut(KeyCode.F10), "");
CancelTimer = Config.Bind("Shortcuts", "CancelTimer", new KeyboardShortcut(KeyCode.None), "Cancel the timer (does not affect pb)");
StartTimer = Config.Bind("Shortcuts", "StartTimer", new KeyboardShortcut(KeyCode.None), "");
EndTimer = Config.Bind("Shortcuts", "EndTimer", new KeyboardShortcut(KeyCode.None), "");
ResetPb = Config.Bind("Shortcuts", "ResetPb", new KeyboardShortcut(KeyCode.None), "");
ToggleTimerVisibility = Config.Bind("Shortcuts", "ToggleTimerVisibility", new KeyboardShortcut(KeyCode.None), "");
}
}
[BepInDependency(DependencyGUID: "org.silksong-modding.modlist")]
[BepInAutoPlugin(id: "io.github.hk-speedrunning.timermod")]
public partial class TimerMod : BaseUnityPlugin
{
public static ConfigFile config;
private TimerDisplay timerDisplay;
private Triggers.Trigger startTrigger = new Triggers.SceneTrigger("");
private Triggers.Trigger endTrigger = new Triggers.SceneTrigger("");
private Keybinds keybinds;
private bool usingSceneTriggers = false;
private double time = 0.0;
private bool timerPaused = true;
private double[] history = new double[5];
private int history_num = 0;
private double pb = 0;
private int funSceneCount = 0;
private ConfigEntry<bool> showSpeed;
private Vector2 startPos;
private ConfigEntry<Vector2> startTriggerSize;
private ConfigEntry<Vector2> endTriggerSize;
private void resetPb()
{
pb = 0;
timerDisplay.setPbTime(getTimeText(pb));
}
private void endTimer()
{
Vector2 endPos = HeroController.instance.gameObject.transform.position;
double speedx = Math.Abs(startPos.x - endPos.x) / time;
double speedy = Math.Abs(startPos.y - endPos.y) / time;
history[history_num] = time;
history_num += 1;
history_num %= 5;
timerPaused = true;
if (pb == 0 || time < pb)
{
pb = time;
timerDisplay.setPbTime(getTimeText(pb));
if (showSpeed.Value)
timerDisplay.setPbTime($"{getTimeText(pb)}\n{speedx:0.00} u/s, {speedy:0.00} u/s");
}
}
private void startTimer()
{
time = 0;
timerPaused = false;
startPos = HeroController.instance.gameObject.transform.position;
Logger.LogInfo("Started timer");
}
private void LateUpdate()
{
if (funSceneCount != 4)
return;
if (startTrigger.active() && timerPaused)
startTimer();
if (endTrigger.active() && !timerPaused)
endTimer();
if (keybinds.ToggleTriggerMethod.Value.IsDown())
usingSceneTriggers = !usingSceneTriggers;
if (keybinds.CancelTimer.Value.IsDown())
{
Logger.LogInfo("Canceled");
time = 0;
timerPaused = true;
}
if (keybinds.ResetPb.Value.IsDown())
resetPb();
if (keybinds.StartTimer.Value.IsDown())
startTimer();
if (keybinds.EndTimer.Value.IsDown())
endTimer();
if (keybinds.ToggleTimerVisibility.Value.IsDown())
timerDisplay.toggleVisibility();
if (keybinds.SetStart.Value.IsDown())
{
startTrigger.destroy();
resetPb();
if (usingSceneTriggers)
{
startTrigger = new Triggers.SceneTrigger(SceneManager.GetActiveScene().name);
Logger.LogInfo("Set start scene");
}
else
{
startTrigger = new Triggers.CollisionTrigger(GameManager.instance.hero_ctrl.transform.position,
startTriggerSize.Value, new Color(0.1f, 0.4f, 0.1f));
Logger.LogInfo("Set start pos");
}
}
if (keybinds.SetEnd.Value.IsDown())
{
endTrigger.destroy();
resetPb();
if (usingSceneTriggers)
{
endTrigger = new Triggers.SceneTrigger(SceneManager.GetActiveScene().name);
Logger.LogInfo("Set end scene");
}
else
{
endTrigger = new Triggers.CollisionTrigger(GameManager.instance.hero_ctrl.transform.position,
endTriggerSize.Value, new Color(0.4f, 0.1f, 0.1f));
Logger.LogInfo("Set end pos");
}
}
if (!timerPaused && LoadRemover.shouldTick())
{
time += Time.unscaledDeltaTime;
// Logger.LogInfo(getTimeText(time));
timerDisplay.setTime(getTimeText(time));
}
}
public void onActiveSceneChanged(Scene from, Scene to)
{
funSceneCount += 1;
Logger.LogInfo($"Start2 aaa timermod is going a {funSceneCount}");
if (funSceneCount == 4)
{
Logger.LogInfo("Setting up timer display");
timerDisplay.setup();
SceneManager.activeSceneChanged -= onActiveSceneChanged;
}
}
private string getTimeText(double t)
{
int milis = (int)(t * 100) % 100;
int seconds = (int)(t) % 60;
int minutes = (int)(t) / 60;
return $"{minutes}:{seconds:00}.{milis:00}";
}
private void Awake()
{
config = Config;
SceneManager.activeSceneChanged += onActiveSceneChanged;
keybinds = new Keybinds(Config);
timerDisplay = new TimerDisplay();
showSpeed = Config.Bind("UI", "Show Speed", false, "");
startTriggerSize = Config.Bind("Triggers", "Start (collision) trigger size", new Vector2(0.35f, 0.35f), "");
endTriggerSize = Config.Bind("Triggers", "End (collision) trigger size", new Vector2(0.35f, 0.35f), "");
Logger.LogInfo($"Plugin {Name} ({Id}) has loaded!");
}
}