using UnityEngine; using UnityEngine.Events; using UnityEngine.InputSystem; using InputAction = UnityEngine.InputSystem.InputAction; public class InputHandler : MonoBehaviour { #region Data [Space, Header("Input Data")] [SerializeField] private CameraInputData cameraInputData = null; [SerializeField] private CameraController camController = null; [SerializeField] private MovementInputData movementInputData = null; private GamePadInputEvents gamePadEvents; #endregion [SerializeField] private WeaponSway weaponSwayData = null; private bool isSprinting = false; [System.NonSerialized] public bool ToggleAiming = false; [System.NonSerialized] public bool UsingGamePad; [System.NonSerialized] public bool InteractKeyPressed; [System.NonSerialized] public bool PingKeyPressed; [System.NonSerialized] public bool ChatKeyPressed; [System.NonSerialized] public bool DrinkPotionKeyPressed; [System.NonSerialized] public bool EquipGrenadeKeyPressed; [System.NonSerialized] public bool PauseKeyPressed; [System.NonSerialized] public bool ReloadKeyPressed; [System.NonSerialized] public bool AimKeyPressed; [System.NonSerialized] public bool InventoryKeyPressed; [System.NonSerialized] public bool VoiceChatMenuKeyPressed; [System.NonSerialized] private bool SprintKeyPressed; [System.NonSerialized] public bool PushToTalkKeyPressed; [System.NonSerialized] public bool SwapWeaponKeyPressed; [System.NonSerialized] public bool IsXboxController; private PlayerInput input; private InputAction lookAction; private InputAction moveAction; private InputAction weaponSwayAction; [System.NonSerialized] public bool FireKeyPressed; public bool FireKeyWasPressedLastFrame { get; private set; } public bool FireKeyPressedThisFrame => FireKeyPressed && !FireKeyWasPressedLastFrame; [System.NonSerialized] public bool UI_Submit; [System.NonSerialized] public bool UI_Submit_Released; [System.NonSerialized] public bool UI_Cancel; [System.NonSerialized] public bool UI_Cancel_Released; [System.NonSerialized] public bool UI_Drop_Item; public UnityEvent GameStatsOpenEvent = new UnityEvent(); public UnityEvent GameStatsClose = new UnityEvent(); private void Awake() { input = GetComponent(); lookAction = input.actions.FindAction("Look"); moveAction = input.actions.FindAction("Move"); weaponSwayAction = input.actions.FindAction("WeaponSway"); gamePadEvents = GetComponent(); } void Start() { cameraInputData.ResetInput(); movementInputData.ResetInput(); GameStatsOpenEvent.AddListener(HandleOpenGameStatsCanvas); GameStatsClose.AddListener(HandleHideGameStatsCanvas); } void Update() { bool updatePlayer = Player.Instance.updatePlayer; if (Client.NoClipped) return; GetMovementInputData(); HandleLook(updatePlayer); HandleMove(updatePlayer); HandeSway(); gamePadEvents.enabled = UsingGamePad; } private void HandleLook(bool updatePlayer) { Vector2 lookAxis = lookAction.ReadValue(); bool invertedKB = !UsingGamePad && SettingsMenu.Instance.ToggleInvertY; cameraInputData.InputVectorX = updatePlayer ? lookAxis.x : 0; cameraInputData.InputVectorY = updatePlayer ? lookAxis.y : 0; cameraInputData.InputVectorY = invertedKB ? -lookAxis.y : lookAxis.y; } private void HandleMove(bool updatePlayer) { Vector2 moveAxis = moveAction.ReadValue(); movementInputData.InputVectorX = updatePlayer ? moveAxis.x : 0; movementInputData.InputVectorY = updatePlayer ? moveAxis.y : 0; } private void HandeSway() { Vector2 swayAxis = weaponSwayAction.ReadValue(); weaponSwayData.SwayX = swayAxis.x; weaponSwayData.SwayY = swayAxis.y; } private void LateUpdate() { FireKeyWasPressedLastFrame = FireKeyPressed; } public MovementInputData GetMovementData() => movementInputData; public void OnDeviceChanged(PlayerInput input) { if (input.devices.Count <= 0) { UsingGamePad = false; return; } UsingGamePad = !input.devices[0].device.name.Equals("Keyboard"); if (UsingGamePad) { IsXboxController = !input.devices[0].name.Contains("DualShock"); } // foreach (InputDevice device in input.devices) // { // string deviceName = device.name; // Debug.Log("Device Name: " + deviceName); // } } public void OnPause(InputAction.CallbackContext context) { PauseKeyPressed = context.action.WasPressedThisFrame(); } public void OnJump(InputAction.CallbackContext context) { if (Client.NoClipped) return; movementInputData.JumpClicked = context.action.WasPressedThisFrame(); } public void OnInteract(InputAction.CallbackContext context) { if (Client.NoClipped) return; InteractKeyPressed = context.action.WasPressedThisFrame(); } public void OnInventory(InputAction.CallbackContext context) { if (Client.NoClipped) return; InventoryKeyPressed = context.action.WasPressedThisFrame(); } public void OnReload(InputAction.CallbackContext context) { if (Client.NoClipped) return; ReloadKeyPressed = context.action.WasPressedThisFrame(); } public void OnVoiceChatMenu(InputAction.CallbackContext context) { if (Client.NoClipped) return; UIManager.Instance.HandleVoiceTabMenu(context.action.WasPressedThisFrame()); if (context.started) { GameStatsOpenEvent?.Invoke(); } else if (context.canceled) { GameStatsClose?.Invoke(); } } public void OnFire(InputAction.CallbackContext context) { if (Client.NoClipped) return; if (context.started) { FireKeyPressed = true; } else if (context.canceled) { FireKeyPressed = false; } } public void OnHoldAim(InputAction.CallbackContext context) { if (Client.NoClipped) return; if (context.started) { AimKeyPressed = true; } else if (context.canceled) { AimKeyPressed = false; } } public void OnPressAim(InputAction.CallbackContext context) { if (Client.NoClipped) return; if (context.started) { AimKeyPressed = true; } else if (context.canceled) { AimKeyPressed = false; } } public void OnPushToTalk(InputAction.CallbackContext context) { if (Client.NoClipped) return; if (context.started) { PushToTalkKeyPressed = true; } else if (context.canceled) { PushToTalkKeyPressed = false; } } public void OnPing(InputAction.CallbackContext context) { if (Client.NoClipped) return; PingKeyPressed = context.action.WasPressedThisFrame(); } public void OnSprint(InputAction.CallbackContext context) { if (Client.NoClipped) return; SprintKeyPressed = context.action.WasPressedThisFrame(); //Debug.Log("Sprinting!"); } public void OnChat(InputAction.CallbackContext context) { ChatKeyPressed = context.action.WasPressedThisFrame(); } public void OnDrinkPotion(InputAction.CallbackContext context) { if (Client.NoClipped) return; DrinkPotionKeyPressed = context.action.WasPressedThisFrame(); } public void OnEquipGrenade(InputAction.CallbackContext context) { if (Client.NoClipped) return; EquipGrenadeKeyPressed = context.action.WasPressedThisFrame(); } public void OnSwapWeapon(InputAction.CallbackContext context) { if (Client.NoClipped) return; SwapWeaponKeyPressed = context.action.WasPressedThisFrame(); } public void OnUISubmit(InputAction.CallbackContext context) { if (Client.NoClipped) return; UI_Submit = context.action.WasPressedThisFrame(); UI_Submit_Released = context.action.WasReleasedThisFrame(); } public void OnUICancel(InputAction.CallbackContext context) { if (Client.NoClipped) return; UI_Cancel = context.action.WasPressedThisFrame(); UI_Cancel_Released = context.action.WasReleasedThisFrame(); } public void OnUIDropItem(InputAction.CallbackContext context) { if (Client.NoClipped) return; UI_Drop_Item = context.action.WasPressedThisFrame(); } #region Custom Methods private void HandleOpenGameStatsCanvas() { if (Client.NoClipped) return; if (UIManager.Instance.IsInGameInstance) { // PacketManager.sendChatMessage("/showgamestats"); } } private void HandleHideGameStatsCanvas() { if (Client.NoClipped) return; UIManager.Instance.hideInterface("InGameStatsCanvas"); } private void GetMovementInputData() { if (Client.NoClipped) return; FirstPersonController playerController = Player.Instance.playerController; if (movementInputData.InputVectorY == 0 || playerController.CurrentMagnitude < playerController.LowestAllowedMagnitude) { isSprinting = false; movementInputData.RunClicked = false; movementInputData.RunReleased = true; Player.Instance.RunButtonPressed = false; return; } //Keyboard if (!UsingGamePad) { if (SettingsMenu.Instance.ToggleSprintInput && SprintKeyPressed) { SprintKeyPressed = false; isSprinting = !isSprinting; movementInputData.RunClicked = isSprinting; movementInputData.RunReleased = !isSprinting; } else if (!SettingsMenu.Instance.ToggleSprintInput) { movementInputData.RunClicked = SprintKeyPressed; movementInputData.RunReleased = !SprintKeyPressed; } } //Controller if (UsingGamePad) { if (SprintKeyPressed) { movementInputData.RunClicked = true; movementInputData.RunReleased = false; } } if (movementInputData.RunClicked) { movementInputData.runPressed = true; } if (movementInputData.RunReleased) { movementInputData.runPressed = false; } Player.Instance.RunButtonPressed = movementInputData.runPressed; //movementInputData.CrouchClicked = Input.GetKeyDown(KeyCode.LeftControl); } #endregion }