Skip to content

Commit

Permalink
Rework item offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
DaXcess committed Aug 24, 2024
1 parent ed45e85 commit 34ea0af
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 48 deletions.
64 changes: 63 additions & 1 deletion Source/Experiments/Experiments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,66 @@ private static bool DeveloperMode(ref bool __result)
return false;
}
}
#endif
#endif

// TODO: Remove yes
// [LCVRPatch]
[HarmonyPatch]
internal static class ItemOffsetEditorPatches
{
private static Transform _offset;
private static Transform Offset =>
_offset == null ? _offset = new GameObject("VR Item Offset Editor").transform : _offset;

[HarmonyPatch(typeof(GrabbableObject), nameof(GrabbableObject.LateUpdate))]
[HarmonyPrefix]
private static bool OverrideItemOffset(GrabbableObject __instance)
{
return true;

if (!Offset.gameObject.activeSelf)

Check warning on line 165 in Source/Experiments/Experiments.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected

Check warning on line 165 in Source/Experiments/Experiments.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected
return true;

if (__instance.parentObject != null)
{
var tf = __instance.transform;

tf.rotation = __instance.parentObject.rotation;
tf.Rotate(Offset.eulerAngles);
tf.position = __instance.parentObject.position + __instance.parentObject.rotation * Offset.position;
}

if (__instance.radarIcon != null)
{
__instance.radarIcon.position = __instance.transform.position;
}

return false;
}

[HarmonyPatch(typeof(CaveDwellerPhysicsProp), nameof(CaveDwellerPhysicsProp.LateUpdate))]
[HarmonyPrefix]
private static bool OverrideCaveDwellerItemOffset(CaveDwellerPhysicsProp __instance)
{
return true;

if (!Offset.gameObject.activeSelf)

Check warning on line 191 in Source/Experiments/Experiments.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected

Check warning on line 191 in Source/Experiments/Experiments.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected
return true;

if (__instance.caveDwellerScript.inSpecialAnimation && __instance.parentObject != null)
{
var tf = __instance.transform;

tf.rotation = __instance.parentObject.rotation;
tf.Rotate(Offset.eulerAngles);
tf.position = __instance.parentObject.position + __instance.parentObject.rotation * Offset.position;
}

if (__instance.radarIcon != null)
{
__instance.radarIcon.position = __instance.transform.position;
}

return false;
}
}
5 changes: 0 additions & 5 deletions Source/Items/VRItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public abstract class VRItem<T> : MonoBehaviour where T : GrabbableObject
/// </summary>
protected bool UpdateWhenPocketed { get; set; }

/// <summary>
/// Prevents the game from running LateUpdate calls on this item, which mess with the position and rotation of the object
/// </summary>
public bool CancelGameUpdate { get; protected set; }

protected bool IsLocal { get; private set; }

protected virtual void Awake()
Expand Down
2 changes: 0 additions & 2 deletions Source/Items/VRShovelItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ internal class VRShovelItem : VRItem<Shovel>
{
base.Awake();

CancelGameUpdate = true;

if (!IsLocal)
return;

Expand Down
4 changes: 0 additions & 4 deletions Source/Networking/NetworkSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,9 @@ private void OnPlayerJoinedSession(VoicePlayerState player)

private void OnPlayerLeftSession(VoicePlayerState player)
{
Logger.LogWarning($"Player left session: {player.Name}");

if (!playerIdByName.TryGetValue(player.Name, out var id))
return;

Logger.LogWarning($"Okay so that was player: {id}");

if (players.TryGetValue(id, out var networkPlayer))
Destroy(networkPlayer);

Expand Down
10 changes: 5 additions & 5 deletions Source/Patches/HUDManagerPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ static void DisableComponents(GameObject @object)
/// </summary>
[HarmonyPatch(typeof(HUDManager), nameof(HUDManager.UpdateBoxesSpectateUI))]
[HarmonyTranspiler]
[HarmonyDebug]
private static IEnumerable<CodeInstruction> SpectatorBoxAlwaysOnTop(IEnumerable<CodeInstruction> instructions)
{
return new CodeMatcher(instructions)
.MatchForward(false,
new CodeMatch(OpCodes.Call,
typeof(Object)
.GetMethods(all).SingleOrDefault(method => method.ContainsGenericParameters && method.GetParameters().Length == 3 &&
method.GetParameters()[2].ParameterType == typeof(bool))!
.GetMethods(all).SingleOrDefault(method =>
method.ContainsGenericParameters && method.GetParameters().Length == 3 &&
method.GetParameters()[2].ParameterType == typeof(bool))!
.MakeGenericMethod(typeof(GameObject))))
.Advance(2)
.InsertAndAdvance(
Expand All @@ -134,13 +134,13 @@ static void SetAlwaysOnTop(GameObject obj)
{
if (element.materialForRendering == null)
continue;

if (!VRHUD.materialMappings.TryGetValue(element.materialForRendering, out var materialCopy))
{
materialCopy = new Material(element.materialForRendering);
VRHUD.materialMappings.Add(element.materialForRendering, materialCopy);
}

materialCopy.SetInt("unity_GUIZTestMode", (int)CompareFunction.Always);
element.material = materialCopy;
}
Expand Down
63 changes: 54 additions & 9 deletions Source/Patches/ItemPatches.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using HarmonyLib;
using LCVR.Items;
using LCVR.Networking;
using LCVR.Player;
using UnityEngine;

Expand Down Expand Up @@ -33,26 +33,71 @@ private static void GetItemFloorPositionFromHand(ref Vector3 startPosition)
internal static class UniversalItemPatches
{
/// <summary>
/// Prevents the built in LateUpdate if a VR item disables it
/// Handle setting custom VR item offsets, while still allowing non-VR offsets to function normally
/// </summary>
[HarmonyPatch(typeof(GrabbableObject), nameof(GrabbableObject.LateUpdate))]
[HarmonyPrefix]
private static bool LateUpdatePrefix(GrabbableObject __instance)
private static bool ItemUpdateOffset(GrabbableObject __instance)
{
if (VRItem<GrabbableObject>.itemCache.TryGetValue(__instance, out var item))
return !item.CancelGameUpdate;
return HandleUpdateItemOffset(__instance);
}

/// <summary>
/// Cave creature is different so need a separate patch
/// </summary>
[HarmonyPatch(typeof(CaveDwellerPhysicsProp), nameof(CaveDwellerPhysicsProp.LateUpdate))]
[HarmonyPrefix]
private static bool ItemUpdateOffset(CaveDwellerPhysicsProp __instance)
{
return !__instance.caveDwellerScript.inSpecialAnimation || HandleUpdateItemOffset(__instance);
}

private static bool HandleUpdateItemOffset(GrabbableObject item)
{
var isLocalPlayer = item.playerHeldBy == StartOfRound.Instance.localPlayerController;

// Don't set custom offset if local player is not in VR
if (isLocalPlayer && !VRSession.InVR)
return true;

// If the item isn't held, we don't care
if (item.playerHeldBy == null)
return true;

// Don't set custom offset if remote player is not in VR
if (!isLocalPlayer && !NetworkSystem.Instance.IsInVR((ushort)item.playerHeldBy.playerClientId))
return true;

// Prevent shovels from updating item offset as we're using our own implementation
if (item.GetType() == typeof(Shovel))
return false;

// Don't set custom offset if item does not have a custom offset
if (!Player.Items.itemOffsets.TryGetValue(item.itemProperties.itemName, out var offset))
return true;

var (positionOffset, rotationOffset) = offset;

if (item.parentObject == null)
return false;

var tf = item.transform;

tf.rotation = item.parentObject.rotation;
tf.Rotate(rotationOffset);
tf.position = item.parentObject.position + item.parentObject.rotation * positionOffset;

return true;
return false;
}

/// <summary>
/// Updates radar position of the item if the original LateUpdate function got blocked
/// Make sure to set the radar icon position if needed
/// </summary>
[HarmonyPatch(typeof(GrabbableObject), nameof(GrabbableObject.LateUpdate))]
[HarmonyPostfix]
private static void LateUpdatePostfix(GrabbableObject __instance, bool __runOriginal)
private static void PostfixSetRadarIcon(GrabbableObject __instance)
{
if (!__runOriginal && __instance.radarIcon != null)
if (__instance.radarIcon != null)
__instance.radarIcon.position = __instance.transform.position;
}
}
34 changes: 15 additions & 19 deletions Source/Player/Items.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,20 @@ internal static class Items
{ "Maneater", typeof(VRManEaterBaby) }
};

public static void UpdateVRControlsItemsOffsets()
public static readonly Dictionary<string, (Vector3, Vector3)> itemOffsets = new()
{
foreach (var item in StartOfRound.Instance.allItemsList.itemsList)
{
#if DEBUG
item.canBeGrabbedBeforeGameStart = true;
#endif

switch (item.itemName)
{
case "Chemical Jug":
item.positionOffset = new Vector3(-0.1f, 0.18f, -0.24f);
item.rotationOffset = new Vector3(180, 287.52f, 0);
break;
case "Boombox":
item.positionOffset = new Vector3(0.1f, 0, -0.03f);
break;
}
}
}
{ "Chemical jug", (new Vector3(0.2f, -0.18f, 0.24f), new Vector3(270, 180, 0)) },
{ "Toilet paper", (new Vector3(0.15f, -0.16f, 0.17f), new Vector3(270, 180, 0)) },
{ "Boombox", (new Vector3(-0.02f, 0.1f, -0.29f), new Vector3(90, 285, 0)) },
{ "Apparatus", (new Vector3(0.21f, -0.2f, 0.33f), new Vector3(90, 180, 180)) },
{ "Large axle", (new Vector3(0.07f, -0.2f, 0.3f), new Vector3(90, 180, 180)) },
{ "Cash register", (new Vector3(0.05f, -0.29f, 0.11f), new Vector3(270, 132, 288)) },
{ "V-type engine", (new Vector3(-0.10f, -0.06f, 0.26f), new Vector3(66, 190, 0)) },
{ "Extension ladder", (new Vector3(-0.20f, 0.28f, -0.47f), new Vector3(90, 90, 0)) },
{ "Painting", (new Vector3(0.05f, 0.75f, -0.06f), new Vector3(6, 270, 184)) },
{ "Soccer ball", (new Vector3(-0.07f, 0.17f, -0.19f), Vector3.zero) },
{ "Control pad", (new Vector3(0.25f, -0.22f, 0.27f), new Vector3(340, 248, 167)) },
{ "Garbage lid", (new Vector3(0.23f, -0.1f, 0.44f), new Vector3(14, 15, 17)) },
{ "Maneater", (new Vector3(0.24f, -0.15f, 0.35f), new Vector3(-6, 167, 14)) }
};
}
3 changes: 0 additions & 3 deletions Source/Player/VRSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.MixedReality.Toolkit.Experimental.UI;
using System.Collections.Generic;
using System.Linq;
using LCVR.Networking;
using LCVR.Physics.Interactions.Car;
using UnityEngine;
using UnityEngine.InputSystem;
Expand Down Expand Up @@ -358,8 +357,6 @@ private void InitializeVRSession()

// Misc
VolumeManager = Instantiate(AssetManager.VolumeManager, transform).GetComponent<Rendering.VolumeManager>();

Items.UpdateVRControlsItemsOffsets();
}

#region VR
Expand Down

0 comments on commit 34ea0af

Please sign in to comment.