Skip to content

Commit

Permalink
feat(player): add ability to toggle headlamps (#151)
Browse files Browse the repository at this point in the history
Add two interactable zones. For the camera mounted light its located above the headset. The shoulder monted is next to the right ear. The `use` action (Right trigger) will toggle the light.
  • Loading branch information
Xenira committed Mar 17, 2024
1 parent 21a40f1 commit c56296a
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ name: release-please

env:
MOD_NAME: "techtonica_vr"
PI_UTILS_VERSION: "0.2.0"
PI_UTILS_VERSION: "0.3.0"
TTIK_VERSION: "0.2.2"
TOBEY_UNITY_AUDIO_VERSION: "2.0.2"

Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"description": "VR Mod for Techtonica",
"dependencies": [
"BepInEx-BepInExPack-5.4.2100",
"3_141-PiUtils-0.2.0",
"3_141-TTIK-0.2.0",
"3_141-PiUtils-0.3.0",
"3_141-TTIK-0.2.2",
"Tobey-UnityAudio-2.0.2"
]
}
2 changes: 1 addition & 1 deletion plugin/TechtonicaVr.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageReference Include="BepInEx.Core" Version="5.*" />
<PackageReference Include="BepInEx.PluginInfoProps" Version="2.*" />
<PackageReference Include="techtonica-libs" Version="0.3.0-e" />
<PackageReference Include="PiUtils" Version="0.2.0" />
<PackageReference Include="PiUtils" Version="0.3.0" />
<PackageReference Include="TTIK" Version="0.2.2" />
<!-- <PackageReference Include="UnityEngine.Modules" Version="5.6.0" IncludeAssets="compile" /> -->
</ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/TechtonicaVR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace TechtonicaVR;

[BepInPlugin("de.xenira.techtonicavr", MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInProcess("Techtonica.exe")]
[BepInDependency("de.xenira.ttik", "0.2.0")]
[BepInDependency("de.xenira.pi_utils", "0.2.0")]
[BepInDependency("de.xenira.ttik", "0.2.2")]
[BepInDependency("de.xenira.pi_utils", "0.3.0")]
[BepInDependency("Tobey.UnityAudio", BepInDependency.DependencyFlags.SoftDependency)]
public class TechtonicaVR : BaseUnityPlugin
{
Expand Down
6 changes: 4 additions & 2 deletions plugin/src/camera/VRCameraManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Valve.VR;
using PiUtils.Util;
using PiUtils.Debug;
using TechtonicaVR.VrPlayer;

namespace TechtonicaVR.VRCamera;
public class VRCameraManager : MonoBehaviour
Expand Down Expand Up @@ -94,10 +95,11 @@ private void SetupCamera()
Vignette.Create();
}
Teleport.Create().transform.parent = mainCamera.transform;

new GameObject("MainGamePlayer").AddComponent<MainGamePlayer>();

mainGameLoaded = true;
}

FindObjectsOfType<Headlamp>().ForEach(h => h.transform.parent = mainCamera.transform);
}

IEnumerator PatchCoroutine()
Expand Down
3 changes: 3 additions & 0 deletions plugin/src/ik/IkSetup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using PiUtils.Objects.Behaviours;
using PiUtils.Util;
using TechtonicaVR.VRCamera;
using TechtonicaVR.VrPlayer;
using TTIK.Network;
using UnityEngine;
using Valve.VR.InteractionSystem;
Expand Down Expand Up @@ -62,6 +63,8 @@ public static void SetupIk()
instance.avatar.SetActive(ModConfig.displayBody.Value);
MainGamePlayer.instance?.lightController.SetBackpackLight(GameObjectFinder.FindChildObjectByName("Backpack Spotlight", instance.avatar));
// SteamVRInputMapper.Grab.ButtonPressed += (object _sender, SteamVR_Input_Sources source) =>
// {
// Logger.LogDebug($"Grabbing target with source {source}");
Expand Down
5 changes: 5 additions & 0 deletions plugin/src/input/SteamvrInputMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,9 @@ private static void RightHandUpdate(SteamVR_Action_Pose fromAction, SteamVR_Inpu
rightHandObject.transform.localPosition = fromAction.localPosition;
rightHandObject.transform.localRotation = fromAction.localRotation;
}

public static void PlayVibration(SteamVR_Input_Sources input_sources, float amplitude, float? duration = null, float? frequency = null)
{
SteamVR_Actions._default.Haptic.Execute(0, duration ?? Time.deltaTime, frequency ?? 1f / 60f, amplitude, input_sources);
}
}
101 changes: 101 additions & 0 deletions plugin/src/player/LightController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System.Linq;
using PiUtils.Objects.Behaviours;
using PiUtils.Util;
using TechtonicaVR.Input;
using TechtonicaVR.VRCamera;
using UnityEngine;
using Valve.VR;

namespace TechtonicaVR.VrPlayer;

public class LightController
{
private static PluginLogger Logger = PluginLogger.GetLogger<LightController>();
private Headlamp[] headlamps;
private GameObject backpackLight;
private Interactable headlampInteractable;
private Interactable backpackInteractable;

public LightController(Headlamp[] headlamps)
{

SteamVRInputMapper.Use.ButtonReleased += OnUseButtonReleased;

this.headlamps = headlamps.Where(h => h.enabled).ToArray();

headlampInteractable = AddHeadlampInteractable();
}

public void SetBackpackLight(GameObject light)
{
Logger.LogDebug("Setting backpack light...");
backpackLight = light;

var interactableObject = new GameObject("BackpackLightInteractable");
interactableObject.transform.SetParent(light.transform.parent, false);
interactableObject.transform.localPosition = new Vector3(0.1837f, 0.9103f, 0.4041f);
interactableObject.transform.localRotation = Quaternion.Euler(337.1403f, 3.5846f, 277.2928f);

backpackInteractable = interactableObject.AddComponent<Interactable>();
backpackInteractable.interactionTransform = VRCameraManager.rightHandObject.transform;
backpackInteractable.OnEnter += () => SteamVRInputMapper.PlayVibration(SteamVR_Input_Sources.RightHand, 0.3f);

headlamps?.ForEach(lamp => lamp.TurnOff());
}

private void OnUseButtonReleased(object sender, SteamVR_Input_Sources source)
{
if (headlampInteractable?.isHovered == true)
{
ToggleHeadlamps();
return;
}

if (backpackInteractable?.isHovered == true)
{
ToggleBackpackLight();
}
}

public void ToggleHeadlamps(bool group = true)
{
Logger.LogDebug("Toggling headlamp...");

headlamps?.ForEach(lamp => lamp.ToggleLight());
}

public void ToggleBackpackLight(bool group = true)
{
Logger.LogDebug("Toggling backpack light...");

backpackLight?.SetActive(!backpackLight.activeSelf);
}

private Interactable AddHeadlampInteractable()
{
Logger.LogDebug("Adding headlamp interactable...");
if (headlamps == null)
{
Logger.LogDebug("No headlamp found");
return null;
}

var firstHeadlamp = headlamps.FirstOrDefault();
if (firstHeadlamp == null)
{
Logger.LogDebug("First headlamp not found");
return null;
}

var interactableObject = new GameObject("HeadlampInteractable");
interactableObject.transform.SetParent(firstHeadlamp.transform.parent, false);
interactableObject.transform.localPosition = new Vector3(-0.0324f, 0.1657f, -0.0179f);
interactableObject.transform.localRotation = Quaternion.Euler(336.0304f, 355.1733f, 353.0185f);

var interactable = interactableObject.gameObject.AddComponent<Interactable>();
interactable.interactionTransform = VRCameraManager.rightHandObject.transform;
interactable.OnEnter += () => SteamVRInputMapper.PlayVibration(SteamVR_Input_Sources.RightHand, 0.3f);

return interactable;
}
}
30 changes: 30 additions & 0 deletions plugin/src/player/MainGamePlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using PiUtils.Util;
using TechtonicaVR.VRCamera;
using UnityEngine;

namespace TechtonicaVR.VrPlayer;

public class MainGamePlayer : MonoBehaviour
{
private static PluginLogger Logger = PluginLogger.GetLogger<MainGamePlayer>();

public static MainGamePlayer instance;

public LightController lightController;

private void Start()
{
if (instance != null)
{
Logger.LogWarning("MainGamePlayer already exists! Destroying old instance!");
Destroy(instance);
}

instance = this;

Logger.LogDebug("Setting up light controller...");
var headlamps = FindObjectsOfType<Headlamp>();
headlamps.ForEach(h => h.transform.SetParent(VRCameraManager.mainCamera.transform, false));
lightController = new LightController(headlamps);
}
}

0 comments on commit c56296a

Please sign in to comment.