Skip to content

Commit

Permalink
Reaction to CVRPointer component with ragdoll type
Browse files Browse the repository at this point in the history
  • Loading branch information
SDraw committed Apr 10, 2023
1 parent dfa7da5 commit faac28f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
12 changes: 12 additions & 0 deletions ml_prm/RagdollController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class RagdollController : MonoBehaviour
Vector3 m_velocity = Vector3.zero;

RagdollToggle m_avatarRagdollToggle = null;
RagdollTrigger m_customTrigger = null;

internal RagdollController()
{
Expand All @@ -55,6 +56,8 @@ void Start()
m_puppetRoot.localPosition = Vector3.zero;
m_puppetRoot.localRotation = Quaternion.identity;

m_customTrigger = MovementSystem.Instance.proxyCollider.gameObject.AddComponent<RagdollTrigger>();

Settings.SwitchChange += this.SwitchRagdoll;
Settings.MovementDragChange += this.OnMovementDragChange;
Settings.AngularDragChange += this.OnAngularDragChange;
Expand All @@ -63,6 +66,12 @@ void Start()

void OnDestroy()
{
if(m_customTrigger != null)
{
Object.Destroy(m_customTrigger);
m_customTrigger = null;
}

Settings.SwitchChange -= this.SwitchRagdoll;
Settings.MovementDragChange -= this.OnMovementDragChange;
Settings.AngularDragChange -= this.OnAngularDragChange;
Expand All @@ -83,6 +92,9 @@ void Update()

if(m_enabled && m_avatarReady && BodySystem.isCalibratedAsFullBody)
BodySystem.TrackingPositionWeight = 0f;

if(!m_enabled && m_avatarReady && (m_customTrigger != null) && m_customTrigger.GetStateWithReset() && Settings.PointersReaction)
SwitchRagdoll();
}

void LateUpdate()
Expand Down
42 changes: 42 additions & 0 deletions ml_prm/RagdollTrigger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using ABI.CCK.Components;
using UnityEngine;

namespace ml_prm
{
[DisallowMultipleComponent]
class RagdollTrigger : MonoBehaviour
{
static int ms_localPlayerLayer = 0;

Collider m_lastCollider = null;
bool m_triggered = false;

void Start()
{
ms_localPlayerLayer = LayerMask.NameToLayer("PlayerLocal");
}

void OnTriggerEnter(Collider p_other)
{
CVRPointer l_pointer = p_other.gameObject.GetComponent<CVRPointer>();
if((l_pointer != null) && l_pointer.type == "ragdoll" && p_other.gameObject.layer == ms_localPlayerLayer && (m_lastCollider != p_other))
{
m_lastCollider = p_other;
m_triggered = true;
}
}

void OnTriggerExit(Collider p_other)
{
if(m_lastCollider == p_other)
m_lastCollider = null;
}

public bool GetStateWithReset()
{
bool l_state = m_triggered;
m_triggered = false;
return l_state;
}
}
}
21 changes: 20 additions & 1 deletion ml_prm/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ enum ModSetting
RestorePosition,
MovementDrag,
AngularDrag,
Gravity
Gravity,
PointersReaction
}

enum UiElementIndex
{
Hotkey = 0,
RestorePosition,
Gravity,
PointersReaction,
VelocityMultiplier,
MovementDrag,
AngularDrag
Expand All @@ -32,6 +34,7 @@ enum UiElementIndex
public static float MovementDrag { get; private set; } = 2f;
public static float AngularDrag { get; private set; } = 2f;
public static bool Gravity { get; private set; } = true;
public static bool PointersReaction { get; private set; } = true;

static public event Action SwitchChange;
static public event Action<bool> HotkeyChange;
Expand All @@ -40,6 +43,7 @@ enum UiElementIndex
static public event Action<float> MovementDragChange;
static public event Action<float> AngularDragChange;
static public event Action<bool> GravityChange;
static public event Action<bool> PointersReactionChange;

static MelonLoader.MelonPreferences_Category ms_category = null;
static List<MelonLoader.MelonPreferences_Entry> ms_entries = null;
Expand All @@ -57,6 +61,7 @@ internal static void Init()
ms_category.CreateEntry(ModSetting.MovementDrag.ToString(), MovementDrag),
ms_category.CreateEntry(ModSetting.AngularDrag.ToString(), AngularDrag),
ms_category.CreateEntry(ModSetting.Gravity.ToString(), Gravity),
ms_category.CreateEntry(ModSetting.PointersReaction.ToString(), PointersReaction)
};

Hotkey = (bool)ms_entries[(int)ModSetting.Hotkey].BoxedValue;
Expand All @@ -65,6 +70,7 @@ internal static void Init()
MovementDrag = UnityEngine.Mathf.Clamp((float)ms_entries[(int)ModSetting.MovementDrag].BoxedValue, 0f, 50f);
AngularDrag = UnityEngine.Mathf.Clamp((float)ms_entries[(int)ModSetting.MovementDrag].BoxedValue, 0f, 50f);
Gravity = (bool)ms_entries[(int)ModSetting.Gravity].BoxedValue;
PointersReaction = (bool)ms_entries[(int)ModSetting.PointersReaction].BoxedValue;

if(MelonLoader.MelonMod.RegisteredMelons.FirstOrDefault(m => m.Info.Name == "BTKUILib") != null)
{
Expand Down Expand Up @@ -108,6 +114,14 @@ static void CreateBtkUi()
GravityChange?.Invoke(state);
};

ms_uiElements.Add(l_categoryMod.AddToggle("Pointers reaction", "React to CVRPointer components with 'ragdoll' type", PointersReaction));
(ms_uiElements[(int)UiElementIndex.PointersReaction] as BTKUILib.UIObjects.Components.ToggleButton).OnValueUpdated += (state) =>
{
PointersReaction = state;
ms_entries[(int)ModSetting.PointersReaction].BoxedValue = state;
PointersReactionChange?.Invoke(state);
};

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) =>
{
Expand Down Expand Up @@ -149,6 +163,11 @@ static void CreateBtkUi()
(ms_uiElements[(int)UiElementIndex.Gravity] as BTKUILib.UIObjects.Components.ToggleButton).ToggleValue = true;
GravityChange?.Invoke(true);

PointersReaction = true;
ms_entries[(int)ModSetting.PointersReaction].BoxedValue = true;
(ms_uiElements[(int)UiElementIndex.PointersReaction] as BTKUILib.UIObjects.Components.ToggleButton).ToggleValue = true;
PointersReactionChange?.Invoke(true);

VelocityMultiplier = 2f;
ms_entries[(int)ModSetting.VelocityMultiplier].BoxedValue = 2f;
(ms_uiElements[(int)UiElementIndex.VelocityMultiplier] as BTKUILib.UIObjects.Components.SliderFloat).SetSliderValue(2f);
Expand Down
1 change: 1 addition & 0 deletions ml_prm/ml_prm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="RagdollTrigger.cs" />
<Compile Include="Main.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RagdollController.cs" />
Expand Down

0 comments on commit faac28f

Please sign in to comment.