Skip to content

Commit

Permalink
Complete new item interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
DaXcess committed Sep 3, 2024
1 parent a98c424 commit 0831faf
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 19 deletions.
63 changes: 63 additions & 0 deletions Source/Items/VRBeltBagItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using LCVR.Input;
using LCVR.Player;
using UnityEngine;
using UnityEngine.XR;

namespace LCVR.Items;

public class VRBeltBagItem : VRItem<BeltBagItem>
{
private ShakeDetector shake;
private ShakeDetector shakeBig;
private BeltBagInventoryUI inventoryUI;

private float lastShakeTime;
private float lastItemDrop;
private float minWaitTime = 0.3f;

protected override void Awake()
{
base.Awake();

shake = new ShakeDetector(transform, 0.06f, true);
shakeBig = new ShakeDetector(transform, 0.1f, true);
inventoryUI = FindObjectOfType<BeltBagInventoryUI>(true);

shake.onShake += () =>
{
if (Time.realtimeSinceStartup - lastShakeTime > 0.5f)
return;

if (item.currentPlayerChecking != StartOfRound.Instance.localPlayerController)
return;

if (transform.forward.y > -0.6)
return;

if (Time.realtimeSinceStartup - lastItemDrop < minWaitTime)
return;

if (item.objectsInBag.Count < 1)
return;

inventoryUI.RemoveItemFromUI(item.objectsInBag.Count - 1);
lastItemDrop = Time.realtimeSinceStartup;
lastShakeTime = Time.realtimeSinceStartup;
minWaitTime = Random.Range(0.1f, 0.3f);

RoundManager.PlayRandomClip(item.bagAudio, item.grabItemInBagSFX, audibleNoiseID: -1);
VRSession.VibrateController(XRNode.RightHand, 0.1f, 0.5f);
};

shakeBig.onShake += () =>
{
lastShakeTime = Time.realtimeSinceStartup;
};
}

protected override void OnUpdate()
{
shake.Update();
shakeBig.Update();
}
}
2 changes: 1 addition & 1 deletion Source/Items/VRManeaterBaby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class VRManEaterBaby : VRItem<CaveDwellerPhysicsProp>
var source = VRSession.Instance.LocalPlayer.PrimaryController.transform;

shake = new ShakeDetector(source, 0.015f, true);
shakeBig = new ShakeDetector(source, 0.04f, true);
shakeBig = new ShakeDetector(source, 0.05f, true);

shake.onShake += OnShakeMotion;
shakeBig.onShake += OnShakeBigMotion;
Expand Down
2 changes: 1 addition & 1 deletion Source/Patches/HarmonyPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private static void Patch(Harmony patcher, LCVRPatchTarget target)
}
catch (Exception e)
{
Logger.LogError($"Failed to apply patches from {type}: {e.Message}");
Logger.LogError($"Failed to apply patches from {type}: {e.Message}, {e.InnerException}");
}
});
}
Expand Down
1 change: 0 additions & 1 deletion Source/Patches/Items/BeltItemPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ private static bool CloseBagPatch(BeltBagItem __instance, bool buttonDown)

StartOfRound.Instance.localPlayerController.SetInSpecialMenu(false);
return false;

}
}
3 changes: 2 additions & 1 deletion Source/Patches/PlayerControllerPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ private static IEnumerable<CodeInstruction> AllowActivateDuringMenu(IEnumerable<
.MatchForward(false,
new CodeMatch(OpCodes.Ldfld, Field(typeof(PlayerControllerB), nameof(PlayerControllerB.inSpecialMenu))))
.Advance(-1)
.RemoveInstructions(4)
.SetOpcodeAndAdvance(OpCodes.Nop)
.RemoveInstructions(3)
.InstructionEnumeration();
}
}
Expand Down
3 changes: 2 additions & 1 deletion Source/Player/Items.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ internal static class Items
{ "Flashlight", typeof(VRFlashlight) },
{ "Laser pointer", typeof(VRFlashlight) },
{ "Kitchen knife", typeof(VRKnife) },
{ "Maneater", typeof(VRManEaterBaby) }
{ "Maneater", typeof(VRManEaterBaby) },
{ "Belt bag", typeof(VRBeltBagItem) }
};

public static readonly Dictionary<string, (Vector3, Vector3)> itemOffsets = new()
Expand Down
17 changes: 6 additions & 11 deletions Source/Player/VRController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public static void DisableInteractTrigger(string objectName)
DisabledInteractTriggers.Add(objectName);
}

public void EnableDebugInteractorVisual(bool enabled = true)
public void ShowDebugInteractorVisual(bool visible = true)
{
debugLineRenderer.enabled = enabled && Plugin.Config.EnableInteractRay.Value;
debugLineRenderer.enabled = visible && Plugin.Config.EnableInteractRay.Value;
}

private void OnInteractPerformed(InputAction.CallbackContext context)
Expand Down Expand Up @@ -133,9 +133,9 @@ private void OnInteractPerformed(InputAction.CallbackContext context)
return;

// Handle belt bag item
if (PlayerController.currentlyHeldObjectServer is { itemProperties.itemId: 20 })
if (PlayerController.currentlyHeldObjectServer is BeltBagItem bag)
{
BeginBagTool();
BeginBagTool(bag);
return;
}

Expand Down Expand Up @@ -371,13 +371,8 @@ private void BeginGrabObject()
GrabItem(hit.collider.transform.gameObject.GetComponent<GrabbableObject>());
}

private void BeginBagTool()
private void BeginBagTool(BeltBagItem bag)
{
// TODO: Remove line
var line = DebugLinePool.GetLine("BagTool");
line.startColor = line.endColor = Color.red;
line.SetPositions([InteractOrigin.position, InteractOrigin.position + InteractOrigin.forward * 4]);

var ray = new Ray(InteractOrigin.position, InteractOrigin.forward);
if (!ray.Raycast(out var hit, 4, 1073742144))
return;
Expand All @@ -386,7 +381,7 @@ private void BeginBagTool()
if (item == null || item == PlayerController.currentlyHeldObjectServer || !CanBeInsertedIntoBag(item))
return;

((BeltBagItem)PlayerController.currentlyHeldObjectServer).TryAddObjectToBag(item);
bag.TryAddObjectToBag(item);
}

public void GrabItem(GrabbableObject item)
Expand Down
4 changes: 2 additions & 2 deletions Source/Player/VRSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public void OnEnterTerminal()
HUD.TerminalKeyboard.PresentKeyboard();

LocalPlayer.EnableInteractorVisuals();
LocalPlayer.PrimaryController.EnableDebugInteractorVisual(false);
LocalPlayer.PrimaryController.ShowDebugInteractorVisual(false);
}

public void OnExitTerminal()
Expand All @@ -403,7 +403,7 @@ public void OnExitTerminal()
HUD.TerminalKeyboard.Close();

LocalPlayer.EnableInteractorVisuals(false);
LocalPlayer.PrimaryController.EnableDebugInteractorVisual();
LocalPlayer.PrimaryController.ShowDebugInteractorVisual();
}

public void OnPauseMenuOpened()
Expand Down
2 changes: 1 addition & 1 deletion Source/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Plugin : BaseUnityPlugin

private readonly string[] GAME_ASSEMBLY_HASHES =
[
"9FB376FFA068AC5D6B7924F18CC6603C23F9FFE4CA2232C9E0DA552887298782", // V64 Beta
"323BFD6C60F97171C1011F758E7C2205730DB939C31E3DABEEA2D68EFF0B722F", // V64 Beta
];

public new static Config Config { get; private set; }
Expand Down

0 comments on commit 0831faf

Please sign in to comment.