11using BepInEx ;
2+ using BepInEx . Configuration ;
23using GameNetcodeStuff ;
34using System ;
45using System . Collections . Generic ;
@@ -14,15 +15,16 @@ internal class SpectateEnemies : MonoBehaviour
1415 {
1516 public static SpectateEnemies Instance ;
1617
17- private static readonly Dictionary < string , bool > settings = [ ] ;
18+ private static readonly Dictionary < string , ConfigEntry < bool > > settings = [ ] ;
1819
1920 private bool WindowOpen = false ;
2021 private Rect window = new ( 10 , 10 , 500 , 400 ) ;
2122
2223 public int SpectatedEnemyIndex = - 1 ;
2324 public bool SpectatingEnemies = false ;
2425 public Spectatable [ ] SpectatorList ;
25- public float zoomLevel = 1f ;
26+ public float ZoomLevel = 1f ;
27+ public ConfigEntry < bool > HideControls ;
2628
2729 private void Awake ( )
2830 {
@@ -33,6 +35,8 @@ private void Awake()
3335 SetupKeybinds ( ) ;
3436 Instance = this ;
3537 DontDestroyOnLoad ( gameObject ) ;
38+
39+ HideControls = Plugin . Configuration . Bind ( "Config" , "Hide Controls" , false , "Hides the controls toolip on the right hand side." ) ;
3640 }
3741
3842 private void SetupKeybinds ( )
@@ -92,18 +96,18 @@ private void OnZoomOutPressed(InputAction.CallbackContext context)
9296 {
9397 if ( ! context . performed ) return ;
9498 if ( ! SpectatingEnemies ) return ;
95- zoomLevel += 0.1f ;
96- if ( zoomLevel > 10f )
97- zoomLevel = 10f ;
99+ ZoomLevel += 0.1f ;
100+ if ( ZoomLevel > 10f )
101+ ZoomLevel = 10f ;
98102 }
99103
100104 private void OnZoomInPressed ( InputAction . CallbackContext context )
101105 {
102106 if ( ! context . performed ) return ;
103107 if ( ! SpectatingEnemies ) return ;
104- zoomLevel -= 0.1f ;
105- if ( zoomLevel < 1f )
106- zoomLevel = 1f ;
108+ ZoomLevel -= 0.1f ;
109+ if ( ZoomLevel < 1f )
110+ ZoomLevel = 1f ;
107111 }
108112
109113 private void LateUpdate ( )
@@ -139,12 +143,15 @@ private void LateUpdate()
139143 {
140144 TryFixName ( ref currentEnemy ) ;
141145 }
142- GameNetworkManager . Instance . localPlayerController . spectateCameraPivot . position = position . Value + Vector3 . up * zoomLevel ;
146+
147+ GameNetworkManager . Instance . localPlayerController . spectateCameraPivot . position = position . Value ;
143148 if ( currentEnemy . type == SpectatableType . Masked && currentEnemy . maskedName != string . Empty )
144- HUDManager . Instance . spectatingPlayerText . text = string . Format ( "(Spectating: {0}) [{1:F1}x]" , currentEnemy . maskedName , zoomLevel ) ;
149+ HUDManager . Instance . spectatingPlayerText . text = string . Format ( "(Spectating: {0}) [{1:F1}x]" , currentEnemy . maskedName , ZoomLevel ) ;
145150 else
146- HUDManager . Instance . spectatingPlayerText . text = string . Format ( "(Spectating: {0}) [{1:F1}x]" , currentEnemy . enemyName , zoomLevel ) ;
151+ HUDManager . Instance . spectatingPlayerText . text = string . Format ( "(Spectating: {0}) [{1:F1}x]" , currentEnemy . enemyName , ZoomLevel ) ;
147152 Plugin . raycastSpectate . Invoke ( GameNetworkManager . Instance . localPlayerController , [ ] ) ;
153+ // Thanks HalfyRed!
154+ GameNetworkManager . Instance . localPlayerController . spectateCameraPivot . GetComponentInChildren < Camera > ( ) . transform . localPosition = Vector3 . back * ( ZoomLevel + 0.5f ) ;
148155 }
149156 }
150157
@@ -209,7 +216,7 @@ public void ToggleSpectatingMode(PlayerControllerB __instance)
209216 SpectatingEnemies = ! SpectatingEnemies ;
210217 if ( SpectatingEnemies )
211218 {
212- SpectatorList = FindObjectsByType < Spectatable > ( FindObjectsSortMode . None ) . Where ( x => settings [ x . enemyName ] ) . ToArray ( ) ;
219+ SpectatorList = FindObjectsByType < Spectatable > ( FindObjectsSortMode . None ) . Where ( x => GetSetting ( x . enemyName ) ) . ToArray ( ) ;
213220 if ( SpectatorList . Length == 0 )
214221 {
215222 SpectatingEnemies = false ;
@@ -254,12 +261,13 @@ public void PopulateSettings()
254261 {
255262 if ( type . enemyName == "Red pill" || type . enemyName == "Lasso" )
256263 continue ;
257- settings . TryAdd ( type . enemyName , ! type . isDaytimeEnemy ) ;
264+ settings . TryAdd ( type . enemyName , Plugin . Configuration . Bind ( "Enemies" , type . enemyName , ! type . isDaytimeEnemy , "Enables spectating " + type . enemyName ) ) ;
258265 }
259266
260- settings . TryAdd ( "Landmine" , false ) ;
261- settings . TryAdd ( "Turret" , false ) ;
267+ settings . TryAdd ( "Landmine" , Plugin . Configuration . Bind ( "Enemies" , "Landmine" , false , "Enables spectating Landmines" ) ) ;
268+ settings . TryAdd ( "Turret" , Plugin . Configuration . Bind ( "Enemies" , "Turret" , false , "Enables spectating Turrets" ) ) ;
262269
270+ /*
263271 // TODO: this is terrible, improve
264272 try
265273 {
@@ -290,22 +298,24 @@ public void PopulateSettings()
290298 catch (Exception)
291299 {
292300 Debug.LogWarning("[SpectateEnemies]: Config failed to load, using default values!");
293- }
301+ } */
294302
303+ Debug . LogWarning ( "[SpectateEnemies]: Config loaded" ) ;
295304 AssertSettings ( ) ;
296305 }
297306
298307 private void OnApplicationQuit ( )
299308 {
300- StringBuilder sb = new ( ) ;
309+ /* StringBuilder sb = new();
301310 foreach (var s in settings)
302311 {
303312 sb.Append(s.Key);
304313 sb.Append(':');
305314 sb.Append(s.Value);
306315 sb.AppendLine();
307316 }
308- File . WriteAllText ( Paths . ConfigPath + "/SpectateEnemy.cfg" , sb . ToString ( ) ) ;
317+ File.WriteAllText(Paths.ConfigPath + "/SpectateEnemy.cfg", sb.ToString());*/
318+ Plugin . Configuration . Save ( ) ;
309319 Debug . LogWarning ( "[SpectateEnemies]: Config saved" ) ;
310320 }
311321
@@ -322,7 +332,7 @@ public bool SpectateNextEnemy()
322332
323333 private void GetNextValidSpectatable ( )
324334 {
325- SpectatorList = FindObjectsByType < Spectatable > ( FindObjectsSortMode . None ) . Where ( x => settings [ x . enemyName ] ) . ToArray ( ) ;
335+ SpectatorList = FindObjectsByType < Spectatable > ( FindObjectsSortMode . None ) . Where ( x => GetSetting ( x . enemyName ) ) . ToArray ( ) ;
326336 int enemiesChecked = 0 ;
327337 int current = SpectatedEnemyIndex ;
328338 while ( enemiesChecked < SpectatorList . Length )
@@ -366,7 +376,7 @@ private void AssertSettings()
366376 public bool GetSetting ( string name )
367377 {
368378 if ( settings . ContainsKey ( name ) )
369- return settings [ name ] ;
379+ return settings [ name ] . Value ;
370380 return false ;
371381 }
372382
@@ -403,7 +413,7 @@ private void DrawGUI(int windowID)
403413 int y = 0 ;
404414 foreach ( string k in settings . Keys . ToList ( ) )
405415 {
406- settings [ k ] = GUI . Toggle ( new Rect ( 10 + ( 150 * x ) , 60 + ( 30 * y ) , 150 , 20 ) , settings [ k ] , k ) ;
416+ settings [ k ] . Value = GUI . Toggle ( new Rect ( 10 + ( 150 * x ) , 60 + ( 30 * y ) , 150 , 20 ) , settings [ k ] . Value , k ) ;
407417 x ++ ;
408418 if ( x == 3 )
409419 {
0 commit comments