From 7664efa96bc52530b766ab3cc409afda68973e15 Mon Sep 17 00:00:00 2001 From: RedBigz Date: Sun, 17 Nov 2024 14:12:31 +1000 Subject: [PATCH] Add weapon switching --- README.md | 2 +- TABGVR/Player/VRControls.cs | 46 ++++++++++++++++++++++++++++++++++--- TABGVR/Util/MathUtil.cs | 9 ++++++++ 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 TABGVR/Util/MathUtil.cs diff --git a/README.md b/README.md index 33d0cea..0d13d15 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,4 @@ ## Controller Layout ![Controller Layout](auxillary/controller_layout.svg) -(NOTE: Snap Turn, Weapon Switching, Grenades, and Fire Mode are not implemented yet!) \ No newline at end of file +(NOTE: Snap Turn, Grenades, and Fire Mode are not implemented yet!) \ No newline at end of file diff --git a/TABGVR/Player/VRControls.cs b/TABGVR/Player/VRControls.cs index 8aea19d..d639e7e 100644 --- a/TABGVR/Player/VRControls.cs +++ b/TABGVR/Player/VRControls.cs @@ -1,10 +1,8 @@ +using System; using JetBrains.Annotations; -using Landfall.TABG.UI; using TABGVR.Util; using UnityEngine; -using UnityEngine.EventSystems; using UnityEngine.XR; -using UnityEngine.XR.Interaction.Toolkit; namespace TABGVR.Player; @@ -12,6 +10,7 @@ public class VRControls : MonoBehaviour { internal const float TriggerDeadZone = 0.7f; internal const float StopSprintingThreshold = 0.1f; + internal const float SwapWeaponThreshold = 0.8f; private bool _aButtonPressed; private bool _bButtonPressed; @@ -23,6 +22,9 @@ public class VRControls : MonoBehaviour private bool _menuButtonPressed; + private bool _weaponUpPressed; + private bool _weaponDownPressed; + [CanBeNull] private Pickup currentPickup; private HaxInput haxInput; private InputHandler inputHandler; @@ -48,6 +50,30 @@ private void Start() inputHandler.enabled = false; } + private void SwapWeaponViaOffset(int offset) + { + if (weaponHandler.CurrentWeapon == Pickup.EquipSlots.ThrowableSlot) return; // TODO: Implement grenades + + if (weaponHandler.CurrentWeapon == Pickup.EquipSlots.None) + { + weaponHandler.CurrentWeapon = offset >= 0 + ? Pickup.EquipSlots.WeaponSlot01 + : Pickup.EquipSlots.WeaponSlot03; + + return; + } + + for (var i = 0; i < MathUtil.CanonicalMod(offset, 3); i++) + { + weaponHandler.CurrentWeapon = weaponHandler.CurrentWeapon switch + { + Pickup.EquipSlots.WeaponSlot01 => Pickup.EquipSlots.WeaponSlot02, + Pickup.EquipSlots.WeaponSlot02 => Pickup.EquipSlots.WeaponSlot03, + Pickup.EquipSlots.WeaponSlot03 => Pickup.EquipSlots.WeaponSlot01, + }; + } + } + private void Update() { // update interactor visibility @@ -73,6 +99,7 @@ private void Update() Controllers.LeftHandXR.TryGetFeatureValue(CommonUsages.menuButton, out var menuButtonPressed); + // Menu if (menuButtonPressed && !_menuButtonPressed) { var menuTransitions = InventoryUI.instance.gameObject.GetComponent(); @@ -91,11 +118,13 @@ private void Update() _menuButtonPressed = menuButtonPressed; + // Sprinting if (leftClick && !inputHandler.isSpringting) inputHandler.isSpringting = true; inputHandler.isSpringting &= leftJoystick.magnitude > StopSprintingThreshold; + // Right Trigger if (rightTrigger > TriggerDeadZone) { if (!_rightTriggered) @@ -109,6 +138,7 @@ private void Update() } } + // Left Trigger if (leftTrigger > TriggerDeadZone) { if (!_leftTriggered) @@ -125,6 +155,7 @@ private void Update() _rightTriggered = rightTrigger > TriggerDeadZone; _leftTriggered = leftTrigger > TriggerDeadZone; + // Right Click if (rightClick) weaponHandler.CurrentWeapon = Pickup.EquipSlots.None; @@ -145,6 +176,15 @@ private void Update() _bButtonPressed = bButton; _xButtonPressed = xButton; _yButtonPressed = yButton; + + var weaponUpPressed = rightJoystick.y >= SwapWeaponThreshold; + var weaponDownPressed = rightJoystick.y <= -SwapWeaponThreshold; + + if (weaponUpPressed && !_weaponUpPressed) SwapWeaponViaOffset(-1); + if (weaponDownPressed && !_weaponDownPressed) SwapWeaponViaOffset(1); + + _weaponUpPressed = weaponUpPressed; + _weaponDownPressed = weaponDownPressed; } /// diff --git a/TABGVR/Util/MathUtil.cs b/TABGVR/Util/MathUtil.cs new file mode 100644 index 0000000..0784b3a --- /dev/null +++ b/TABGVR/Util/MathUtil.cs @@ -0,0 +1,9 @@ +namespace TABGVR.Util; + +public static class MathUtil +{ + public static int CanonicalMod(int a, int b) + { + return ((a % b) + b) % b; + } +} \ No newline at end of file