diff --git a/DFUVR/DFUVR.csproj b/DFUVR/DFUVR.csproj index d84be7d..dcea80b 100644 --- a/DFUVR/DFUVR.csproj +++ b/DFUVR/DFUVR.csproj @@ -118,7 +118,7 @@ - + diff --git a/DFUVR/HandLabel.cs b/DFUVR/HandLabel.cs index db56021..3dbe1c5 100644 --- a/DFUVR/HandLabel.cs +++ b/DFUVR/HandLabel.cs @@ -1,19 +1,26 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using UnityEngine; +using UnityEngine; +using UnityEngine.XR; namespace DFUVR { public class HandLabel : MonoBehaviour { - public bool rightHand; + public bool isMainHand; + public XRNode xrHandNode; + public InputDevice inputDevice; + public KeyCode grabButton; + + public void Init(bool isMainHand, XRNode xrHandNode, InputDevice inputDevice, KeyCode grabButton) + { + this.isMainHand = isMainHand; + this.xrHandNode = xrHandNode; + this.inputDevice = inputDevice; + this.grabButton = grabButton; + } + void Start() { Plugin.LoggerInstance.LogInfo("added Handlabel"); } - } } diff --git a/DFUVR/HandObjectLoadList.cs b/DFUVR/HandObjectLoadList.cs index 3e12b6c..a27fd1f 100644 --- a/DFUVR/HandObjectLoadList.cs +++ b/DFUVR/HandObjectLoadList.cs @@ -446,7 +446,6 @@ private HandObjectLoadList(string assetBundlePath, Action postAction new HandObjectLoadList("AssetBundles/weapons", null, typeof(WeaponCollision), typeof(MeshCollider), new[] { WeaponTypes.LongBlade, WeaponTypes.LongBlade_Magic }, "Sword", Vector3.zero, Quaternion.identity, Vector3.zero, Quaternion.identity, true, true, true), new HandObjectLoadList("AssetBundles/weapons", null, typeof(WeaponCollision), typeof(MeshCollider), new[] { WeaponTypes.Dagger, WeaponTypes.Dagger_Magic }, "Steel_Dagger_512", Vector3.zero, Quaternion.Euler(0, 90, 90), Vector3.zero, Quaternion.Euler(0, 90, 90), true), new HandObjectLoadList("AssetBundles/weapons", null, typeof(WeaponCollision), typeof(MeshCollider), new[] { WeaponTypes.Battleaxe, WeaponTypes.Battleaxe_Magic }, "MM_Axe_01_01_lod2", Vector3.zero, Quaternion.Euler(0, -90, 90), Vector3.zero, Quaternion.Euler(0, 180, 180)), - new HandObjectLoadList("AssetBundles/weapons", null, null, null, new[] { WeaponTypes.None }, "Sheath", Vector3.zero, Quaternion.identity, Vector3.zero, Quaternion.identity, true), new HandObjectLoadList("AssetBundles/weapons", null, typeof(WeaponCollision), typeof(MeshCollider), new[] { WeaponTypes.Mace, WeaponTypes.Mace_Magic }, "mace", new Vector3(0, 0, 0.1f), Quaternion.Euler(90, 0, 0), new Vector3(0, 0, 0.1f), Quaternion.Euler(90, 0, 0)), new HandObjectLoadList("AssetBundles/weapons", null, typeof(WeaponCollision), typeof(MeshCollider), new[] { WeaponTypes.Flail, WeaponTypes.Flail_Magic }, "mace", new Vector3(0, 0, 0.1f), Quaternion.Euler(90, 0, 0), new Vector3(0, 0, 0.1f), Quaternion.Euler(90, 0, 0)), new HandObjectLoadList("AssetBundles/weapons", null, typeof(WeaponCollision), typeof(BoxCollider), new[] { WeaponTypes.Warhammer, WeaponTypes.Warhammer_Magic }, "Warhammer_1", Vector3.zero, Quaternion.Euler(0, 0, 90), Vector3.zero, Quaternion.identity), @@ -575,9 +574,10 @@ public static void LoadAllHandObjects(Dictionary handOb } } // this is for accessing by name later - else if (!string.IsNullOrEmpty(handObjectLoad.assetName)) + if (!string.IsNullOrEmpty(handObjectLoad.assetName)) { - handObjectDictionaryByName.Add(handObjectLoad.assetName, new HandObject(handObjectLoad, gameObject)); + if (!handObjectDictionaryByName.ContainsKey(handObjectLoad.assetName)) + handObjectDictionaryByName.Add(handObjectLoad.assetName, new HandObject(handObjectLoad, gameObject)); } handObjectLoad.postAction?.Invoke(gameObject); diff --git a/DFUVR/Plugin.cs b/DFUVR/Plugin.cs index 72fb932..3c4831d 100644 --- a/DFUVR/Plugin.cs +++ b/DFUVR/Plugin.cs @@ -562,28 +562,47 @@ static void Prefix(InputManager __instance) //ControllerPatch.flag = false; //float input = Input.GetAxis("Axis5"); //float input = Input.GetAxis(Var.rThumbStickVertical); + + // toggle skybox if (Input.GetKeyDown(Var.acceptButton)) { Var.skyboxToggle = !Var.skyboxToggle; Plugin.LoggerInstance.LogInfo(Var.skyboxToggle); } - var rightHand = InputDevices.GetDeviceAtXRNode(XRNode.RightHand); - Vector2 rThumbStick; - rightHand.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out rThumbStick); - + // adjust sheath position + { + //Var.sphereObject.transform.localPosition=Vector3.zero; + if (Var.leftHand != null) + Var.sheathOffset = Var.leftHand.transform.position; + + if (Var.mainHandSphereSheathObject != null) + { + Var.mainHandSphereSheathObject.transform.position = Var.sheathOffset; + + if (Var.offHandSphereSheathObject != null) + { + Vector3 localPos = Var.mainHandSphereSheathObject.transform.localPosition; + localPos.x = -localPos.x; + Var.offHandSphereSheathObject.transform.localPosition = localPos; + //Var.offHandSphereSheathObject.transform.position = Var.sheathOffset; + } + } + } + + // adjust height offset + { + var rightHand = InputDevices.GetDeviceAtXRNode(XRNode.RightHand); - float inputX1 = rThumbStick.x; - float inputY1 = rThumbStick.y; + Vector2 rThumbStick; + rightHand.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out rThumbStick); - float input = rThumbStick.y; + float input = rThumbStick.y; + Var.heightOffset += input / 100; - Var.heightOffset += input / 100; - //Var.sphereObject.transform.localPosition=Vector3.zero; - Var.sheathOffset = Var.leftHand.transform.position; - Var.sphereObject.transform.position = Var.sheathOffset; - GameObject vrparent = GameObject.Find("VRParent"); - vrparent.transform.localPosition = new Vector3(vrparent.transform.localPosition.x, (float)Var.heightOffset, vrparent.transform.localPosition.z); + GameObject vrparent = GameObject.Find("VRParent"); + vrparent.transform.localPosition = new Vector3(vrparent.transform.localPosition.x, (float)Var.heightOffset, vrparent.transform.localPosition.z); + } } if (Input.GetKeyDown(Var.left2Button)) { @@ -1017,60 +1036,208 @@ static void Postfix(WeaponManager __instance) [HarmonyPatch(typeof(WeaponManager), "ToggleSheath")] public class CorrectWeaponPatch : MonoBehaviour { - [HarmonyPrefix] - static void Prefix(WeaponManager __instance) + [HarmonyPostfix] + internal static void Postfix(WeaponManager __instance) { - if (Var.weaponObject != null) - Destroy(Var.weaponObject); - - if (__instance.ScreenWeapon == null) + if (GameManager.Instance == null) + { + Plugin.LoggerInstance.LogError("GameManager is null"); + return; + } + if (GameManager.Instance.PlayerEntity == null) { - Var.currentWeaponName = null; + Plugin.LoggerInstance.LogError("PlayerEntity is null"); return; } + if (GameManager.Instance.PlayerEntity.ItemEquipTable == null) + { + Plugin.LoggerInstance.LogError("ItemEquipTable is null"); + return; + } + + if (Var.leftWeaponObject != null) + Destroy(Var.leftWeaponObject); + if (Var.rightWeaponObject != null) + Destroy(Var.rightWeaponObject); - Var.currentWeaponName = __instance.ScreenWeapon.SpecificWeapon?.LongName ?? ""; + DaggerfallUnityItem leftHandItem = GameManager.Instance.PlayerEntity.ItemEquipTable.GetItem(EquipSlots.LeftHand); + DaggerfallUnityItem rightHandItem = GameManager.Instance.PlayerEntity.ItemEquipTable.GetItem(EquipSlots.RightHand); - HandObject currentHandObject = null; - if (__instance.ScreenWeapon.WeaponType != WeaponTypes.Bow && Var.handObjectsByName.ContainsKey(Var.currentWeaponName)) - currentHandObject = Var.handObjectsByName[Var.currentWeaponName]; + if (leftHandItem == null) + Var.currentLeftWeaponName = "rHandClosed"; else - currentHandObject = Var.handObjects[__instance.ScreenWeapon.WeaponType]; + Var.currentLeftWeaponName = leftHandItem.LongName; - GameObject tempObject = currentHandObject.gameObject; + if (rightHandItem == null) + Var.currentRightWeaponName = "rHandClosed"; + else + Var.currentRightWeaponName = rightHandItem.LongName; - Var.weaponObject = Instantiate(tempObject); - Var.weaponObject.GetComponent().enabled = true; + HandObject currentLeftHandObject = null; + if (Var.currentLeftWeaponName != null) + { + var weaponType = leftHandItem?.GetWeaponType() ?? WeaponTypes.Melee; - // if it is unsheathing - if (__instance.Sheathed) + if (weaponType != WeaponTypes.Bow && Var.handObjectsByName.ContainsKey(Var.currentLeftWeaponName)) + currentLeftHandObject = Var.handObjectsByName[Var.currentLeftWeaponName]; + else + currentLeftHandObject = Var.handObjects[weaponType]; + if (currentLeftHandObject == null) + { + Plugin.LoggerInstance.LogError("Current left hand object is null"); + return; + } + + Var.leftWeaponObject = Instantiate(currentLeftHandObject.gameObject); + if (Var.leftWeaponObject == null) + { + Plugin.LoggerInstance.LogError("Left weapon object is null after instantiation"); + return; + } + + Collider collider = Var.leftWeaponObject.GetComponent(); + if (collider != null) + collider.enabled = true; + + WeaponCollision weaponCollision = Var.leftWeaponObject.GetComponent(); + if (weaponCollision != null && leftHandItem != null) + weaponCollision.item = leftHandItem; + } + + HandObject currentRightHandObject = null; + if (Var.currentRightWeaponName != null) { - Var.sheathObject.GetComponent().enabled = true; + var weaponType = rightHandItem?.GetWeaponType() ?? WeaponTypes.Melee; - if (Var.leftHanded) - Var.weaponObject.transform.SetParent(Var.leftHand.transform); + if (weaponType != WeaponTypes.Bow && Var.handObjectsByName.ContainsKey(Var.currentRightWeaponName)) + currentRightHandObject = Var.handObjectsByName[Var.currentRightWeaponName]; else - Var.weaponObject.transform.SetParent(Var.rightHand.transform); + currentRightHandObject = Var.handObjects[weaponType]; + if (currentRightHandObject == null) + { + Plugin.LoggerInstance.LogError("Current right hand object is null"); + return; + } - Var.weaponObject.transform.localPosition = currentHandObject.unsheatedPositionOffset; - Var.weaponObject.transform.localRotation = currentHandObject.unsheatedRotationOffset; - Var.weaponObject.SetActive(true); + Var.rightWeaponObject = Instantiate(currentRightHandObject.gameObject); + if (Var.rightWeaponObject == null) + { + Plugin.LoggerInstance.LogError("Right weapon object is null after instantiation"); + return; + } - Hands.rHand.SetActive(false); - Hands.lHand.SetActive(false); + Collider collider = Var.rightWeaponObject.GetComponent(); + if (collider != null) + collider.enabled = true; + + WeaponCollision weaponCollision = Var.rightWeaponObject.GetComponent(); + if (weaponCollision != null && rightHandItem != null) + weaponCollision.item = rightHandItem; } - else + + if (__instance.Sheathed) { - Var.weaponObject.GetComponent().enabled = false; - Var.weaponObject.transform.SetParent(Var.sheathObject.transform); + if (Var.leftWeaponObject != null) + { + Collider collider = Var.leftWeaponObject.GetComponent(); + if (collider != null) + collider.enabled = false; + + if (Var.offHandSheathObject != null) + Var.leftWeaponObject.transform.SetParent(Var.offHandSheathObject.transform); + + Var.leftWeaponObject.transform.localPosition = currentLeftHandObject.sheatedPositionOffset; + Var.leftWeaponObject.transform.localRotation = currentLeftHandObject.sheatedRotationOffset; + } - Var.weaponObject.transform.localPosition = currentHandObject.sheatedPositionOffset; - Var.weaponObject.transform.localRotation = currentHandObject.sheatedRotationOffset; - Var.sheathObject.GetComponent().enabled = currentHandObject.renderSheated; + if (Var.rightWeaponObject != null) + { + Collider collider = Var.rightWeaponObject.GetComponent(); + if (collider != null) + collider.enabled = false; + + if (Var.mainHandSheathObject != null) + Var.rightWeaponObject.transform.SetParent(Var.mainHandSheathObject.transform); + + Var.rightWeaponObject.transform.localPosition = currentRightHandObject.sheatedPositionOffset; + Var.rightWeaponObject.transform.localRotation = currentRightHandObject.sheatedRotationOffset; + } + + if (Var.mainHandSheathObject != null) + { + MeshRenderer meshRenderer = Var.mainHandSheathObject.GetComponent(); + if (meshRenderer != null) + meshRenderer.enabled = currentRightHandObject.renderSheated; + } + if (Var.offHandSheathObject != null) + { + MeshRenderer meshRenderer = Var.offHandSheathObject.GetComponent(); + if (meshRenderer != null) + meshRenderer.enabled = currentLeftHandObject.renderSheated; + } Hands.rHand.SetActive(true); Hands.lHand.SetActive(true); } + else + { + if (Var.mainHandSheathObject != null) + { + MeshRenderer meshRenderer = Var.mainHandSheathObject.GetComponent(); + if (meshRenderer != null) + meshRenderer.enabled = true; + } + if (Var.offHandSheathObject != null) + { + MeshRenderer meshRenderer = Var.offHandSheathObject.GetComponent(); + if (meshRenderer != null) + meshRenderer.enabled = true; + } + + if (Var.leftWeaponObject != null) + { + Var.leftWeaponObject.transform.SetParent(Var.offHand.transform); + + if (currentLeftHandObject != null) + { + Var.leftWeaponObject.transform.localPosition = currentLeftHandObject.unsheatedPositionOffset; + Var.leftWeaponObject.transform.localRotation = currentLeftHandObject.unsheatedRotationOffset; + } + Var.leftWeaponObject.SetActive(true); + } + + if (Var.rightWeaponObject != null) + { + Var.rightWeaponObject.transform.SetParent(Var.mainHand.transform); + + if (currentRightHandObject != null) + { + Var.rightWeaponObject.transform.localPosition = currentRightHandObject.unsheatedPositionOffset; + Var.rightWeaponObject.transform.localRotation = currentRightHandObject.unsheatedRotationOffset; + } + Var.rightWeaponObject.SetActive(true); + } + + Hands.rHand.SetActive(false); + Hands.lHand.SetActive(false); + } + } + } + + [HarmonyPatch(typeof(WeaponManager), "ApplyWeapon")] + public class ApplyWeaponPatch : MonoBehaviour + { + [HarmonyPostfix] + static void Postfix(WeaponManager __instance) + { + DaggerfallUnityItem leftHandItem = GameManager.Instance.PlayerEntity.ItemEquipTable.GetItem(EquipSlots.LeftHand); + DaggerfallUnityItem rightHandItem = GameManager.Instance.PlayerEntity.ItemEquipTable.GetItem(EquipSlots.RightHand); + + string leftName = leftHandItem?.LongName ?? "rHandClosed"; + string rightName = rightHandItem?.LongName ?? "rHandClosed"; + + if (leftName != Var.currentLeftWeaponName || rightName != Var.currentRightWeaponName) + CorrectWeaponPatch.Postfix(__instance); } } diff --git a/DFUVR/Sheath.cs b/DFUVR/Sheath.cs deleted file mode 100644 index 612ecde..0000000 --- a/DFUVR/Sheath.cs +++ /dev/null @@ -1,109 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using UnityEngine; -using BepInEx; -using DaggerfallWorkshop.Game; -using UnityEngine.XR; - - -namespace DFUVR -{ - public class SheathController : MonoBehaviour - { - public static GameObject sheath; - public static GameObject sphere; - public static SphereCollider sphereCollider; - public static GameObject sheathOB; - private static bool gripFlag=false; - private static bool alreadyGripped=false; - void Start() - { - sphere = new GameObject("Sphere"); - sphere.transform.parent=GameObject.Find("Body").transform; - if (GameObject.Find("Body") == null) { Plugin.LoggerInstance.LogError("Body not found. Are you a ghost? Or did I just fuck smthn up?"); } - //sphere.transform.localPosition = new Vector3(-0.155f,0,0); - //sphere.transform.localPosition = Var.sheathOffset; - //sphere.transform.localPosition = sphere.transform.parent.InverseTransformPoint(Var.sheathOffset); - sphere.transform.Rotate(-231.724f, 0, 0); - - - sphere.transform.localScale = new Vector3(0.25f, 0.25f, 0.25f); - - sphereCollider = sphere.AddComponent(); - sphereCollider.radius = 0.25f; - sphereCollider.isTrigger = true; - Var.sphereObject = sphere; - sphere.AddComponent(); - - Rigidbody rb=sphere.AddComponent(); - rb.isKinematic = true; - rb.useGravity=false; - //DebugSphere.CreateVisualizer(sphereCollider); - - string assetBundlePath = Path.Combine(Paths.PluginPath, "AssetBundles/weapons"); - - - AssetBundle assetBundle = AssetBundle.LoadFromFile(assetBundlePath); - - GameObject sheathInstance = assetBundle.LoadAsset("Sheath"); - - GameObject sheath = Instantiate(sheathInstance); - - sheath.transform.parent = sphere.transform; - sheath.transform.localRotation = Quaternion.identity; - - sheathOB = sheath; - //GameObject sheath = new GameObject("Sheath"); - //sheath.transform.parent = sphere.transform; - //sheath.transform.localRotation = Quaternion.identity; - - assetBundle.Unload(false); - Var.sheathObject=sheath; - - //sphere.transform.localPosition = sphere.transform.parent.InverseTransformPoint(Var.sheathOffset); - sphere.transform.localPosition = Var.sheathOffset; - - } - //this will handle all interaction with the Sheath - - void Update() - { - if (Var.isNotOculus) - { - bool gripButton; - var rightHand = InputDevices.GetDeviceAtXRNode(XRNode.RightHand); - if (Var.leftHanded) { rightHand = InputDevices.GetDeviceAtXRNode(XRNode.RightHand); } - rightHand.TryGetFeatureValue(CommonUsages.gripButton, out gripButton); - //rightHand.TryGetFeatureValue(CommonUsages.secondaryButton, out secondaryButton); - - if (gripButton) { gripFlag = true; } - if(!gripButton) { gripFlag= false; alreadyGripped = false; } - if ((gripFlag && SheathCollision.flag) && !alreadyGripped) - { - alreadyGripped = true; - GameObject.Find("PlayerAdvanced").GetComponent().ToggleSheath(); - } - } - if (Input.GetKeyDown(Var.gripButton)) - { - gripFlag = true; - } - if(Input.GetKeyUp(Var.gripButton)) { gripFlag = false; alreadyGripped = false; } - if ((gripFlag && SheathCollision.flag)&&!alreadyGripped) - { - alreadyGripped = true; - GameObject.Find("PlayerAdvanced").GetComponent().ToggleSheath(); - - - } - - - } - - - } -} diff --git a/DFUVR/SheathCollision.cs b/DFUVR/SheathCollision.cs index 85fd9f8..19aa4a6 100644 --- a/DFUVR/SheathCollision.cs +++ b/DFUVR/SheathCollision.cs @@ -1,47 +1,40 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; using UnityEngine; -using DaggerfallWorkshop; +using UnityEngine.XR; + namespace DFUVR { - public class SheathCollision:MonoBehaviour + public class SheathCollision : MonoBehaviour { - public static bool flag=false; - public static bool rightHand=true; - void OnTriggerEnter(Collider other) - { - if (other.gameObject.GetComponent() != null) - { - - if (other.gameObject.GetComponent().rightHand == false) - { - Haptics.TriggerHapticFeedback(UnityEngine.XR.XRNode.LeftHand, 0.6f); - } - else - { - Haptics.TriggerHapticFeedback(UnityEngine.XR.XRNode.RightHand, 0.6f); - flag = true; + public Dictionary handsInside = new Dictionary(2); - } - - //Plugin.LoggerInstance.LogInfo("Entered Collider"); + public SheathCollision() + { + handsInside.Add(XRNode.LeftHand, null); + handsInside.Add(XRNode.RightHand, null); + } - } + void OnTriggerEnter(Collider other) + { + var handLabel = other.gameObject.GetComponent(); + if (handLabel == null) + return; + Haptics.TriggerHapticFeedback(handLabel.xrHandNode, 0.6f); + handsInside[handLabel.xrHandNode] = handLabel; + //Plugin.LoggerInstance.LogInfo("Entered Collider"); } - void OnTriggerExit(Collider other) + + void OnTriggerExit(Collider other) { - if (other.gameObject.GetComponent() != null) - { - flag = false; - //Plugin.LoggerInstance.LogInfo("Exited Collider"); + var handLabel = other.gameObject.GetComponent(); + if (handLabel == null) + return; - } + handsInside[handLabel.xrHandNode] = null; + //Plugin.LoggerInstance.LogInfo("Exited Collider"); } } } diff --git a/DFUVR/SheathController.cs b/DFUVR/SheathController.cs new file mode 100644 index 0000000..8201a17 --- /dev/null +++ b/DFUVR/SheathController.cs @@ -0,0 +1,129 @@ +using System.IO; +using UnityEngine; +using BepInEx; +using DaggerfallWorkshop.Game; +using UnityEngine.XR; +using System; + +namespace DFUVR +{ + public class SheathController : MonoBehaviour + { + public GameObject sheath; + public GameObject sphere; + public SphereCollider sphereCollider; + public GameObject sheathOB; + public SheathCollision sheathCollision; + + public bool isOffHandSheath = false; + + private bool isGripPressed = false; + private bool alreadyGripped = false; + + void Start() + { + sphere = new GameObject("Sphere" + Guid.NewGuid()); + + var body = GameObject.Find("Body"); + if (body == null) + Plugin.LoggerInstance.LogError("Body not found. Are you a ghost? Or did I just fuck smthn up?"); + else + sphere.transform.parent = body.transform; + + //sphere.transform.localPosition = new Vector3(-0.155f,0,0); + //sphere.transform.localPosition = Var.sheathOffset; + //sphere.transform.localPosition = sphere.transform.parent.InverseTransformPoint(Var.sheathOffset); + //sphere.transform.Rotate(-231.724f, 0, 0); + sphere.transform.Rotate(-300f, 0, 0); + sphere.transform.localScale = new Vector3(0.25f, 0.25f, 0.25f); + + sphereCollider = sphere.AddComponent(); + sphereCollider.radius = 0.25f; + sphereCollider.isTrigger = true; + + sheathCollision = sphere.AddComponent(); + + Rigidbody rb = sphere.AddComponent(); + rb.isKinematic = true; + rb.useGravity = false; + //DebugSphere.CreateVisualizer(sphereCollider); + + string assetBundlePath = Path.Combine(Paths.PluginPath, "AssetBundles/weapons"); + AssetBundle assetBundle = AssetBundle.LoadFromFile(assetBundlePath); + GameObject sheathAsset = assetBundle.LoadAsset("Sheath"); + assetBundle.Unload(false); + + GameObject sheath = Instantiate(sheathAsset); + sheath.transform.parent = sphere.transform; + sheath.transform.localRotation = Quaternion.identity; + + sheathOB = sheath; + //GameObject sheath = new GameObject("Sheath"); + //sheath.transform.parent = sphere.transform; + //sheath.transform.localRotation = Quaternion.identity; + + //sphere.transform.localPosition = sphere.transform.parent.InverseTransformPoint(Var.sheathOffset); + sphere.transform.localPosition = Var.sheathOffset; + + if (isOffHandSheath) + { + Var.offHandSphereSheathObject = sphere; + Var.offHandSheathObject = sheathOB; + MirrorSheathPosition(); + } + else + { + Var.mainHandSphereSheathObject = sphere; + Var.mainHandSheathObject = sheathOB; + } + } + + //this will handle all interaction with the Sheath + void Update() + { + foreach (var hand in sheathCollision.handsInside.Values) + { + if (hand == null) + continue; + + if (Var.isNotOculus) + { + hand.inputDevice.TryGetFeatureValue(CommonUsages.gripButton, out bool gripButton); + if (gripButton) + { + isGripPressed = true; + } + else + { + isGripPressed = false; + alreadyGripped = false; + } + } + else + { + if (Input.GetKeyDown(hand.grabButton)) + isGripPressed = true; + + if (Input.GetKeyUp(hand.grabButton)) + { + isGripPressed = false; + alreadyGripped = false; + } + } + + if (isGripPressed && !alreadyGripped) + { + alreadyGripped = true; + GameObject.Find("PlayerAdvanced").GetComponent().ToggleSheath(); + } + } + } + + private void MirrorSheathPosition() + { + Vector3 localPos = sphere.transform.localPosition; + localPos.x = -localPos.x; + sphere.transform.localPosition = localPos; + } + } +} diff --git a/DFUVR/SlotCollision.cs b/DFUVR/SlotCollision.cs index 6c4d0f6..77a28a6 100644 --- a/DFUVR/SlotCollision.cs +++ b/DFUVR/SlotCollision.cs @@ -17,7 +17,7 @@ void OnTriggerEnter(Collider other) { //Plugin.LoggerInstance.LogInfo("Collision detected"); - if (other.gameObject.GetComponent().rightHand == false) + if (other.gameObject.GetComponent().isMainHand == false) { Haptics.TriggerHapticFeedback(UnityEngine.XR.XRNode.LeftHand, 0.6f); } diff --git a/DFUVR/SpawnHands.cs b/DFUVR/SpawnHands.cs index f7935d2..3046b38 100644 --- a/DFUVR/SpawnHands.cs +++ b/DFUVR/SpawnHands.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using UnityEngine; using UnityEngine.SpatialTracking; +using UnityEngine.XR; using static DaggerfallWorkshop.Game.InputManager;//for slots namespace DFUVR @@ -25,27 +26,31 @@ public static void Spawn() Var.leftHand.transform.parent = GameObject.Find("VRParent").transform; - TrackedPoseDriver rightTracker=Var.rightHand.AddComponent(); + TrackedPoseDriver rightTracker = Var.rightHand.AddComponent(); TrackedPoseDriver leftTracker = Var.leftHand.AddComponent(); rightTracker.SetPoseSource(TrackedPoseDriver.DeviceType.GenericXRController, TrackedPoseDriver.TrackedPose.RightPose); leftTracker.SetPoseSource(TrackedPoseDriver.DeviceType.GenericXRController, TrackedPoseDriver.TrackedPose.LeftPose); - SphereCollider rCollider=Var.rightHand.AddComponent(); + SphereCollider rCollider = Var.rightHand.AddComponent(); SphereCollider lCollider = Var.leftHand.AddComponent(); rCollider.radius = 0.0668935f; lCollider.radius = 0.0668935f; rCollider.isTrigger = true; - lCollider.isTrigger=true; - Var.rightHand.AddComponent().rightHand=true; - Var.leftHand.AddComponent().rightHand = false; + lCollider.isTrigger = true; - if (Var.leftHanded) { - Var.rightHand.GetComponent().rightHand = false; - Var.leftHand.GetComponent().rightHand = true; + if (Var.leftHanded) + { + Var.rightHand.AddComponent().Init(false, XRNode.RightHand, InputDevices.GetDeviceAtXRNode(XRNode.RightHand), Var.gripButton); + Var.leftHand.AddComponent().Init(true, XRNode.LeftHand, InputDevices.GetDeviceAtXRNode(XRNode.LeftHand), Var.lGripButton); + } + else + { + Var.rightHand.AddComponent().Init(true, XRNode.RightHand, InputDevices.GetDeviceAtXRNode(XRNode.RightHand), Var.gripButton); + Var.leftHand.AddComponent().Init(false, XRNode.LeftHand, InputDevices.GetDeviceAtXRNode(XRNode.LeftHand), Var.lGripButton); } - + //Rigidbody rHandBody=Var.rightHand.AddComponent(); //Rigidbody lHandBody = Var.leftHand.AddComponent(); @@ -59,8 +64,8 @@ public static void Spawn() AssetBundle assetBundle = AssetBundle.LoadFromFile(assetBundlePath); - GameObject controllerPrefab = assetBundle.LoadAsset("vr_controller_01_mrhat"); + assetBundle.Unload(false); rHand = Instantiate(controllerPrefab); lHand= Instantiate(controllerPrefab); @@ -83,8 +88,9 @@ public static void Spawn() Var.body = new GameObject("Body"); Var.body.AddComponent(); - Var.body.AddComponent(); - assetBundle.Unload(false); + Var.body.AddComponent().isOffHandSheath = false; + Var.body.AddComponent().isOffHandSheath = true; + try { string watchBundlePath = Path.Combine(pluginFolderPath, "AssetBundles/watchandfonts"); diff --git a/DFUVR/Var.cs b/DFUVR/Var.cs index 9b4e8c3..4f6b73b 100644 --- a/DFUVR/Var.cs +++ b/DFUVR/Var.cs @@ -43,9 +43,13 @@ public class Var : MonoBehaviour public static Vector3 sheathOffset; public static bool leftHanded; - - public static GameObject sphereObject; + + public static GameObject mainHandSphereSheathObject; + public static GameObject offHandSphereSheathObject; + public static GameObject mainHandSheathObject; + public static GameObject offHandSheathObject; + //Default Bindings public static KeyCode gripButton = KeyCode.JoystickButton5; //public static KeyCode gripButton = KeyCode.JoystickButton5; @@ -80,12 +84,28 @@ public class Var : MonoBehaviour public static GameObject VRParent; public static GameObject debugSphere; + public static GameObject mainHand + { + get + { + return leftHanded ? leftHand : rightHand; + } + } + public static GameObject offHand + { + get + { + return leftHanded ? rightHand : leftHand; + } + } + public static Dictionary handObjects = new Dictionary(); public static Dictionary handObjectsByName = new Dictionary(); - public static GameObject weaponObject; - public static string currentWeaponName; - public static GameObject sheathObject; + public static GameObject rightWeaponObject; + public static GameObject leftWeaponObject; + public static string currentRightWeaponName; + public static string currentLeftWeaponName; public static int connectedJoysticks; @@ -148,8 +168,8 @@ public static void InitKeyboard() Button[]buttons=keyboard.GetComponentsInChildren