Skip to content

Commit

Permalink
Fix noclip no longer working correctly in AI
Browse files Browse the repository at this point in the history
Fixes #25
  • Loading branch information
ManlyMarco committed Apr 16, 2024
1 parent 691de53 commit 4009dd4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 40 deletions.
8 changes: 1 addition & 7 deletions AI_CheatTools/CheatToolsWindowInit.AI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ public static void Initialize(CheatToolsPlugin instance)
BuildOverlap.SettingChanged += (sender, args) => BuildOverlapHooks.Enabled = BuildOverlap.Value;
BuildOverlapHooks.Enabled = BuildOverlap.Value;

NoclipFeature.InitializeNoclip(instance, () =>
{
if (!Map.IsInstance()) return null;
if (Map.Instance.Player == null) return null;
if (Map.Instance.Player.Controller == null) return null;
return Map.Instance.Player.Controller.GetComponent<NavMeshAgent>();
});
NoclipFeature.InitializeNoclip(instance);

ToStringConverter.AddConverter<AgentActor>(heroine => !string.IsNullOrEmpty(heroine.CharaName) ? heroine.CharaName : heroine.name);
ToStringConverter.AddConverter<AgentData>(d => $"AgentData - {d.CharaFileName} | {d.NowCoordinateFileName}");
Expand Down
7 changes: 1 addition & 6 deletions KKS_CheatTools/CheatToolsWindowInit.KKS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,7 @@ public static class CheatToolsWindowInit

public static void Initialize(CheatToolsPlugin instance)
{
NoclipFeature.InitializeNoclip(instance, () =>
{
if (Game.Player == null) return null;
if (Game.Player.transform == null) return null;
return Game.Player.transform.GetComponent<NavMeshAgent>();
});
NoclipFeature.InitializeNoclip(instance);

CheatToolsWindow.OnShown += _ =>
{
Expand Down
10 changes: 1 addition & 9 deletions KK_CheatTools/CheatToolsWindowInit.KK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,7 @@ public static void Initialize(CheatToolsPlugin instance)
ToStringConverter.AddConverter<SaveData.Heroine>(heroine => !string.IsNullOrEmpty(heroine.Name) ? heroine.Name : heroine.nickname);
ToStringConverter.AddConverter<SaveData.CharaData.Params.Data>(d => $"[{d.key} | {d.value}]");

NoclipFeature.InitializeNoclip(instance, () =>
{
if (!Game.IsInstance()) return null;
var player = Game.Instance.Player;
if (player == null) return null;
var playerTransform = player.transform;
if (playerTransform == null) return null;
return playerTransform.GetComponent<NavMeshAgent>();
});
NoclipFeature.InitializeNoclip(instance);

CheatToolsWindow.OnShown += _ =>
{
Expand Down
70 changes: 52 additions & 18 deletions Shared_Noclip/NoclipFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@
using System.Collections;
using BepInEx.Configuration;
using KKAPI.Studio;
using UnityEngine;
using Manager;
using UnityEngine.AI;

namespace CheatTools
{
internal static class NoclipFeature
{
private static ConfigEntry<KeyboardShortcut> _noclip;
private static Func<NavMeshAgent> _getPlayerNavMeshAgent;
private static bool _noclipMode;

public static void InitializeNoclip(CheatToolsPlugin instance, Func<NavMeshAgent> getPlayerNavMeshAgent)
public static void InitializeNoclip(CheatToolsPlugin instance)
{
if (StudioAPI.InsideStudio) return;

_getPlayerNavMeshAgent = getPlayerNavMeshAgent ?? throw new ArgumentNullException(nameof(getPlayerNavMeshAgent));

_noclip = instance.Config.Bind("Hotkeys", "Toggle player noclip", KeyboardShortcut.Empty);
instance.StartCoroutine(NoclipCo());
}
Expand All @@ -34,7 +31,7 @@ private static IEnumerator NoclipCo()

if (NoclipMode)
{
var navMeshAgent = _getPlayerNavMeshAgent();
var navMeshAgent = GetPlayerNavMeshAgent();
if (navMeshAgent == null || navMeshAgent.enabled)
{
NoclipMode = false;
Expand All @@ -53,7 +50,7 @@ internal static bool NoclipMode
{
if (_noclipMode != value)
{
var navMeshAgent = _getPlayerNavMeshAgent();
var navMeshAgent = GetPlayerNavMeshAgent();
if (navMeshAgent != null)
{
navMeshAgent.enabled = !value;
Expand All @@ -66,25 +63,62 @@ internal static bool NoclipMode
}
}

private static void RunNoclip(Transform playerTransform)
private static NavMeshAgent GetPlayerNavMeshAgent()
{
#if AI
if (!Map.IsInstance()) return null;
if (Map.Instance.Player == null) return null;
if (Map.Instance.Player.Controller == null) return null;
return Map.Instance.Player.Controller.GetComponent<NavMeshAgent>();
#elif KK
if (!Game.IsInstance()) return null;
var player = Game.Instance.Player;
if (player == null) return null;
var playerTransform = player.transform;
if (playerTransform == null) return null;
return playerTransform.GetComponent<NavMeshAgent>();
#elif KKS
if (Game.Player == null) return null;
if (Game.Player.transform == null) return null;
return Game.Player.transform.GetComponent<NavMeshAgent>();
#endif
}

private static void RunNoclip(UnityEngine.Transform playerTransform)
{
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
#if AI || HS2
var x = Manager.Input.Instance.GetAxisRaw(ActionID.MoveHorizontal);
var y = Manager.Input.Instance.GetAxisRaw(ActionID.MoveVertical);
#else
var x = UnityEngine.Input.GetAxisRaw("Horizontal");
var y = UnityEngine.Input.GetAxisRaw("Vertical");
#endif
if (x != 0 || y != 0)
{
var moveSpeed = Input.GetKey(KeyCode.LeftShift) ? 0.5f : 0.05f;
playerTransform.Translate(
moveSpeed * new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")),
Camera.main.transform);
var moveSpeed = UnityEngine.Input.GetKey(UnityEngine.KeyCode.LeftShift) ?
#if AI || HS2
5f : 0.5f;
#else
0.5f : 0.05f;
#endif
playerTransform.Translate(moveSpeed * new UnityEngine.Vector3(x, 0, y), UnityEngine.Camera.main.transform);
}

if (Input.GetAxis("Mouse ScrollWheel") != 0)
var w = UnityEngine.Input.GetAxis("Mouse ScrollWheel");
if (w != 0)
{
var scrollSpeed = Input.GetKey(KeyCode.LeftShift) ? 10f : 1f;
playerTransform.position += scrollSpeed * new Vector3(0, -Input.GetAxis("Mouse ScrollWheel"), 0);
var scrollSpeed = UnityEngine.Input.GetKey(UnityEngine.KeyCode.LeftShift) ?
#if AI || HS2
100f : 10f;
#else
10f : 1f;
#endif
playerTransform.position += scrollSpeed * new UnityEngine.Vector3(0, -w, 0);
}

var eulerAngles = playerTransform.rotation.eulerAngles;
eulerAngles.y = Camera.main.transform.rotation.eulerAngles.y;
playerTransform.rotation = Quaternion.Euler(eulerAngles);
eulerAngles.y = UnityEngine.Camera.main.transform.rotation.eulerAngles.y;
playerTransform.rotation = UnityEngine.Quaternion.Euler(eulerAngles);
}
}
}

0 comments on commit 4009dd4

Please sign in to comment.