mirror of
https://github.com/NotAKidoS/NAK_CVR_Mods.git
synced 2025-09-02 14:29:25 +00:00
72 lines
No EOL
2.4 KiB
C#
72 lines
No EOL
2.4 KiB
C#
using ABI_RC.Systems.MovementSystem;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
namespace NAK.DesktopVRSwitch.VRModeTrackers;
|
|
|
|
public class MovementSystemTracker : VRModeTracker
|
|
{
|
|
private Vector3 preSwitchWorldPosition;
|
|
private Quaternion preSwitchWorldRotation;
|
|
|
|
public override void TrackerInit()
|
|
{
|
|
VRModeSwitchManager.OnPreVRModeSwitch += OnPreSwitch;
|
|
VRModeSwitchManager.OnFailVRModeSwitch += OnFailedSwitch;
|
|
VRModeSwitchManager.OnPostVRModeSwitch += OnPostSwitch;
|
|
}
|
|
|
|
public override void TrackerDestroy()
|
|
{
|
|
VRModeSwitchManager.OnPreVRModeSwitch -= OnPreSwitch;
|
|
VRModeSwitchManager.OnFailVRModeSwitch -= OnFailedSwitch;
|
|
VRModeSwitchManager.OnPostVRModeSwitch -= OnPostSwitch;
|
|
}
|
|
|
|
private void OnPreSwitch(bool intoVR)
|
|
{
|
|
|
|
DesktopVRSwitch.Logger.Msg("Storing player world position and rotation.");
|
|
|
|
preSwitchWorldPosition = MovementSystem.Instance.rotationPivot.transform.position;
|
|
preSwitchWorldPosition.y = MovementSystem.Instance.transform.position.y;
|
|
preSwitchWorldRotation = MovementSystem.Instance.rotationPivot.transform.rotation;
|
|
|
|
MovementSystem.Instance.ChangeCrouch(false);
|
|
MovementSystem.Instance.ChangeProne(false);
|
|
MovementSystem.Instance.SetImmobilized(true);
|
|
|
|
}
|
|
|
|
private void OnFailedSwitch(bool intoVR)
|
|
{
|
|
DesktopVRSwitch.Logger.Msg("Resetting MovementSystem mobility.");
|
|
|
|
MovementSystem.Instance.SetImmobilized(false);
|
|
}
|
|
|
|
private void OnPostSwitch(bool intoVR)
|
|
{
|
|
// Lazy
|
|
MelonLoader.MelonCoroutines.Start(TeleportFrameAfter(intoVR));
|
|
}
|
|
|
|
private IEnumerator TeleportFrameAfter(bool intoVR)
|
|
{
|
|
yield return null; // need to wait a frame
|
|
|
|
DesktopVRSwitch.Logger.Msg("Resetting MovementSystem mobility and applying stored position and rotation.");
|
|
|
|
MovementSystem.Instance.rotationPivot = Utils.GetPlayerCameraObject(intoVR).transform;
|
|
MovementSystem.Instance.TeleportToPosRot(preSwitchWorldPosition, preSwitchWorldRotation, false);
|
|
|
|
if (!intoVR)
|
|
MovementSystem.Instance.UpdateColliderCenter(MovementSystem.Instance.transform.position);
|
|
|
|
MovementSystem.Instance.ChangeCrouch(false);
|
|
MovementSystem.Instance.ChangeProne(false);
|
|
MovementSystem.Instance.SetImmobilized(false);
|
|
|
|
yield break;
|
|
}
|
|
} |