-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixed issue where a specific setup could break spectating - Fixed door interactions on Artiface - Added hangar lever interactions - Added knife stabbing motion controls
- Loading branch information
Showing
12 changed files
with
293 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using System.Linq; | ||
using LCVR.Assets; | ||
using LCVR.Player; | ||
using UnityEngine; | ||
|
||
namespace LCVR.Items; | ||
|
||
public class VRKnife : VRItem<KnifeItem> | ||
{ | ||
private GameObject interactionTarget; | ||
private GameObject knifeCollider; | ||
|
||
private Vector3 previous; | ||
private float attackTimer; | ||
|
||
public float Speed { get; private set; } | ||
private Vector3 Position => VRSession.Instance.LocalPlayer.transform.InverseTransformPoint(transform.position); | ||
|
||
private new void Awake() | ||
{ | ||
base.Awake(); | ||
|
||
if (!IsLocal) | ||
return; | ||
|
||
interactionTarget = Instantiate(AssetManager.interactable, VRSession.Instance.MainCamera.transform); | ||
interactionTarget.transform.localPosition = new Vector3(0, 0, 0.5f); | ||
interactionTarget.transform.localScale = Vector3.one * 0.3f; | ||
interactionTarget.AddComponent<KnifeInteractor>(); | ||
interactionTarget.AddComponent<Rigidbody>().isKinematic = true; | ||
|
||
knifeCollider = Instantiate(AssetManager.interactable, transform); | ||
knifeCollider.transform.localPosition = new Vector3(0, 0, 7.25f); | ||
knifeCollider.transform.localScale = new Vector3(1.2f, 3, 12.9f); | ||
|
||
previous = Position; | ||
} | ||
|
||
protected override void OnUpdate() | ||
{ | ||
if (!IsLocal) | ||
return; | ||
|
||
Speed = (Position - previous).magnitude / Time.deltaTime; | ||
previous = Position; | ||
} | ||
|
||
private void OnDestroy() | ||
{ | ||
Destroy(interactionTarget); | ||
Destroy(knifeCollider); | ||
} | ||
|
||
internal void Attack() | ||
{ | ||
if (Time.realtimeSinceStartup < attackTimer) | ||
return; | ||
|
||
attackTimer = Time.realtimeSinceStartup + 0.15f; | ||
item.ItemActivate(true); | ||
} | ||
|
||
internal static RaycastHit[] GetKnifeHits(KnifeItem knife) | ||
{ | ||
var tf = knife.transform; | ||
|
||
var forwardHits = UnityEngine.Physics.SphereCastAll(tf.position, 0.3f, tf.forward, 0.75f, knife.knifeMask, | ||
QueryTriggerInteraction.Collide); | ||
var upHits = UnityEngine.Physics.SphereCastAll(tf.position, 0.3f, -tf.up, 0.75f, knife.knifeMask, | ||
QueryTriggerInteraction.Collide); | ||
|
||
RaycastHit[] allHits = [..forwardHits, ..upHits]; | ||
|
||
return allHits.GroupBy(x => x.collider).Select(x => x.First()).ToArray(); | ||
} | ||
} | ||
|
||
public class KnifeInteractor : MonoBehaviour | ||
{ | ||
private void OnTriggerEnter(Collider other) | ||
{ | ||
var knife = other.GetComponentInParent<VRKnife>(); | ||
if (knife?.Speed > 6) | ||
knife.Attack(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using LCVR.Items; | ||
using UnityEngine; | ||
using static HarmonyLib.AccessTools; | ||
|
||
namespace LCVR.Patches.Items; | ||
|
||
[LCVRPatch] | ||
[HarmonyPatch] | ||
internal static class KnifeItemPatches | ||
{ | ||
[HarmonyPatch(typeof(KnifeItem), nameof(KnifeItem.HitKnife))] | ||
[HarmonyTranspiler] | ||
private static IEnumerable<CodeInstruction> HitKnifeVRPatch(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
return new CodeMatcher(instructions) | ||
.MatchForward(false, | ||
[ | ||
new CodeMatch(OpCodes.Call, | ||
Method(typeof(UnityEngine.Physics), nameof(UnityEngine.Physics.SphereCastAll), | ||
[ | ||
typeof(Vector3), typeof(float), typeof(Vector3), typeof(float), typeof(int), | ||
typeof(QueryTriggerInteraction) | ||
])) | ||
]) | ||
.Advance(-23) | ||
.RemoveInstructions(24) | ||
.InsertAndAdvance( | ||
new CodeInstruction(OpCodes.Ldarg_0), | ||
new CodeInstruction(OpCodes.Call, Method(typeof(VRKnife), nameof(VRKnife.GetKnifeHits))) | ||
) | ||
.InstructionEnumeration(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
using HarmonyLib; | ||
using LCVR.Assets; | ||
using LCVR.Patches; | ||
using LCVR.Player; | ||
using UnityEngine; | ||
using Object = UnityEngine.Object; | ||
|
||
namespace LCVR.Physics.Interactions; | ||
|
||
public class HangarLever : MonoBehaviour, VRInteractable | ||
{ | ||
private InteractTrigger trigger; | ||
private AnimatedObjectTrigger animTrigger; | ||
|
||
private bool interacting; | ||
private Transform lookAtTransform; | ||
|
||
private float lockedRotation; | ||
private float lockedRotationTime; | ||
|
||
public InteractableFlags Flags => InteractableFlags.BothHands; | ||
|
||
private void Awake() | ||
{ | ||
trigger = GetComponentInParent<InteractTrigger>(); | ||
animTrigger = GetComponentInParent<AnimatedObjectTrigger>(); | ||
|
||
transform.parent.GetComponent<BoxCollider>().enabled = false; | ||
} | ||
|
||
public void OnColliderEnter(VRInteractor interactor) | ||
{ | ||
} | ||
|
||
public void OnColliderExit(VRInteractor interactor) | ||
{ | ||
} | ||
|
||
public bool OnButtonPress(VRInteractor interactor) | ||
{ | ||
if (!trigger.interactable) | ||
return false; | ||
|
||
interacting = true; | ||
lookAtTransform = interactor.transform; | ||
|
||
interactor.FingerCurler.ForceFist(true); | ||
|
||
return true; | ||
} | ||
|
||
public void OnButtonRelease(VRInteractor interactor) | ||
{ | ||
interactor.FingerCurler.ForceFist(false); | ||
|
||
if (!interacting) | ||
return; | ||
|
||
interacting = false; | ||
|
||
var rot = GetLookRotation(); | ||
switch (animTrigger.boolValue) | ||
{ | ||
case true when rot is > 160 and < 200: | ||
trigger.Interact(VRSession.Instance.LocalPlayer.transform); | ||
lockedRotation = 180; | ||
lockedRotationTime = Time.realtimeSinceStartup + 0.5f; | ||
|
||
break; | ||
case false when rot < 20: | ||
trigger.Interact(VRSession.Instance.LocalPlayer.transform); | ||
lockedRotation = 0; | ||
lockedRotationTime = Time.realtimeSinceStartup + 0.5f; | ||
|
||
break; | ||
} | ||
} | ||
|
||
private void LateUpdate() | ||
{ | ||
if (lockedRotationTime > Time.realtimeSinceStartup) | ||
{ | ||
transform.parent.localEulerAngles = new Vector3(0, lockedRotation, 0); | ||
return; | ||
} | ||
|
||
if (!interacting) | ||
return; | ||
|
||
transform.parent.localEulerAngles = new Vector3(0, GetLookRotation(), 0); | ||
} | ||
|
||
private float GetLookRotation() | ||
{ | ||
// I know, I'm supposed to calculate this myself but idk how and internet is not helping | ||
var tf = transform.parent; | ||
|
||
var rotation = tf.rotation; | ||
tf.LookAt(lookAtTransform.position); | ||
|
||
var lookRotation = tf.localEulerAngles.y; | ||
tf.rotation = rotation; | ||
|
||
// Prevent specific issue with the lever jumping down | ||
if (lookRotation > 270) | ||
return 0; | ||
|
||
return Mathf.Clamp(lookRotation, 0, 180); | ||
} | ||
} | ||
|
||
[LCVRPatch] | ||
[HarmonyPatch] | ||
internal static class LeverSwitchPatches | ||
{ | ||
[HarmonyPatch(typeof(InteractTrigger), nameof(InteractTrigger.Start))] | ||
[HarmonyPostfix] | ||
private static void OnInteractTriggerStart(InteractTrigger __instance) | ||
{ | ||
if (__instance.gameObject.name is not "LeverSwitchHandle") | ||
return; | ||
|
||
__instance.gameObject.name = "LeverSwitchInteractable"; | ||
|
||
var interactableObject = Object.Instantiate(AssetManager.interactable, __instance.transform); | ||
|
||
interactableObject.transform.localPosition = new Vector3(0.0044f, -0.0513f, 0.2529f); | ||
interactableObject.transform.localScale = new Vector3(0.0553f, 0.1696f, 0.0342f); | ||
interactableObject.AddComponent<HangarLever>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.