Reset button, animation cancel, world check, VR offset fix

This commit is contained in:
SDraw 2023-04-08 17:12:54 +03:00
parent 8ffc876376
commit 8bee0d2adf
No known key found for this signature in database
GPG key ID: BB95B4DAB2BB8BB5
5 changed files with 128 additions and 18 deletions

View file

@ -1,4 +1,5 @@
using ABI_RC.Core.InteractionSystem; using ABI_RC.Core;
using ABI_RC.Core.InteractionSystem;
using ABI_RC.Core.Player; using ABI_RC.Core.Player;
using ABI_RC.Systems.IK.SubSystems; using ABI_RC.Systems.IK.SubSystems;
using System; using System;
@ -39,6 +40,11 @@ namespace ml_prm
new HarmonyLib.HarmonyMethod(typeof(PlayerRagdollMod).GetMethod(nameof(OnStartCalibration_Prefix), BindingFlags.Static | BindingFlags.NonPublic)), new HarmonyLib.HarmonyMethod(typeof(PlayerRagdollMod).GetMethod(nameof(OnStartCalibration_Prefix), BindingFlags.Static | BindingFlags.NonPublic)),
null null
); );
HarmonyInstance.Patch(
typeof(RootLogic).GetMethod(nameof(RootLogic.SpawnOnWorldInstance)),
new HarmonyLib.HarmonyMethod(typeof(PlayerRagdollMod).GetMethod(nameof(OnWorldSpawn_Prefix), BindingFlags.Static | BindingFlags.NonPublic)),
null
);
MelonLoader.MelonCoroutines.Start(WaitForLocalPlayer()); MelonLoader.MelonCoroutines.Start(WaitForLocalPlayer());
} }
@ -115,5 +121,19 @@ namespace ml_prm
} }
} }
static void OnWorldSpawn_Prefix() => ms_instance?.OnWorldSpawn();
void OnWorldSpawn()
{
try
{
if(m_localController != null)
m_localController.OnWorldSpawn();
}
catch(Exception e)
{
MelonLoader.MelonLogger.Error(e);
}
}
} }
} }

View file

@ -17,8 +17,9 @@ Optional mod's settings with [BTKUILib](https://github.com/BTK-Development/BTKUI
* **Velocity multiplier:** velocity force multiplier based on player's movement direction; `2.0` by default. * **Velocity multiplier:** velocity force multiplier based on player's movement direction; `2.0` by default.
* **Movement drag:** movement resistance; `1.0` by default. * **Movement drag:** movement resistance; `1.0` by default.
* **Angular movement drag:** angular movement resistance; `0.5` by default. * **Angular movement drag:** angular movement resistance; `0.5` by default.
* **Reset settings:** resets mod settings to default.
# Notes # Notes
* Incompatible with `Follow hips on IK override` option in AvatarMotionTweaker. * Incompatible with `Follow hips on IK override` option in AvatarMotionTweaker.
* Even if locally ragdoll state is activated in the middle of playing emote, remote players still see whole emote animation.
* Not suggested to activate fly mode with enabled ragdoll state. * Not suggested to activate fly mode with enabled ragdoll state.
* Can't be activated in worlds that don't allow flying and spawnables.

View file

@ -11,6 +11,8 @@ namespace ml_prm
{ {
class RagdollController : MonoBehaviour class RagdollController : MonoBehaviour
{ {
static readonly Vector4 ms_pointVector = new Vector4(0f, 0f, 0f, 1f);
VRIK m_vrIK = null; VRIK m_vrIK = null;
float m_vrIkWeight = 1f; float m_vrIkWeight = 1f;
@ -196,6 +198,12 @@ namespace ml_prm
SwitchRagdoll(); SwitchRagdoll();
} }
internal void OnWorldSpawn()
{
if(m_enabled && m_avatarReady)
SwitchRagdoll(true);
}
// IK updates // IK updates
void OnIKPreUpdate() void OnIKPreUpdate()
{ {
@ -238,13 +246,15 @@ namespace ml_prm
} }
// Arbitrary // Arbitrary
public void SwitchRagdoll() void SwitchRagdoll() => SwitchRagdoll(false);
public void SwitchRagdoll(bool p_force = false)
{ {
if(m_avatarReady && (MovementSystem.Instance.lastSeat == null) && !BodySystem.isCalibrating) if(m_avatarReady && (MovementSystem.Instance.lastSeat == null) && !BodySystem.isCalibrating && (Utils.IsWorldSafe() || p_force))
{ {
m_enabled = !m_enabled; m_enabled = !m_enabled;
MovementSystem.Instance.SetImmobilized(m_enabled); MovementSystem.Instance.SetImmobilized(m_enabled);
PlayerSetup.Instance.animatorManager.SetAnimatorParameterTrigger("CancelEmote");
if(m_enabled) if(m_enabled)
{ {
@ -268,8 +278,21 @@ namespace ml_prm
if(!Settings.RestorePosition && (m_puppetReferences.hips != null)) if(!Settings.RestorePosition && (m_puppetReferences.hips != null))
{ {
Vector3 l_pos = m_puppetReferences.hips.position; if(Utils.IsInVR())
PlayerSetup.Instance.transform.position = l_pos; {
Matrix4x4 l_playerMatrix = PlayerSetup.Instance.transform.GetMatrix();
Matrix4x4 l_avatarMatrix = PlayerSetup.Instance._avatar.transform.GetMatrix();
Matrix4x4 l_avatarToPlayer = l_avatarMatrix.inverse * l_playerMatrix;
Vector3 l_pos = m_puppetReferences.hips.position;
Vector3 l_offset = l_avatarToPlayer * ms_pointVector;
PlayerSetup.Instance.transform.position = (l_pos + l_offset);
}
else
{
Vector3 l_pos = m_puppetReferences.hips.position;
PlayerSetup.Instance.transform.position = l_pos;
}
} }
} }

View file

@ -16,6 +16,16 @@ namespace ml_prm
Gravity Gravity
} }
enum UiElementIndex
{
Hotkey = 0,
RestorePosition,
Gravity,
VelocityMultiplier,
MovementDrag,
AngularDrag
}
public static bool Hotkey { get; private set; } = true; public static bool Hotkey { get; private set; } = true;
public static float VelocityMultiplier { get; private set; } = 2f; public static float VelocityMultiplier { get; private set; } = 2f;
public static bool RestorePosition { get; private set; } = false; public static bool RestorePosition { get; private set; } = false;
@ -34,6 +44,8 @@ namespace ml_prm
static MelonLoader.MelonPreferences_Category ms_category = null; static MelonLoader.MelonPreferences_Category ms_category = null;
static List<MelonLoader.MelonPreferences_Entry> ms_entries = null; static List<MelonLoader.MelonPreferences_Entry> ms_entries = null;
static List<object> ms_uiElements = new List<object>();
internal static void Init() internal static void Init()
{ {
ms_category = MelonLoader.MelonPreferences.CreateCategory("PRM"); ms_category = MelonLoader.MelonPreferences.CreateCategory("PRM");
@ -71,42 +83,87 @@ namespace ml_prm
{ {
SwitchChange?.Invoke(); SwitchChange?.Invoke();
}; };
l_categoryMod.AddToggle("Use hotkey", "Switch ragdoll mode with 'R' key", Hotkey).OnValueUpdated += (state) =>
ms_uiElements.Add(l_categoryMod.AddToggle("Use hotkey", "Switch ragdoll mode with 'R' key", Hotkey));
(ms_uiElements[(int)UiElementIndex.Hotkey] as BTKUILib.UIObjects.Components.ToggleButton).OnValueUpdated += (state) =>
{ {
Hotkey = state; Hotkey = state;
ms_entries[(int)ModSetting.Hotkey].BoxedValue = state; ms_entries[(int)ModSetting.Hotkey].BoxedValue = state;
HotkeyChange?.Invoke(Hotkey); HotkeyChange?.Invoke(state);
}; };
l_categoryMod.AddToggle("Restore position", "Bring avatar back where ragdoll state was activated", RestorePosition).OnValueUpdated += (state) =>
ms_uiElements.Add(l_categoryMod.AddToggle("Restore position", "Bring avatar back where ragdoll state was activated", RestorePosition));
(ms_uiElements[(int)UiElementIndex.RestorePosition] as BTKUILib.UIObjects.Components.ToggleButton).OnValueUpdated += (state) =>
{ {
RestorePosition = state; RestorePosition = state;
ms_entries[(int)ModSetting.RestorePosition].BoxedValue = state; ms_entries[(int)ModSetting.RestorePosition].BoxedValue = state;
RestorePositionChange?.Invoke(RestorePosition); RestorePositionChange?.Invoke(state);
}; };
l_categoryMod.AddToggle("Use gravity", "Apply gravity to ragdoll", Gravity).OnValueUpdated += (state) =>
ms_uiElements.Add(l_categoryMod.AddToggle("Use gravity", "Apply gravity to ragdoll", Gravity));
(ms_uiElements[(int)UiElementIndex.Gravity] as BTKUILib.UIObjects.Components.ToggleButton).OnValueUpdated += (state) =>
{ {
Gravity = state; Gravity = state;
ms_entries[(int)ModSetting.Gravity].BoxedValue = state; ms_entries[(int)ModSetting.Gravity].BoxedValue = state;
GravityChange?.Invoke(Gravity); GravityChange?.Invoke(state);
}; };
l_page.AddSlider("Velocity multiplier", "Velocity multiplier upon entering ragdoll state", VelocityMultiplier, 1f, 50f).OnValueUpdated += (value) =>
ms_uiElements.Add(l_page.AddSlider("Velocity multiplier", "Velocity multiplier upon entering ragdoll state", VelocityMultiplier, 1f, 50f));
(ms_uiElements[(int)UiElementIndex.VelocityMultiplier] as BTKUILib.UIObjects.Components.SliderFloat).OnValueUpdated += (value) =>
{ {
VelocityMultiplier = value; VelocityMultiplier = value;
ms_entries[(int)ModSetting.VelocityMultiplier].BoxedValue = value; ms_entries[(int)ModSetting.VelocityMultiplier].BoxedValue = value;
VelocityMultiplierChange?.Invoke(VelocityMultiplier); VelocityMultiplierChange?.Invoke(value);
}; };
l_page.AddSlider("Movement drag", "Movement resistance", MovementDrag, 0f, 100f).OnValueUpdated += (value) =>
ms_uiElements.Add(l_page.AddSlider("Movement drag", "Movement resistance", MovementDrag, 0f, 100f));
(ms_uiElements[(int)UiElementIndex.MovementDrag] as BTKUILib.UIObjects.Components.SliderFloat).OnValueUpdated += (value) =>
{ {
MovementDrag = value; MovementDrag = value;
ms_entries[(int)ModSetting.MovementDrag].BoxedValue = value; ms_entries[(int)ModSetting.MovementDrag].BoxedValue = value;
MovementDragChange?.Invoke(MovementDrag); MovementDragChange?.Invoke(value);
}; };
l_page.AddSlider("Angular movement drag", "Rotation movement resistance", AngularDrag, 0.5f, 50f).OnValueUpdated += (value) =>
ms_uiElements.Add(l_page.AddSlider("Angular movement drag", "Rotation movement resistance", AngularDrag, 0.5f, 50f));
(ms_uiElements[(int)UiElementIndex.AngularDrag] as BTKUILib.UIObjects.Components.SliderFloat).OnValueUpdated += (value) =>
{ {
AngularDrag = value; AngularDrag = value;
ms_entries[(int)ModSetting.AngularDrag].BoxedValue = value; ms_entries[(int)ModSetting.AngularDrag].BoxedValue = value;
AngularDragChange?.Invoke(AngularDrag); AngularDragChange?.Invoke(AngularDrag);
}; };
l_categoryMod.AddButton("Reset settings", "", "Reset mod settings to default").OnPress += () =>
{
Hotkey = true;
(ms_uiElements[(int)UiElementIndex.Hotkey] as BTKUILib.UIObjects.Components.ToggleButton).ToggleValue = true;
ms_entries[(int)ModSetting.Hotkey].BoxedValue = true;
HotkeyChange?.Invoke(true);
RestorePosition = false;
ms_entries[(int)ModSetting.RestorePosition].BoxedValue = false;
(ms_uiElements[(int)UiElementIndex.RestorePosition] as BTKUILib.UIObjects.Components.ToggleButton).ToggleValue = false;
RestorePositionChange?.Invoke(false);
Gravity = true;
ms_entries[(int)ModSetting.Gravity].BoxedValue = true;
(ms_uiElements[(int)UiElementIndex.Gravity] as BTKUILib.UIObjects.Components.ToggleButton).ToggleValue = true;
GravityChange?.Invoke(true);
VelocityMultiplier = 2f;
ms_entries[(int)ModSetting.VelocityMultiplier].BoxedValue = 2f;
(ms_uiElements[(int)UiElementIndex.VelocityMultiplier] as BTKUILib.UIObjects.Components.SliderFloat).SetSliderValue(2f);
VelocityMultiplierChange?.Invoke(2f);
MovementDrag = 1f;
ms_entries[(int)ModSetting.MovementDrag].BoxedValue = 1f;
(ms_uiElements[(int)UiElementIndex.MovementDrag] as BTKUILib.UIObjects.Components.SliderFloat).SetSliderValue(1f);
MovementDragChange?.Invoke(1f);
AngularDrag = 0.5f;
ms_entries[(int)ModSetting.MovementDrag].BoxedValue = 0.5f;
(ms_uiElements[(int)UiElementIndex.AngularDrag] as BTKUILib.UIObjects.Components.SliderFloat).SetSliderValue(0.5f);
AngularDragChange?.Invoke(0.5f);
};
} }
} }
} }

View file

@ -1,13 +1,22 @@
using UnityEngine; using ABI.CCK.Components;
using UnityEngine;
namespace ml_prm namespace ml_prm
{ {
static class Utils static class Utils
{ {
public static bool IsInVR() => ((ABI_RC.Core.Savior.CheckVR.Instance != null) && ABI_RC.Core.Savior.CheckVR.Instance.hasVrDeviceLoaded);
public static bool IsWorldSafe() => ((CVRWorld.Instance != null) && CVRWorld.Instance.allowFlying && CVRWorld.Instance.allowSpawnables);
public static void CopyGlobal(this Transform p_source, Transform p_target) public static void CopyGlobal(this Transform p_source, Transform p_target)
{ {
p_target.position = p_source.position; p_target.position = p_source.position;
p_target.rotation = p_source.rotation; p_target.rotation = p_source.rotation;
} }
public static Matrix4x4 GetMatrix(this Transform p_transform, bool p_pos = true, bool p_rot = true, bool p_scl = false)
{
return Matrix4x4.TRS(p_pos ? p_transform.position : Vector3.zero, p_rot ? p_transform.rotation : Quaternion.identity, p_scl ? p_transform.localScale : Vector3.one);
}
} }
} }