using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Events; using SiegeSong; namespace SiegeSong { public class PlayerInputManager : MonoBehaviour { public string RunForwardKeyboardInput = "w"; public string RunBackwardKeyboardInput = "s"; public string StrafeLeftKeyboardInput = "a"; public string StrafeRightKeyboardInput = "d"; public string ChangeCameraKeyboardInput = "f"; public string RotateCameraVerticalKeyboardInput = "MouseVerticalAxis"; public string RotateCameraHorizontalKeyboardInput = "MouseHorizontalAxis"; public bool InputModeGamepad = true; public string MenuOpenGamepadInput = "SelectButton"; public string MenuVerticalGamepadInput = "DPadVerticalAxis"; public string MenuHorizontalGamepadInput = "DPadHorizontalAxis"; public string ActivateGamepadInput = "AButton"; public string CancelGamepadInput = "BButton"; public string DropGamepadInput = "XButton"; public string GamePadUseRightInput = "RightTrigger"; public string GamePadUseLeftInput = "LeftTrigger"; public string GamePadSwitchLeftInput = "LeftBumper"; public string GamePadSwitchRightInput = "RightBumper"; public string RunGamepadInput = "LeftAnalogVertical"; public string StrafeGamepadInput = "LeftAnalogHorizontal"; public string RotateCameraVerticalGamepadInput = "RightAnalogVertical"; public string RotateCameraHorizontalGamepadInput = "RightAnalogHorizontal"; public string ChangeCameraGamepadInput = "RightStickClick"; public ActorMotor ActorMotor; public PlayerCameraManager CameraManager; public InventoryManager InventoryManager; public Selector Selector; public float InputMin = 0.01f; public ActorLoader loader; public CommandConsole CommandConsole; public float longPressTime = 0.4f; public float doublePressDelayTime = 0.1f; public Dictionary HandleButtonInputOldState; public Dictionary HandleButtonChordedPressed; public Dictionary HandleButtonDoublePressStarted; public Dictionary HandleButtonTimerActive; public Dictionary HandleButtonChordedTimerActive; public Dictionary HandleButtonInputInUse; public Dictionary HandleButtonTimer; public Dictionary HandleButtonChordedTimer; bool oldActivateState; bool oldMenuState; bool oldCameraState; bool oldDropState; bool oldCrouchState; void Start() { } public InputDirection GetInputDirection(bool leftStick = false) { var stickVerticalGamepad = Input.GetAxis(leftStick ? RunGamepadInput : RotateCameraVerticalGamepadInput); var stickHorizontalGamepad = Input.GetAxis(leftStick ? StrafeGamepadInput : RotateCameraHorizontalGamepadInput); var up = false; var down = false; var left = false; var right = false; if (stickVerticalGamepad > 0) up = true; if (stickVerticalGamepad < 0) down = true; if (stickHorizontalGamepad > 0) left = true; if (stickHorizontalGamepad < 0) right = true; if (up && left) return InputDirection.TopLeft; if (up && right) return InputDirection.TopRight; if (down && right) return InputDirection.BottomRight; if (down && left) return InputDirection.BottomLeft; if (up) return InputDirection.TopMid; if (down) return InputDirection.BottomMid; if (left) return InputDirection.MidLeft; if (right) return InputDirection.MidRight; return InputDirection.MidMid; } void Update() { if (Input.GetKeyDown("`")) { CommandConsole.CommandConsoleOpen = !CommandConsole.CommandConsoleOpen; } var shouldRotate = false; var zooming = false; if (InputModeGamepad) { var cameraHorizontalGamepad = Input.GetAxis(RotateCameraHorizontalGamepadInput); var cameraVerticalGamepad = Input.GetAxis(RotateCameraVerticalGamepadInput); var runVertical = Input.GetAxis(RunGamepadInput); var runHorizontal = Input.GetAxis(StrafeGamepadInput); var menuVertical = Input.GetAxis(MenuVerticalGamepadInput); var menuHorizontal = Input.GetAxis(MenuHorizontalGamepadInput); var changeCameraGamepad = Input.GetAxis(ChangeCameraGamepadInput); var activateGamepadInput = Input.GetButton(ActivateGamepadInput); var dropGamepadInput = Input.GetButton(DropGamepadInput); var openMenuGamepadInput = Input.GetButton(MenuOpenGamepadInput); var crouchState = Input.GetButton(CancelGamepadInput); InventoryManager.OpenInputNewState = !oldMenuState && openMenuGamepadInput; if (InventoryManager.InventoryScreenOpen || InventoryManager.TransferScreenOpen) { if (crouchState && !oldCrouchState) { InventoryManager.CloseInventoryScreen(); InventoryManager.CloseTransferScreen(); } InventoryManager.ActivateInputNewState = !oldActivateState && activateGamepadInput; InventoryManager.DropInputNewState = !oldDropState && dropGamepadInput; } else { if (openMenuGamepadInput && !oldMenuState) InventoryManager.OpenInventoryScreen(); Selector.InteractInputNewState = !oldActivateState && activateGamepadInput; HandleButtonInput(GamePadUseRightInput, ActorMotor.UseRight, null, null, null, null, true); HandleButtonInput(GamePadUseLeftInput, ActorMotor.UseLeft, null, null, null, null, true); HandleButtonInput(DropGamepadInput, ActorMotor.UseAbility, null, null, null, null, true); HandleButtonInput(GamePadSwitchRightInput, ActorMotor.NextRightTechnique, ActorMotor.NextRightEquip); HandleButtonInput(GamePadSwitchLeftInput, ActorMotor.NextLeftTechnique, ActorMotor.NextLeftEquip, ActorMotor.NextAbilityEquip, ActorMotor.NextAbilityTechnique, GamePadSwitchRightInput); HandleButtonInput(ChangeCameraGamepadInput, CameraManager.SwitchCamera, null, null, null, null, true); if (!InventoryManager.ActivateInputNewState && activateGamepadInput && !oldActivateState && ActorMotor.UsingStation && ActorMotor.TargetStation != null && Selector.Target == null) ActorMotor.StopUsingStation(ActorMotor.TargetStation); } if (runVertical > InputMin || runVertical < -InputMin) { ActorMotor.Run(runVertical); shouldRotate = true; } else { ActorMotor.Run(0); } if (runHorizontal != 0) { ActorMotor.Strafe(runHorizontal); shouldRotate = true; } else { ActorMotor.Strafe(0); } if (cameraVerticalGamepad != 0) CameraManager.RotateVertical(cameraVerticalGamepad); else CameraManager.RotateVertical(0); if (cameraHorizontalGamepad != 0) CameraManager.RotateHorizontal(cameraHorizontalGamepad); else CameraManager.RotateHorizontal(0); if (oldCameraState && changeCameraGamepad != 0) { if (runVertical != 0) { zooming = true; if (runVertical > 0) CameraManager.ZoomOut(); else CameraManager.ZoomIn(); } } if (menuVertical > InputMin && !zooming) InventoryManager.UpArrowInputNewState = true; else InventoryManager.UpArrowInputNewState = false; if (menuVertical < -InputMin && !zooming) InventoryManager.DownArrowInputNewState = true; else InventoryManager.DownArrowInputNewState = false; if (!zooming && !(runVertical > InputMin || runVertical < -InputMin) && !(runHorizontal > InputMin || runHorizontal < -InputMin)) ActorMotor.Stop(); oldCameraState = changeCameraGamepad != 0; oldActivateState = activateGamepadInput; oldDropState = dropGamepadInput; oldMenuState = openMenuGamepadInput; oldCrouchState = crouchState; } else { var runForward = Input.GetKey(RunForwardKeyboardInput); var runBackward = Input.GetKey(RunBackwardKeyboardInput); var strafeLeft = Input.GetKey(StrafeLeftKeyboardInput); var strafeRight = Input.GetKey(StrafeRightKeyboardInput); var cameraHorizontalKeyboad = Input.GetAxis(RotateCameraHorizontalKeyboardInput); var cameraVerticalKeyboad = Input.GetAxis(RotateCameraVerticalKeyboardInput); var changeCameraKeyboad = Input.GetKeyDown(ChangeCameraKeyboardInput); if (!changeCameraKeyboad && cameraVerticalKeyboad > InputMin || cameraVerticalKeyboad < -InputMin) CameraManager.RotateVertical(cameraVerticalKeyboad); if (cameraHorizontalKeyboad > InputMin || cameraHorizontalKeyboad < -InputMin) CameraManager.RotateHorizontal(cameraHorizontalKeyboad); if (changeCameraKeyboad) { if (runForward || runBackward) { zooming = true; if (runForward) CameraManager.ZoomOut(); if (runBackward) CameraManager.ZoomIn(); } } if (runForward && !changeCameraKeyboad && !zooming) { ActorMotor.Run(1); shouldRotate = true; } if (runBackward && !changeCameraKeyboad && !zooming) { ActorMotor.Run(-1); shouldRotate = true; } if (strafeLeft && !zooming) { ActorMotor.Strafe(-1); shouldRotate = true; } if (strafeRight && !zooming) { ActorMotor.Strafe(1); shouldRotate = true; } if (!runForward && !runBackward && !strafeLeft && !strafeRight) ActorMotor.Stop(); } if (shouldRotate || (CameraManager.FirstPersonActive && CameraManager.cameraX != 0.0f)) { CameraManager.RealignCamera(true); if (CameraManager.FirstPersonActive && CameraManager.cameraX != 0.0f) ActorMotor.Strafe(0, true); // TODO: need to fix this so that the strafing animation is played when a character rotates in place } } public delegate void ShortPress(); public delegate void LongPress(); public delegate void ChordedShortPress(); public delegate void ChordedLongPress(); public void HandleButtonInput(string inputName, ShortPress shortPress, LongPress longPress = null, ChordedShortPress chordedShortPress = null, ChordedLongPress chordedLongPress = null, string chordedInput = "", bool isAxis = false, bool chordIsAxis = false) { if (HandleButtonInputOldState == null) HandleButtonInputOldState = new Dictionary(); if (HandleButtonChordedPressed == null) HandleButtonChordedPressed = new Dictionary(); if (HandleButtonDoublePressStarted == null) HandleButtonDoublePressStarted = new Dictionary(); if (HandleButtonTimerActive == null) HandleButtonTimerActive = new Dictionary(); if (HandleButtonChordedTimerActive == null) HandleButtonChordedTimerActive = new Dictionary(); if (HandleButtonInputInUse == null) HandleButtonInputInUse = new Dictionary(); if (HandleButtonTimer == null) HandleButtonTimer = new Dictionary(); if (HandleButtonChordedTimer == null) HandleButtonChordedTimer = new Dictionary(); if (!HandleButtonInputOldState.ContainsKey(inputName)) HandleButtonInputOldState.Add(inputName, false); if (!HandleButtonChordedPressed.ContainsKey(inputName)) HandleButtonChordedPressed.Add(inputName, false); if (!HandleButtonDoublePressStarted.ContainsKey(inputName)) HandleButtonDoublePressStarted.Add(inputName, false); if (!HandleButtonTimerActive.ContainsKey(inputName)) HandleButtonTimerActive.Add(inputName, false); if (!HandleButtonChordedTimerActive.ContainsKey(inputName)) HandleButtonChordedTimerActive.Add(inputName, false); if (!HandleButtonInputInUse.ContainsKey(inputName)) HandleButtonInputInUse.Add(inputName, false); if (!HandleButtonTimer.ContainsKey(inputName)) HandleButtonTimer.Add(inputName, 0.0f); if (!HandleButtonChordedTimer.ContainsKey(inputName)) HandleButtonChordedTimer.Add(inputName, 0.0f); if (HandleButtonChordedTimerActive[inputName]) HandleButtonChordedTimer[inputName] += Time.deltaTime; if (HandleButtonTimerActive[inputName]) HandleButtonTimer[inputName] += Time.deltaTime; var inputValue = isAxis ? Input.GetAxis(inputName) > InputMin : Input.GetButton(inputName); var chordedInputValue = chordedInput != "" && chordedInput != null ? (chordIsAxis ? Input.GetAxis(chordedInput) > InputMin : Input.GetButton(chordedInput)) : false; if (inputValue) { if (!HandleButtonInputInUse[inputName]) HandleButtonInputInUse[inputName] = true; if (!chordedInputValue) { if (HandleButtonInputOldState[inputName] == false) { HandleButtonTimer[inputName] = 0; HandleButtonTimerActive[inputName] = true; } HandleButtonInputOldState[inputName] = true; } } else { HandleButtonInputInUse[inputName] = false; if (HandleButtonInputOldState[inputName]) { HandleButtonInputOldState[inputName] = false; HandleButtonTimerActive[inputName] = false; if (HandleButtonTimer[inputName] > longPressTime && longPress != null) { longPress.Invoke(); } else { shortPress.Invoke(); } } if (HandleButtonInputOldState[inputName] && !chordedInputValue) { if (!HandleButtonChordedPressed[inputName]) { if (HandleButtonDoublePressStarted[inputName] && HandleButtonChordedTimer[inputName] < doublePressDelayTime) HandleButtonChordedTimerActive[inputName] = false; HandleButtonChordedTimer[inputName] = 0; HandleButtonChordedTimerActive[inputName] = true; } HandleButtonChordedPressed[inputName] = true; } else { HandleButtonChordedTimerActive[inputName] = false; if (HandleButtonChordedPressed[inputName]) { if (HandleButtonChordedTimer[inputName] > longPressTime && chordedLongPress != null) { chordedLongPress.Invoke(); } else if (chordedShortPress != null) { chordedShortPress.Invoke(); } HandleButtonChordedTimerActive[inputName] = true; HandleButtonDoublePressStarted[inputName] = true; } HandleButtonChordedTimer[inputName] = 0; HandleButtonChordedPressed[inputName] = false; } } } } }