Skip to content

Commit

Permalink
Fix (vanilla) bug with item interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
DaXcess committed Aug 2, 2024
1 parent 2a61d7f commit 311666e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
41 changes: 41 additions & 0 deletions Source/Experiments/Experiments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using LCVR.Patches;
using System.Collections.Generic;
using System.Linq;
using LCVR.Assets;
using Unity.Netcode;
using UnityEngine;

Expand Down Expand Up @@ -87,6 +88,46 @@ private static T SpawnObject<T>(GameObject @object)
}
}

internal static class DebugLinePool
{
private static Dictionary<string, LineRenderer> lines = [];

public static LineRenderer GetLine(string key)
{
if (lines.TryGetValue(key, out var line))
{
if (line != null)
return line;
}

line = CreateRenderer();
lines[key] = line;

return line;
}

private static LineRenderer CreateRenderer()
{
var gameObject = new GameObject("DebugLinePool Line Renderer");

var lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.widthCurve.keys = [new Keyframe(0, 1)];
lineRenderer.widthMultiplier = 0.005f;
lineRenderer.positionCount = 2;
lineRenderer.SetPositions(new[] { Vector3.zero, Vector3.zero });
lineRenderer.numCornerVertices = 4;
lineRenderer.numCapVertices = 4;
lineRenderer.alignment = LineAlignment.View;
lineRenderer.shadowBias = 0.5f;
lineRenderer.useWorldSpace = true;
lineRenderer.maskInteraction = SpriteMaskInteraction.None;
lineRenderer.SetMaterials([AssetManager.DefaultRayMat]);
lineRenderer.enabled = true;

return lineRenderer;
}
}

#if DEBUG
[LCVRPatch(LCVRPatchTarget.Universal)]
[HarmonyPatch]
Expand Down
5 changes: 3 additions & 2 deletions Source/Networking/VRNetPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,11 @@ public void ShowSpectatorGhost()
/// </summary>
public void HideSpectatorGhost()
{
if (!playerGhost)
return;

foreach (var renderer in playerGhost.GetComponentsInChildren<MeshRenderer>())
{
renderer.enabled = false;
}

usernameAlpha.alpha = 0f;
}
Expand Down
50 changes: 36 additions & 14 deletions Source/Patches/ItemPatches.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using HarmonyLib;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using HarmonyLib;
using LCVR.Items;
using LCVR.Player;
using UnityEngine;
using static HarmonyLib.AccessTools;

namespace LCVR.Patches;

Expand All @@ -10,25 +14,20 @@ namespace LCVR.Patches;
internal static class ItemPatches
{
/// <summary>
/// Make the items drop at the real life hand position instead of the item's current position to make dropping easier
/// When dropping items, drop them from the real life hand position instead of the (constrained) in-game hand position
/// (Only when dropping from a larger distance)
/// </summary>
[HarmonyPatch(typeof(GrabbableObject), nameof(GrabbableObject.GetItemFloorPosition))]
[HarmonyPrefix]
private static void GetItemFloorPositionFromHand(ref Vector3 startPosition)
{
if (startPosition != Vector3.zero)
return;

var player = VRSession.Instance.LocalPlayer;
var localPosition = player.transform.InverseTransformPoint(player.RightHandVRTarget.position);

// Only apply the logic if the hand is far away enough, otherwise it looks weird on close range
// TODO: Find a good minimum distance

var magnitude = localPosition.magnitude;
Logger.LogDebug($"Drop item magnitude: {magnitude}");
var handPos = VRSession.Instance.LocalPlayer.RightHandVRTarget.position;
var playerPos = VRSession.Instance.LocalPlayer.transform.position;

if (magnitude > 0.5)
var handXZ = new Vector3(handPos.x, 0, handPos.z);
var playerXZ = new Vector3(playerPos.x, 0, playerPos.z);

if (startPosition == Vector3.zero && Vector3.Distance(handXZ, playerXZ) > 0.6f)
startPosition = VRSession.Instance.LocalPlayer.RightHandVRTarget.position;
}
}
Expand Down Expand Up @@ -60,4 +59,27 @@ private static void LateUpdatePostfix(GrabbableObject __instance, bool __runOrig
if (!__runOriginal && __instance.radarIcon != null)
__instance.radarIcon.position = __instance.transform.position;
}

/// <summary>
/// Prevent the spray paint item from calling "DiscardItem" too early
/// </summary>
[HarmonyPatch(typeof(SprayPaintItem), nameof(SprayPaintItem.DiscardItem))]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> SprayPaintMoveDiscard(IEnumerable<CodeInstruction> instructions)
{
return instructions.Skip(2).AddItem(new CodeInstruction(OpCodes.Ldarg_0)).AddItem(
new CodeInstruction(OpCodes.Callvirt,
Method(typeof(GrabbableObject), nameof(GrabbableObject.DiscardItem))));
}

/// <summary>
/// Correct the "equippedUsableItemQE" field when the walkie talkie is pocketed
/// </summary>
[HarmonyPatch(typeof(WalkieTalkie), nameof(WalkieTalkie.PocketItem))]
[HarmonyPostfix]
private static void OnPocketWalkie(WalkieTalkie __instance)
{
if (__instance.playerHeldBy != null)
__instance.playerHeldBy.equippedUsableItemQE = false;
}
}

0 comments on commit 311666e

Please sign in to comment.