Skip to content

Commit

Permalink
Add weapon switching
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBigz committed Nov 17, 2024
1 parent b2e9761 commit 7664efa
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
(NOTE: Snap Turn, Grenades, and Fire Mode are not implemented yet!)
46 changes: 43 additions & 3 deletions TABGVR/Player/VRControls.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
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;

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;
Expand All @@ -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;
Expand All @@ -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

Check warning on line 68 in TABGVR/Player/VRControls.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern 'Pickup.EquipSlots.None' is not covered.

Check warning on line 68 in TABGVR/Player/VRControls.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern 'Pickup.EquipSlots.None' is not covered.
{
Pickup.EquipSlots.WeaponSlot01 => Pickup.EquipSlots.WeaponSlot02,
Pickup.EquipSlots.WeaponSlot02 => Pickup.EquipSlots.WeaponSlot03,
Pickup.EquipSlots.WeaponSlot03 => Pickup.EquipSlots.WeaponSlot01,
};
}
}

private void Update()
{
// update interactor visibility
Expand All @@ -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<MenuTransitions>();
Expand All @@ -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)
Expand All @@ -109,6 +138,7 @@ private void Update()
}
}

// Left Trigger
if (leftTrigger > TriggerDeadZone)
{
if (!_leftTriggered)
Expand All @@ -125,6 +155,7 @@ private void Update()
_rightTriggered = rightTrigger > TriggerDeadZone;
_leftTriggered = leftTrigger > TriggerDeadZone;

// Right Click
if (rightClick)
weaponHandler.CurrentWeapon = Pickup.EquipSlots.None;

Expand All @@ -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;
}

/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions TABGVR/Util/MathUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TABGVR.Util;

public static class MathUtil
{
public static int CanonicalMod(int a, int b)
{
return ((a % b) + b) % b;
}
}

0 comments on commit 7664efa

Please sign in to comment.