Skip to content

Commit

Permalink
Add light switch interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
DaXcess committed Jan 14, 2025
1 parent c2f536a commit ab8195f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

**Additions**:
- Added persistent configuration that saves your LCVR config between modpacks. Can be disabled in the settings.
- Added physical interactions for light switches

**Bug fixes**:
- Fixed cursor not re-appearing when exiting VR through F8
Expand Down
9 changes: 7 additions & 2 deletions Source/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ public class Config(string assemblyPath, ConfigFile file)

public ConfigEntry<bool> DisableBreakerBoxInteraction { get; } = file.Bind("Interaction",
"DisableBreakerBoxInteraction", false,
"Disabled needing to physically open the breaker box and flip the switches with your finger.");
"Disable needing to physically open the breaker box and flip the switches with your finger.");

public ConfigEntry<bool> DisableLightSwitchInteraction { get; } = file.Bind("Interaction",
nameof(DisableLightSwitchInteraction), false, "Disable needing to physically switch light switches");

public ConfigEntry<bool> DisableDoorInteraction { get; } = file.Bind("Interaction", "DisableDoorInteraction", false,
"Disable needing to physically open and close doors by interacting with the door handles. Will also disable the need to use keys and lockpickers physically on the door handle.");
Expand Down Expand Up @@ -306,7 +309,9 @@ public void DeserializeFromES3()

var entry = File[section, key];

entry.BoxedValue = entry.SettingType.IsEnum ? Enum.ToObject(entry.SettingType, value) : Convert.ChangeType(value, entry.SettingType);
entry.BoxedValue = entry.SettingType.IsEnum
? Enum.ToObject(entry.SettingType, value)
: Convert.ChangeType(value, entry.SettingType);
}
}
catch (Exception ex)
Expand Down
66 changes: 66 additions & 0 deletions Source/Physics/Interactions/LightSwitch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using HarmonyLib;
using LCVR.Assets;
using LCVR.Patches;
using LCVR.Player;
using UnityEngine;

namespace LCVR.Physics.Interactions;

public class LightSwitch : MonoBehaviour, VRInteractable
{
private InteractTrigger trigger;
private float lastInteraction;

public InteractableFlags Flags => InteractableFlags.BothHands;

private void Awake()
{
trigger = GetComponentInParent<InteractTrigger>();
}

public void OnColliderEnter(VRInteractor interactor)
{
if (!interactor.FingerCurler.IsPointer || !trigger.interactable || Time.realtimeSinceStartup - lastInteraction < 0.25f)
return;

lastInteraction = Time.realtimeSinceStartup;
trigger.Interact(VRSession.Instance.LocalPlayer.transform);
}

public void OnColliderExit(VRInteractor interactor)
{
}

public bool OnButtonPress(VRInteractor interactor)
{
return false;
}

public void OnButtonRelease(VRInteractor interactor)
{
}
}

[LCVRPatch]
[HarmonyPatch]
internal static class LightSwitchPatches
{
[HarmonyPatch(typeof(InteractTrigger), nameof(InteractTrigger.Start))]
[HarmonyPostfix]
private static void OnInteractTriggerStart(InteractTrigger __instance)
{
if (Plugin.Config.DisableLightSwitchInteraction.Value)
return;

var go = __instance.gameObject;
if (go.name is not "LightSwitch")
return;

var interactableObject = Object.Instantiate(AssetManager.Interactable, __instance.transform);

interactableObject.transform.localPosition = new Vector3(0, -0.04f, 0.02f);
interactableObject.transform.localScale = new Vector3(0.05f, 0.07f, 0.11f);

interactableObject.AddComponent<LightSwitch>();
}
}
3 changes: 3 additions & 0 deletions Source/Player/VRSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ private void InitializeVRSession()
for (var i = 1; i <= 5; i++)
VRController.DisableInteractTrigger($"BreakerSwitch{i}");
}

if (!Plugin.Config.DisableLightSwitchInteraction.Value)
VRController.DisableInteractTrigger("LightSwitch");

// Doors
if (!Plugin.Config.DisableDoorInteraction.Value)
Expand Down

0 comments on commit ab8195f

Please sign in to comment.