diff --git a/DFUVR/DFUVR.csproj b/DFUVR/DFUVR.csproj index d84be7d..3400fcd 100644 --- a/DFUVR/DFUVR.csproj +++ b/DFUVR/DFUVR.csproj @@ -118,7 +118,7 @@ - + @@ -130,6 +130,7 @@ + diff --git a/DFUVR/HandLabel.cs b/DFUVR/HandLabel.cs index db56021..2c8a555 100644 --- a/DFUVR/HandLabel.cs +++ b/DFUVR/HandLabel.cs @@ -1,19 +1,58 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using DaggerfallWorkshop.Game.Items; 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 GameObject freeHandObject; + + public DaggerfallUnityItem weaponItem; + public GameObject weaponObject; + + 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"); } + public void RemoveHeldWeapon() + { + SetWeapon(null, null, null); + } + + public void SetWeapon(DaggerfallUnityItem weaponItem, GameObject weaponObject, HandObject handObject) + { + if (this.weaponObject != null) + Destroy(this.weaponObject); + + this.weaponObject = weaponObject; + this.weaponItem = weaponItem; + + if (weaponObject != null) + { + weaponObject.transform.SetParent(gameObject.transform); + + if (handObject != null) + { + weaponObject.transform.localPosition = handObject.unsheatedPositionOffset; + weaponObject.transform.localRotation = handObject.unsheatedRotationOffset; + } + + weaponObject.SetActive(true); + } + } } } 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..342cac8 100644 --- a/DFUVR/Plugin.cs +++ b/DFUVR/Plugin.cs @@ -1,15 +1,4 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Reflection.Emit; -using System.Runtime.CompilerServices; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using BepInEx; +using BepInEx; using BepInEx.Logging; using DaggerfallConnect; using DaggerfallWorkshop; @@ -18,25 +7,35 @@ using DaggerfallWorkshop.Game.Formulas; using DaggerfallWorkshop.Game.Items; using DaggerfallWorkshop.Game.MagicAndEffects; +using DaggerfallWorkshop.Game.MagicAndEffects.MagicEffects; using DaggerfallWorkshop.Game.Questing; using DaggerfallWorkshop.Game.Serialization; using DaggerfallWorkshop.Game.UserInterface; using DaggerfallWorkshop.Game.UserInterfaceWindows; +using DaggerfallWorkshop.Utility; using HarmonyLib; using HarmonyLib.Tools; +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Reflection.Emit; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading; +using System.Threading.Tasks; using UnityEngine; using UnityEngine.EventSystems; -using static DaggerfallWorkshop.Game.InputManager; -using static UnityEngine.GraphicsBuffer; using UnityEngine.XR; using UnityEngine.XR.Provider; -using DaggerfallWorkshop.Utility; -using DaggerfallWorkshop.Game.MagicAndEffects.MagicEffects; +using static DaggerfallWorkshop.Game.InputManager; namespace DFUVR { - + //initialize UI [HarmonyPatch(typeof(DaggerfallUI), "Start")] public class MenuPatch : MonoBehaviour @@ -45,10 +44,10 @@ public class MenuPatch : MonoBehaviour static void Postfix(DaggerfallUI __instance) { __instance.gameObject.AddComponent(); - + __instance.StartCoroutine(UI.Spawn()); - - + + } } @@ -262,7 +261,7 @@ static void Postfix(DaggerfallMissile __instance) [HarmonyPatch(typeof(UserInterfaceManager), "PushWindow")] public class PushPatch : MonoBehaviour { - private static PushPatch __pInstance; + private static PushPatch __pInstance; void Awake() { @@ -305,7 +304,7 @@ public static IEnumerator Waiter1() Vector3 uiPositionInFront = vrCameraTransform.position + forwardFlat * 1.5f; vrui.transform.localPosition = vrui.transform.parent.InverseTransformPoint(uiPositionInFront); Vector3 lookDirection = uiPositionInFront - vrCameraTransform.position; - lookDirection.y = 0; + lookDirection.y = 0; vrui.transform.rotation = Quaternion.LookRotation(lookDirection); FixObstruction(vrui); @@ -433,22 +432,22 @@ static void FixObstruction(GameObject vrui) if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask)) { - + if (hit.transform != vrui.transform) { //Plugin.LoggerInstance.LogInfo($"Obstruction found: {hit.transform.name}"); - Vector3 newUIPosition = hit.point + hit.normal * 0.1f; + Vector3 newUIPosition = hit.point + hit.normal * 0.1f; vrui.transform.position = newUIPosition; Vector3 lookDirection = newUIPosition - vrCameraTransform.position; - lookDirection.y = 0; + lookDirection.y = 0; vrui.transform.rotation = Quaternion.LookRotation(lookDirection); } } } } - + //[HarmonyPatch(typeof(DaggerfallUI), "PopupMessage")] //public class PopuphPatch : MonoBehaviour //{ @@ -514,7 +513,7 @@ public class ControllerPatch : MonoBehaviour private static bool changedCam = false; public static bool bindingCalibrated=false; [HarmonyPrefix] - + static void Prefix(InputManager __instance) { __instance.EnableController=false; @@ -541,7 +540,7 @@ static void Prefix(InputManager __instance) { CoroutineRunner.Instance.StartCoroutine(Waiter2()); //GameObject.Find("PLayerAdvanced").AddComponent(); - + } @@ -562,28 +561,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)) { @@ -595,7 +613,7 @@ static void Prefix(InputManager __instance) } - + //we don't want to open the pause menu when the user just wants to recalibrate if (flag) { InputManager.Instance.SetBinding(KeyCode.Escape, Actions.Escape, true); } else if (!flag) { InputManager.Instance.SetBinding(Var.left1Button, Actions.Escape, true); } @@ -636,7 +654,7 @@ static void Prefix(InputManager __instance) { isChanging = true; } - if ((!primaryPressed || !secondaryPressed) && flag) + if ((!primaryPressed || !secondaryPressed) && flag) { //Debug.Log("canceled"); flag = false; @@ -650,7 +668,7 @@ static void Prefix(InputManager __instance) if (!flag) SnapTurnProvider.Snap(); - + GameObject exterior = GameObject.Find("Exterior"); //this atleast creates a sense of day and night @@ -660,16 +678,16 @@ static void Prefix(InputManager __instance) Var.VRCamera.clearFlags = CameraClearFlags.Nothing; } else { Var.VRCamera.clearFlags = CameraClearFlags.Skybox; } - if (!bindingCalibrated) + if (!bindingCalibrated) { AccessTools.Method(typeof(InputManager), "UpdateBindingCache").Invoke(__instance, null); //__instance.UpdateBindingCache(); bindingCalibrated = true; } - + //Plugin.LoggerInstance.LogInfo(Time.fixedDeltaTime); - + } public static IEnumerator Waiter2() { //Plugin.LoggerInstance.LogInfo("Coroutine 2"); @@ -773,7 +791,7 @@ static bool Prefix(InputManager __instance, ref float __result) Vector2 lThumbStick; rightHand.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out lThumbStick); - + float vertical = lThumbStick.y; __result = vertical; @@ -785,7 +803,7 @@ static bool Prefix(InputManager __instance, ref float __result) public class LevitationPatch { [HarmonyPrefix] - static void Prefix(LevitateMotor __instance) + static void Prefix(LevitateMotor __instance) { //float inputX1 = Input.GetAxis("Axis1"); //float inputY1 = Input.GetAxis("Axis2"); @@ -832,7 +850,7 @@ public class MissileDirectionPatch [HarmonyPostfix] static void Postfix(DaggerfallMissile __instance, ref Vector3 __result) { - + if (__instance.CustomAimDirection != Vector3.zero) { __result = __instance.CustomAimDirection; @@ -864,7 +882,7 @@ static void Postfix(DaggerfallMissile __instance, ref Vector3 __result) aimDirection += Vector3.down * 0.05f; } - + __result = aimDirection; } } @@ -888,9 +906,9 @@ static void Postfix(DaggerfallMissile __instance, ref Vector3 __result) { aimPosition = Var.rightHand.transform.position; } - - + + __result = aimPosition; } } @@ -915,7 +933,7 @@ static bool Prefix(DaggerfallMissile __instance, ref DaggerfallEntityBehaviour _ //makes stuff like doors interacteable [HarmonyPatch(typeof(PlayerActivate), "Update")] - public class PlayerActivatePatch : MonoBehaviour + public class PlayerActivatePatch : MonoBehaviour { [HarmonyPrefix] static void Prefix(PlayerActivate __instance) @@ -924,11 +942,11 @@ static void Prefix(PlayerActivate __instance) //InputManager.Instance.SetBinding(KeyCode.UpArrow, InputManager.Actions.ToggleConsole, true); //InputManager.Instance.SetBinding(Var.acceptButton, InputManager.Actions.ActivateCenterObject, true); //InputManager.Instance.SetBinding(Var.cancelButton, InputManager.Actions.Inventory, true); - + AccessTools.Field(typeof(PlayerActivate), "mainCamera").SetValue(__instance, Var.handCam); } - + } //Sets reference for later use [HarmonyPatch(typeof(ClimbingMotor), "Start")] @@ -949,7 +967,7 @@ public class WeaponManagerStartPatch public class BowPatch:MonoBehaviour { [HarmonyPostfix] - static void Prefix(WeaponManager __instance) + static void Prefix(WeaponManager __instance) { #region Fields PlayerEntity playerEntity =(PlayerEntity)AccessTools.Field(typeof(WeaponManager), "playerEntity").GetValue(__instance); @@ -963,7 +981,7 @@ static void Prefix(WeaponManager __instance) { if (Input.GetKeyDown(Var.indexButton)) { - + DaggerfallMissile missile = Instantiate(__instance.ArrowMissilePrefab); if (missile) { @@ -984,7 +1002,7 @@ static void Prefix(WeaponManager __instance) } } } } - + } } //Grants skill points whenever the player hits an enemy @@ -994,7 +1012,7 @@ public class ExperiencePatch : MonoBehaviour [HarmonyPostfix] static void Postfix(WeaponManager __instance) { - + PlayerEntity playerEntity = (PlayerEntity)AccessTools.Field(typeof(WeaponManager), "playerEntity").GetValue(__instance); DaggerfallUnityItem currentRightHandWeapon = (DaggerfallUnityItem)AccessTools.Field(typeof(WeaponManager), "currentRightHandWeapon").GetValue(__instance); DaggerfallUnityItem currentLeftHandWeapon = (DaggerfallUnityItem)AccessTools.Field(typeof(WeaponManager), "currentLeftHandWeapon").GetValue(__instance); @@ -1009,68 +1027,285 @@ static void Postfix(WeaponManager __instance) playerEntity.TallySkill(DFCareer.Skills.CriticalStrike, 1); //Plugin.LoggerInstance.LogInfo("Experience probably granted"); } - + } // - [HarmonyPatch(typeof(WeaponManager), "ToggleSheath")] - public class CorrectWeaponPatch : MonoBehaviour - { - [HarmonyPrefix] - static void Prefix(WeaponManager __instance) - { - if (Var.weaponObject != null) - Destroy(Var.weaponObject); + //[HarmonyPatch(typeof(WeaponManager), "ToggleSheath")] + //public class CorrectWeaponPatch : MonoBehaviour + //{ + // [HarmonyPostfix] + // internal static void Postfix(WeaponManager __instance) + // { + // if (GameManager.Instance == null) + // { + // Plugin.LoggerInstance.LogError("GameManager is null"); + // return; + // } + // if (GameManager.Instance.PlayerEntity == null) + // { + // Plugin.LoggerInstance.LogError("PlayerEntity is null"); + // return; + // } + // if (GameManager.Instance.PlayerEntity.ItemEquipTable == null) + // { + // Plugin.LoggerInstance.LogError("ItemEquipTable is null"); + // return; + // } - if (__instance.ScreenWeapon == null) - { - Var.currentWeaponName = 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]; - else - currentHandObject = Var.handObjects[__instance.ScreenWeapon.WeaponType]; + // if (leftHandItem == null) + // Var.currentLeftWeaponName = "rHandClosed"; + // else + // Var.currentLeftWeaponName = leftHandItem.LongName; + + // if (rightHandItem == null) + // Var.currentRightWeaponName = "rHandClosed"; + // else + // Var.currentRightWeaponName = rightHandItem.LongName; + + // HandObject currentLeftHandObject = null; + // if (Var.currentLeftWeaponName != null) + // { + // var weaponType = leftHandItem?.GetWeaponType() ?? WeaponTypes.Melee; + + // 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; - GameObject tempObject = currentHandObject.gameObject; + // WeaponCollision weaponCollision = Var.leftWeaponObject.GetComponent(); + // if (weaponCollision != null && leftHandItem != null) + // weaponCollision.item = leftHandItem; + // } + + // HandObject currentRightHandObject = null; + // if (Var.currentRightWeaponName != null) + // { + // var weaponType = rightHandItem?.GetWeaponType() ?? WeaponTypes.Melee; - Var.weaponObject = Instantiate(tempObject); - Var.weaponObject.GetComponent().enabled = true; + // if (weaponType != WeaponTypes.Bow && Var.handObjectsByName.ContainsKey(Var.currentRightWeaponName)) + // currentRightHandObject = Var.handObjectsByName[Var.currentRightWeaponName]; + // else + // currentRightHandObject = Var.handObjects[weaponType]; + // if (currentRightHandObject == null) + // { + // Plugin.LoggerInstance.LogError("Current right hand object is null"); + // return; + // } - // if it is unsheathing - if (__instance.Sheathed) + // Var.rightWeaponObject = Instantiate(currentRightHandObject.gameObject); + // if (Var.rightWeaponObject == null) + // { + // Plugin.LoggerInstance.LogError("Right weapon object is null after instantiation"); + // return; + // } + + // Collider collider = Var.rightWeaponObject.GetComponent(); + // if (collider != null) + // collider.enabled = true; + + // WeaponCollision weaponCollision = Var.rightWeaponObject.GetComponent(); + // if (weaponCollision != null && rightHandItem != null) + // weaponCollision.item = rightHandItem; + // } + + // if (__instance.Sheathed) + // { + // 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; + // } + + // 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 leftItemName = Weapon.GetWeaponName(leftHandItem); + string rightItemName = Weapon.GetWeaponName(rightHandItem); + + if (leftItemName == Var.currentLeftWeaponName && rightItemName == Var.currentRightWeaponName) + return; + + Plugin.LoggerInstance.LogError($"ApplyWeapon called with: '{leftItemName}' and '{rightItemName}'"); + + if (leftItemName != Var.currentLeftWeaponName) { - Var.sheathObject.GetComponent().enabled = true; + if (ChangeWeaponItem(Var.currentLeftWeaponName, leftHandItem, leftItemName, true)) + Var.currentLeftWeaponName = leftItemName; + else + Plugin.LoggerInstance.LogError("Failed to change left weapon item"); + } - if (Var.leftHanded) - Var.weaponObject.transform.SetParent(Var.leftHand.transform); + if (rightItemName != Var.currentRightWeaponName) + { + if (ChangeWeaponItem(Var.currentRightWeaponName, rightHandItem, rightItemName, false)) + Var.currentRightWeaponName = rightItemName; else - Var.weaponObject.transform.SetParent(Var.rightHand.transform); + Plugin.LoggerInstance.LogError("Failed to change right weapon item"); + } + } - Var.weaponObject.transform.localPosition = currentHandObject.unsheatedPositionOffset; - Var.weaponObject.transform.localRotation = currentHandObject.unsheatedRotationOffset; - Var.weaponObject.SetActive(true); + private static bool ChangeWeaponItem(string lastWeaponName, DaggerfallUnityItem handItem, string itemName, bool offHandPreffered) + { + var currentOffHandItemName = Weapon.GetWeaponName(Hands.offHandLabel.weaponItem); + var currentMainHandItemName = Weapon.GetWeaponName(Hands.mainHandLabel.weaponItem); + var currentMainSheathedItemName = Weapon.GetWeaponName(Hands.mainHandSheathController.weaponItem); + var currentOffSheathedItemName = Weapon.GetWeaponName(Hands.offHandSheathController.weaponItem); + + var newWeaponObject = Weapon.GetWeaponObjectForHandObject(handItem, out HandObject newHandObject); + if (newHandObject == null || newWeaponObject == null) + return false; - Hands.rHand.SetActive(false); - Hands.lHand.SetActive(false); + if (Hands.mainHandSheathController.isWeaponSheathed && currentMainSheathedItemName == lastWeaponName) + { + Hands.mainHandSheathController.ReplaceWeapon(handItem, newWeaponObject, newHandObject); + } + else if (Hands.mainHandLabel.weaponObject != null && currentMainHandItemName == lastWeaponName) + { + Hands.mainHandLabel.SetWeapon(handItem, newWeaponObject, newHandObject); } + else if (Hands.offHandSheathController.isWeaponSheathed && currentOffSheathedItemName == lastWeaponName) + { + Hands.offHandSheathController.ReplaceWeapon(handItem, newWeaponObject, newHandObject); + } + else if (Hands.offHandLabel.weaponObject != null && currentOffHandItemName == lastWeaponName) + { + Hands.offHandLabel.SetWeapon(handItem, newWeaponObject, newHandObject); + } + // here is if we couldn't find the weapon anywhere + // this can happen when starting the game else { - Var.weaponObject.GetComponent().enabled = false; - Var.weaponObject.transform.SetParent(Var.sheathObject.transform); - - Var.weaponObject.transform.localPosition = currentHandObject.sheatedPositionOffset; - Var.weaponObject.transform.localRotation = currentHandObject.sheatedRotationOffset; - Var.sheathObject.GetComponent().enabled = currentHandObject.renderSheated; - - Hands.rHand.SetActive(true); - Hands.lHand.SetActive(true); + Plugin.LoggerInstance.LogInfo("Couldn't find Weapon to replace. Sheath it is!"); + if (offHandPreffered && Hands.offHandSheathController.weaponItem == null) + { + Plugin.LoggerInstance.LogInfo("Used offhand sheath"); + Hands.offHandSheathController.ReplaceWeapon(handItem, newWeaponObject, newHandObject); + } + else if (!offHandPreffered && Hands.mainHandSheathController.weaponItem == null) + { + Plugin.LoggerInstance.LogInfo("Used mainhand sheath"); + Hands.mainHandSheathController.ReplaceWeapon(handItem, newWeaponObject, newHandObject); + } + else + { + Plugin.LoggerInstance.LogInfo("No sheath used!"); + } } + + return true; } } @@ -1081,7 +1316,7 @@ public class UnitOrientationPatch : MonoBehaviour [HarmonyPrefix] static void Prefix(DaggerfallMobileUnit __instance) { - + AccessTools.Field(typeof(DaggerfallMobileUnit), "mainCamera").SetValue(__instance, Var.VRCamera); } @@ -1096,7 +1331,7 @@ static void Prefix(DaggerfallMobileUnit __instance) public class GroundedMovementPatch : MonoBehaviour { [HarmonyPrefix] - static bool Prefix(FrictionMotor __instance, ref Vector3 moveDirection) + static bool Prefix(FrictionMotor __instance, ref Vector3 moveDirection) { #region Fields bool sliding = (bool)AccessTools.Field(typeof(FrictionMotor), "sliding").GetValue(__instance); @@ -1107,7 +1342,7 @@ static bool Prefix(FrictionMotor __instance, ref Vector3 moveDirection) //SetSliding(); AccessTools.Method(typeof(FrictionMotor), "SetSliding").Invoke(__instance, null); - + if ((sliding && __instance.slideWhenOverSlopeLimit) || (__instance.slideOnTaggedObjects && hit.collider.tag == "Slide")) { Vector3 hitNormal = hit.normal; @@ -1117,7 +1352,7 @@ static bool Prefix(FrictionMotor __instance, ref Vector3 moveDirection) AccessTools.Field(typeof(FrictionMotor), "playerControl").SetValue(__instance, false); playerControl = false; } - + else { @@ -1157,7 +1392,7 @@ static bool Prefix(FrictionMotor __instance, ref Vector3 moveDirection) } //Plugin.LoggerInstance.LogInfo($"sliding: {sliding}, hit: {hit}, playerControl: {playerControl}, moveDirection: {moveDirection}"); return false; - + } } @@ -1169,7 +1404,7 @@ public class FootstepPatch public static float cooldown = 0.1f; private static float lastStepTime=0; [HarmonyPrefix] - static bool Prefix(PlayerFootsteps __instance) + static bool Prefix(PlayerFootsteps __instance) { if (Time.time - lastStepTime < cooldown) { @@ -1178,15 +1413,15 @@ static bool Prefix(PlayerFootsteps __instance) lastStepTime = Time.time; return true; - + } } //fixes the automap orientation. Automap is currently not bound to any key though [HarmonyPatch(typeof(Automap), "UpdateAutomapStateOnWindowPush")] - public class AutomapPatch : MonoBehaviour + public class AutomapPatch : MonoBehaviour { [HarmonyPrefix] - static void Prefix(Automap __instance) + static void Prefix(Automap __instance) { #region Fields GameObject gameObjectPlayerAdvanced = (GameObject)AccessTools.Field(typeof(Automap), "gameObjectPlayerAdvanced").GetValue(__instance); @@ -1220,8 +1455,8 @@ static void Prefix(Automap __instance) //UpdateSlicingPositionY(); } - - + + } [HarmonyPatch(typeof(HUDCompass),"Update")] @@ -1566,7 +1801,7 @@ public class MousePatch : MonoBehaviour static bool Prefix(PlayerMouseLook __instance) { return false; - + } } //extra bindings because VR controllers don't have a lot of buttons @@ -1576,7 +1811,7 @@ public class BindingPatch : MonoBehaviour //private static bool isCrouching = false; [HarmonyPrefix] - + static void Prefix(InputManager __instance) { if (!ControllerPatch.isChanging) @@ -1587,11 +1822,11 @@ static void Prefix(InputManager __instance) //float inputY = Input.GetAxis(Var.rThumbStickVertical); var rightHand = InputDevices.GetDeviceAtXRNode(XRNode.RightHand); //if (Var.leftHanded) { rightHand = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand); } - + Vector2 rThumbStick; rightHand.TryGetFeatureValue(UnityEngine.XR.CommonUsages.primary2DAxis, out rThumbStick); - + float inputX = rThumbStick.x; float inputY = rThumbStick.y; if (!Var.climbingMotor.IsClimbing && !ControllerPatch.flag) @@ -1664,12 +1899,12 @@ static void Prefix(InputManager __instance) //if (Var.leftHanded) { leftHand = InputDevices.GetDeviceAtXRNode(XRNode.RightHand); } //else //{ - //leftHand = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand); + //leftHand = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand); //} bool gripButton; if (Var.leftHanded) { leftHand.TryGetFeatureValue(CommonUsages.gripButton, out gripButton); } else - { + { rightHand.TryGetFeatureValue(CommonUsages.gripButton, out gripButton); } if (gripButton && !ControllerPatch.flag&&!Var.climbingMotor.IsClimbing) @@ -1817,7 +2052,7 @@ static void Prefix(InputManager __instance) currentActions.Add(Actions.Transport); } else if (leftTrigger) { - currentActions.Add(Actions.CharacterSheet); + currentActions.Add(Actions.CharacterSheet); } @@ -1836,7 +2071,7 @@ static void Prefix(InputManager __instance) { currentActions.Add(Actions.Inventory); } - if (secondaryButton && !ControllerPatch.flag) + if (secondaryButton && !ControllerPatch.flag) { currentActions.Add(Actions.Escape); } @@ -1867,7 +2102,7 @@ public class UIPatch : MonoBehaviour static void Prefix(UserInterfaceRenderTarget __instance) { #region Fields - + int customWidth = (int)AccessTools.Field(typeof(UserInterfaceRenderTarget), "customWidth").GetValue(__instance); int customHeight = (int)AccessTools.Field(typeof(UserInterfaceRenderTarget), "customHeight").GetValue(__instance); Vector2 targetSize = (Vector2)AccessTools.Field(typeof(UserInterfaceRenderTarget), "targetSize").GetValue(__instance); @@ -1880,40 +2115,40 @@ static void Prefix(UserInterfaceRenderTarget __instance) int height = (customHeight == 0) ? Screen.height : customHeight; targetSize = new Vector2(width, height); - + float scaleX = (float)Screen.width / (float)customWidth; float scaleY = (float)Screen.height / (float)customHeight; parentPanel.RootSize = targetSize; parentPanel.Scale = new Vector2(scaleX, scaleY); parentPanel.AutoSize = AutoSizeModes.None; - + bool isReady = (bool)AccessTools.Method(typeof(UserInterfaceRenderTarget), "IsReady").Invoke(__instance, null); - + if (!isReady || targetTexture.width != width || targetTexture.height != height) { - + RenderTexture rTexture = GameObject.Find("DaggerfallUI").GetComponent().sTxx; - + targetTexture = rTexture; targetTexture.filterMode = filterMode; targetTexture.name = string.Format("DaggerfallUI RenderTexture {0}", createCount++); targetTexture.Create(); - + AccessTools.Field(typeof(UserInterfaceRenderTarget), "targetTexture").SetValue(__instance, targetTexture); AccessTools.Field(typeof(UserInterfaceRenderTarget), "createCount").SetValue(__instance, createCount); - + //Plugin.LoggerInstance.LogInfo($"Created UI RenderTexture with dimensions {width}, {height}"); - + if (__instance.OutputImage) __instance.OutputImage.texture = targetTexture; - + AccessTools.Method(typeof(UserInterfaceRenderTarget), "RaiseOnCreateTargetTexture").Invoke(__instance, null); } } @@ -1954,7 +2189,7 @@ void Awake() //Plugin.LoggerInstance.LogInfo("Joysticks:"+joystickNames.Length); Var.Initialize(); - + //Time.fixedDeltaTime = 1f / 80f; @@ -1962,7 +2197,7 @@ void Awake() } - + } 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..0261c07 --- /dev/null +++ b/DFUVR/SheathController.cs @@ -0,0 +1,231 @@ +using BepInEx; +using DaggerfallWorkshop.Game.Items; +using System; +using System.IO; +using UnityEngine; +using UnityEngine.XR; + +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; + + public GameObject weaponObj; + public DaggerfallUnityItem weaponItem; + public bool isWeaponSheathed = true; + + 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; + } + + // set initial state + SheathWeapon(isOffHandSheath ? Hands.offHandLabel : Hands.mainHandLabel, null); + } + + //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; + ToggleSheath(hand); + } + } + } + + private void MirrorSheathPosition() + { + Vector3 localPos = sphere.transform.localPosition; + localPos.x = -localPos.x; + sphere.transform.localPosition = localPos; + } + + private void ToggleSheath(HandLabel hand) + { + Plugin.LoggerInstance.LogInfo("ToggleSheath"); + + if (isWeaponSheathed && hand.weaponObject == null) + UnSheathWeapon(hand); + else if (!isWeaponSheathed && hand.weaponObject != null) + SheathWeapon(hand, hand.weaponItem); + } + + private void UnSheathWeapon(HandLabel hand) + { + if (weaponObj != null) + Destroy(weaponObj); + weaponObj = null; + + var newWeaponObj = Weapon.GetWeaponObjectForHandObject(weaponItem, out HandObject currentHandObject); + if (currentHandObject == null || newWeaponObj == null) + return; + + if (newWeaponObj != null) + { + + hand.SetWeapon(weaponItem, newWeaponObj, currentHandObject); + } + + if (sheathOB != null) + { + MeshRenderer meshRenderer = sheathOB.GetComponent(); + if (meshRenderer != null) + meshRenderer.enabled = true; + } + + isWeaponSheathed = false; + weaponItem = null; + + hand.freeHandObject.SetActive(false); + } + + private void SheathWeapon(HandLabel hand, DaggerfallUnityItem weaponItem) + { + // can't sheath if we have something on the sheath already + if (weaponObj != null) + return; + + weaponObj = Weapon.GetWeaponObjectForHandObject(weaponItem, out HandObject currentHandObject); + if (currentHandObject == null || weaponObj == null) + return; + + SetWeapon(weaponItem, currentHandObject); + + hand.RemoveHeldWeapon(); + + hand.freeHandObject.SetActive(true); + } + + /// + /// This method will replace the weapon that is currently sheathed, if any, by the one sent. + /// The object has to have been created already. Use SheathWeapon if not. + /// + public void ReplaceWeapon(DaggerfallUnityItem weaponItem, GameObject weaponObject, HandObject handObject) + { + if (weaponObj != null) + Destroy(weaponObj); + + weaponObj = weaponObject; + + SetWeapon(weaponItem, handObject); + + weaponObj.SetActive(true); + } + + private void SetWeapon(DaggerfallUnityItem weaponItem, HandObject currentHandObject) + { + Collider collider = weaponObj.GetComponent(); + if (collider != null) + collider.enabled = false; + + if (sheathOB != null) + weaponObj.transform.SetParent(sheathOB.transform); + + weaponObj.transform.localPosition = currentHandObject.sheatedPositionOffset; + weaponObj.transform.localRotation = currentHandObject.sheatedRotationOffset; + + if (sheathOB != null) + { + MeshRenderer meshRenderer = sheathOB.GetComponent(); + if (meshRenderer != null) + meshRenderer.enabled = currentHandObject.renderSheated; + } + + isWeaponSheathed = true; + this.weaponItem = weaponItem; + } + } +} 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..21f2d3f 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 @@ -15,6 +16,15 @@ public class Hands:MonoBehaviour { public static GameObject rHand; public static GameObject lHand; + + public static SheathController mainHandSheathController; + public static SheathController offHandSheathController; + + public static HandLabel leftHandLabel; + public static HandLabel rightHandLabel; + public static HandLabel offHandLabel { get { return Var.leftHanded ? rightHandLabel : leftHandLabel; } } + public static HandLabel mainHandLabel { get { return Var.leftHanded ? leftHandLabel : rightHandLabel; } } + public static void Spawn() { //creating Hands @@ -25,27 +35,33 @@ 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; + leftHandLabel = Var.leftHand.AddComponent(); + rightHandLabel = Var.rightHand.AddComponent(); + if (Var.leftHanded) + { + rightHandLabel.Init(false, XRNode.RightHand, InputDevices.GetDeviceAtXRNode(XRNode.RightHand), Var.gripButton); + leftHandLabel.Init(true, XRNode.LeftHand, InputDevices.GetDeviceAtXRNode(XRNode.LeftHand), Var.lGripButton); + } + else + { + rightHandLabel.Init(true, XRNode.RightHand, InputDevices.GetDeviceAtXRNode(XRNode.RightHand), Var.gripButton); + leftHandLabel.Init(false, XRNode.LeftHand, InputDevices.GetDeviceAtXRNode(XRNode.LeftHand), Var.lGripButton); } - + //Rigidbody rHandBody=Var.rightHand.AddComponent(); //Rigidbody lHandBody = Var.leftHand.AddComponent(); @@ -59,8 +75,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); @@ -75,6 +91,8 @@ public static void Spawn() lHand.transform.localPosition = new Vector3(0, 0, 0); lHand.transform.Rotate(0, 180, 0); + rightHandLabel.freeHandObject = rHand; + leftHandLabel.freeHandObject = lHand; if (Var.leftHanded) { Var.handCam = Var.leftHand.AddComponent(); } else { Var.handCam = Var.rightHand.AddComponent(); } @@ -83,8 +101,11 @@ public static void Spawn() Var.body = new GameObject("Body"); Var.body.AddComponent(); - Var.body.AddComponent(); - assetBundle.Unload(false); + mainHandSheathController = Var.body.AddComponent(); + mainHandSheathController.isOffHandSheath = false; + offHandSheathController = Var.body.AddComponent(); + offHandSheathController.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