-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSpectateEnemies.cs
400 lines (370 loc) · 15.5 KB
/
SpectateEnemies.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
using BepInEx.Configuration;
using GameNetcodeStuff;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.InputSystem;
namespace SpectateEnemy
{
internal class SpectateEnemies : MonoBehaviour
{
public static SpectateEnemies Instance;
private static readonly Dictionary<string, ConfigEntry<bool>> settings = [];
private bool WindowOpen = false;
private Rect window = new(10, 10, 500, 400);
private Vector2 scrollPos = Vector2.zero;
public int SpectatedEnemyIndex = -1;
public bool SpectatingEnemies = false;
public Spectatable[] SpectatorList;
public float ZoomLevel = 1f;
public ConfigEntry<bool> HideControls;
private void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
}
SetupKeybinds();
Instance = this;
DontDestroyOnLoad(gameObject);
HideControls = Plugin.Configuration.Bind("Config", "Hide Controls", false, "Hides the controls toolip on the right hand side.");
}
private void SetupKeybinds()
{
Plugin.Inputs.SwapKey.performed += OnSwapKeyPressed;
Plugin.Inputs.MenuKey.performed += OnMenuKeyPressed;
Plugin.Inputs.FlashlightKey.performed += OnFlashlightKeyPressed;
Plugin.Inputs.ZoomOutKey.performed += OnZoomOutPressed;
Plugin.Inputs.ZoomInKey.performed += OnZoomInPressed;
}
private void OnSwapKeyPressed(InputAction.CallbackContext context)
{
if (!context.performed) return;
if (GameNetworkManager.Instance != null && GameNetworkManager.Instance.localPlayerController != null)
{
PlayerControllerB player = GameNetworkManager.Instance.localPlayerController;
if (player.IsOwner && player.isPlayerDead && !StartOfRound.Instance.shipIsLeaving && (!player.IsServer || player.isHostPlayerObject))
{
ToggleSpectatingMode(player);
}
}
}
private void OnMenuKeyPressed(InputAction.CallbackContext context)
{
if (!context.performed) return;
if (GameNetworkManager.Instance != null && GameNetworkManager.Instance.localPlayerController != null)
{
PlayerControllerB player = GameNetworkManager.Instance.localPlayerController;
if (player.IsOwner && player.isPlayerDead && !StartOfRound.Instance.shipIsLeaving && (!player.IsServer || player.isHostPlayerObject))
{
Toggle();
}
}
}
private void OnFlashlightKeyPressed(InputAction.CallbackContext context)
{
if (!context.performed) return;
if (GameNetworkManager.Instance != null && GameNetworkManager.Instance.localPlayerController != null)
{
PlayerControllerB player = GameNetworkManager.Instance.localPlayerController;
if (HUDManager.Instance != null && player.IsOwner && player.isPlayerDead && !StartOfRound.Instance.shipIsLeaving && (!player.IsServer || player.isHostPlayerObject))
{
// flashlight already exists on spectator camera, thanks zeekerss
Light light = HUDManager.Instance.playersManager.spectateCamera.GetComponent<Light>();
if (light != null)
{
light.enabled = !light.enabled;
}
}
}
}
private void OnZoomOutPressed(InputAction.CallbackContext context)
{
if (!context.performed) return;
if (!SpectatingEnemies) return;
ZoomLevel += 0.1f;
if (ZoomLevel > 10f)
ZoomLevel = 10f;
}
private void OnZoomInPressed(InputAction.CallbackContext context)
{
if (!context.performed) return;
if (!SpectatingEnemies) return;
ZoomLevel -= 0.1f;
if (ZoomLevel < 1f)
ZoomLevel = 1f;
}
private void LateUpdate()
{
if (SpectatingEnemies)
{
if (SpectatorList.Length == 0)
{
SpectatingEnemies = false;
return;
}
Spectatable currentEnemy = SpectatorList.ElementAtOrDefault(SpectatedEnemyIndex);
if (currentEnemy == null)
{
GetNextValidSpectatable();
return;
}
if (currentEnemy.type == SpectatableType.Enemy || currentEnemy.type == SpectatableType.Masked || currentEnemy.type == SpectatableType.GhostGirl)
{
if (currentEnemy.enemyInstance != null && currentEnemy.enemyInstance.isEnemyDead)
{
GetNextValidSpectatable();
return;
}
}
Vector3? position = GetSpectatePosition(currentEnemy);
if (!position.HasValue)
{
GetNextValidSpectatable();
return;
}
if (currentEnemy.enemyName == "Enemy")
{
TryFixName(ref currentEnemy);
}
GameNetworkManager.Instance.localPlayerController.spectateCameraPivot.position = position.Value;
if (currentEnemy.type == SpectatableType.Masked && currentEnemy.maskedName != string.Empty)
HUDManager.Instance.spectatingPlayerText.text = string.Format("(Spectating: {0}) [{1:F1}x]", currentEnemy.maskedName, ZoomLevel);
else if (currentEnemy.type == SpectatableType.GhostGirl)
{
// TODO: improve
DressGirlAI ghost = currentEnemy.gameObject.GetComponent<DressGirlAI>();
ghost.EnableEnemyMesh(true, true);
PlayerControllerB target = ghost.targetPlayer;
if (target != null)
{
HUDManager.Instance.spectatingPlayerText.text = string.Format("(Spectating: {0}) [{1:F1}x]\n(Targeting: {2})", currentEnemy.maskedName, ZoomLevel, target.playerUsername);
}
else
{
HUDManager.Instance.spectatingPlayerText.text = string.Format("(Spectating: {0}) [{1:F1}x]\n(No Target)", currentEnemy.maskedName, ZoomLevel);
}
}
else
HUDManager.Instance.spectatingPlayerText.text = string.Format("(Spectating: {0}) [{1:F1}x]", currentEnemy.enemyName, ZoomLevel);
Plugin.raycastSpectate.Invoke(GameNetworkManager.Instance.localPlayerController, []);
// Thanks HalfyRed!
GameNetworkManager.Instance.localPlayerController.spectateCameraPivot.GetComponentInChildren<Camera>().transform.localPosition = Vector3.back * (ZoomLevel + 0.5f);
}
}
private Vector3? GetSpectatePosition(Spectatable obj)
{
if (obj.type == SpectatableType.Enemy || obj.type == SpectatableType.Masked || obj.type == SpectatableType.GhostGirl)
{
EnemyAI enemy = obj.enemyInstance;
if (enemy != null)
{
return enemy.eye == null ? enemy.transform.position : enemy.eye.position;
}
}
else if (obj.type == SpectatableType.Turret)
{
Turret enemy = obj.GetComponent<Turret>();
if (enemy != null)
{
return enemy.centerPoint.transform.position;
}
}
else if (obj.type == SpectatableType.Landmine)
{
return obj.transform.position;
}
else
{
Debug.LogError("[SpectateEnemy]: Error when spectating: no handler for SpectatableType " + obj.type);
}
return null;
}
private void TryFixName(ref Spectatable obj)
{
if (obj.gameObject.TryGetComponent(out MaskedPlayerEnemy masked))
{
if (masked.mimickingPlayer != null)
{
obj.enemyName = masked.mimickingPlayer.playerUsername;
}
else
{
obj.enemyName = masked.enemyType.enemyName;
}
}
else if (obj.gameObject.TryGetComponent(out EnemyAI enemy))
{
obj.enemyName = enemy.enemyType.enemyName;
}
else if (obj.gameObject.TryGetComponent<Turret>(out _))
{
obj.enemyName = "Turret";
}
else if (obj.gameObject.TryGetComponent<Landmine>(out _))
{
obj.enemyName = "Landmine";
}
}
public void ToggleSpectatingMode(PlayerControllerB __instance)
{
SpectatingEnemies = !SpectatingEnemies;
if (SpectatingEnemies)
{
SpectatorList = FindObjectsByType<Spectatable>(FindObjectsSortMode.None).Where(x => GetSetting(SanitizeEnemyName(x.enemyName))).ToArray();
if (SpectatorList.Length == 0)
{
SpectatingEnemies = false;
Plugin.displaySpectatorTip.Invoke(HUDManager.Instance, ["No enemies to spectate"]);
return;
}
if (SpectatedEnemyIndex == -1 || SpectatedEnemyIndex >= SpectatorList.Length)
{
if (__instance.spectatedPlayerScript == null)
{
GetNextValidSpectatable();
}
else
{
float closest = 999999f;
int index = 0;
for (int i = 0; i < SpectatorList.Length; i++)
{
float dist = (SpectatorList[i].transform.position - __instance.spectatedPlayerScript.transform.position).sqrMagnitude;
if (dist < closest * closest)
{
closest = dist;
index = i;
}
}
SpectatedEnemyIndex = index;
}
}
__instance.spectatedPlayerScript = null;
}
else
{
__instance.spectatedPlayerScript = __instance.playersManager.allPlayerScripts.FirstOrDefault(x => !x.isPlayerDead && x.isPlayerControlled);
HUDManager.Instance.spectatingPlayerText.text = $"(Spectating: {__instance.spectatedPlayerScript.playerUsername})";
}
}
public void PopulateSettings()
{
EnemyType[] allEnemies = Resources.FindObjectsOfTypeAll<EnemyType>();
foreach (EnemyType type in allEnemies)
{
if (type.enemyName == "Red pill" || type.enemyName == "Lasso")
continue;
string name = SanitizeEnemyName(type.enemyName);
settings.TryAdd(name, Plugin.Configuration.Bind("Enemies", name, !type.isDaytimeEnemy, "Enables spectating " + name));
}
settings.TryAdd("Landmine", Plugin.Configuration.Bind("Enemies", "Landmine", false, "Enables spectating Landmines"));
settings.TryAdd("Turret", Plugin.Configuration.Bind("Enemies", "Turret", false, "Enables spectating Turrets"));
Debug.LogWarning("[SpectateEnemies]: Config loaded");
AssertSettings();
}
public bool SpectateNextEnemy()
{
if (SpectatorList.Length == 0)
{
SpectatingEnemies = false;
return true;
}
GetNextValidSpectatable();
return false;
}
private string SanitizeEnemyName(string enemyName)
{
return enemyName.Replace("\\", "").Replace("\"", "").Replace("'", "").Replace("[", "").Replace("]", "");
}
private void GetNextValidSpectatable()
{
SpectatorList = FindObjectsByType<Spectatable>(FindObjectsSortMode.None).Where(x => GetSetting(SanitizeEnemyName(x.enemyName))).ToArray();
int enemiesChecked = 0;
int current = SpectatedEnemyIndex;
while (enemiesChecked < SpectatorList.Length)
{
current++;
if (current >= SpectatorList.Length)
{
current = 0;
}
Spectatable enemy = SpectatorList.ElementAtOrDefault(current);
if (enemy != null)
{
if (enemy.type == SpectatableType.Enemy || enemy.type == SpectatableType.Masked || enemy.type == SpectatableType.GhostGirl)
{
if (enemy.enemyInstance != null && enemy.enemyInstance.isEnemyDead)
{
enemiesChecked++;
continue;
}
}
if (settings.ContainsKey(SanitizeEnemyName(enemy.enemyName)))
{
SpectatedEnemyIndex = current;
return;
}
}
enemiesChecked++;
}
SpectatingEnemies = false;
Plugin.displaySpectatorTip.Invoke(HUDManager.Instance, ["No enemies to spectate"]);
}
private void AssertSettings()
{
foreach (var t in settings)
{
Debug.LogWarning($"{t.Key} : {t.Value.Value}");
}
}
public bool GetSetting(string name)
{
if (settings.ContainsKey(name))
return settings[name].Value;
return false;
}
public void Toggle()
{
WindowOpen = !WindowOpen;
}
public void Hide()
{
WindowOpen = false;
}
public bool IsMenuOpen()
{
return WindowOpen;
}
private void OnGUI()
{
if (WindowOpen)
{
GUI.color = Color.gray;
window = GUI.Window(0, window, DrawGUI, "Spectator Settings");
}
}
private void DrawGUI(int windowID)
{
if (windowID == 0)
{
scrollPos = GUI.BeginScrollView(new Rect(5, 20, 490, 370), scrollPos, new Rect(5, 20, 490, 370), false, true);
GUI.Label(new Rect(10, 30, 500, 100), "Tip: Checking the box next to an enemy name will enable spectating it.");
int x = 0;
int y = 0;
foreach (string k in settings.Keys.ToList())
{
settings[k].Value = GUI.Toggle(new Rect(10 + (150 * x), 60 + (30 * y), 150, 20), settings[k].Value, k);
x++;
if (x == 3)
{
x = 0;
y++;
}
}
GUI.EndScrollView(true);
}
GUI.DragWindow(new Rect(0, 0, 10000, 10000));
}
}
}