using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.Events; using SiegeSong; namespace SiegeSong { public class InputManager : MonoBehaviour { public bool InputModeGamepad = true; public Motor Motor; public CameraManager CameraManager; public InventoryManager InventoryManager; public Selector Selector; public ActorLoader loader; public CommandConsole CommandConsole; public Dictionary HandleButtonInputOldState; public Dictionary HandleButtonChordedPressed; public Dictionary HandleButtonDoublePressStarted; public Dictionary HandleButtonTimerActive; public Dictionary HandleButtonChordedTimerActive; public Dictionary HandleButtonInputInUse; public Dictionary HandleButtonTimer; public Dictionary HandleButtonChordedTimer; private string RunForwardKeyboardInput = "w"; private string RunBackwardKeyboardInput = "s"; private string StrafeLeftKeyboardInput = "a"; private string StrafeRightKeyboardInput = "d"; private string ChangeCameraKeyboardInput = "f"; private string RotateCameraVerticalKeyboardInput = "Mouse Y"; private string RotateCameraHorizontalKeyboardInput = "Mouse X"; private string menuOpenGamepadInput = "Back"; private string menuVerticalGamepadInput = "D-Pad Vertical"; private string menuHorizontalGamepadInput = "D-Pad Horizontal"; private string activateGamepadInput = "A"; private string cancelGamepadInput = "B"; private string dropGamepadInput = "X"; private string gamePadUseRightInput = "RT"; private string gamePadUseLeftInput = "LT"; private string gamePadSwitchLeftInput = "LB"; private string gamePadSwitchRightInput = "RB"; private string runGamepadInput = "LeftAnalogVertical"; private string strafeGamepadInput = "LeftAnalogHorizontal"; private string rotateCameraVerticalGamepadInput = "RightAnalogVertical"; private string rotateCameraHorizontalGamepadInput = "RightAnalogHorizontal"; private string changeCameraGamepadInput = "RightStickClick"; private float inputMin = 0.01f; private float longPressTime = 0.4f; private float doublePressDelayTime = 0.1f; private bool oldActivateState; private bool oldMenuState; private bool oldCameraState; private bool oldDropState; private 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(this.activateGamepadInput); var dropGamepadInput = Input.GetButton(this.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, Motor.UseRight, null, null, null, null, true); HandleButtonInput(gamePadUseLeftInput, Motor.UseLeft, null, null, null, null, true); HandleButtonInput(this.dropGamepadInput, Motor.UseAbility, null, null, null, null, true); HandleButtonInput(gamePadSwitchRightInput, Motor.NextRightTechnique, Motor.NextRightEquip); HandleButtonInput(gamePadSwitchLeftInput, Motor.NextLeftTechnique, Motor.NextLeftEquip, Motor.NextAbilityEquip, Motor.NextAbilityTechnique, gamePadSwitchRightInput); HandleButtonInput(changeCameraGamepadInput, CameraManager.SwitchCamera, null, null, null, null, true); if (!InventoryManager.ActivateInputNewState && activateGamepadInput && !oldActivateState && Motor.UsingStation && Motor.TargetStation != null && Selector.Target == null) Motor.StopUsingStation(Motor.TargetStation); } if (runVertical > inputMin || runVertical < -inputMin) { Motor.Run(runVertical); shouldRotate = true; } else { Motor.Run(0); } if (runHorizontal != 0) { Motor.Strafe(runHorizontal); shouldRotate = true; } else { Motor.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)) Motor.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) { Motor.Run(1); shouldRotate = true; } if (runBackward && !changeCameraKeyboad && !zooming) { Motor.Run(-1); shouldRotate = true; } if (strafeLeft && !zooming) { Motor.Strafe(-1); shouldRotate = true; } if (strafeRight && !zooming) { Motor.Strafe(1); shouldRotate = true; } if (!runForward && !runBackward && !strafeLeft && !strafeRight) Motor.Stop(); } if (shouldRotate || (CameraManager.FirstPersonActive && CameraManager.CameraX != 0.0f)) { CameraManager.RealignCamera(true); if (CameraManager.FirstPersonActive && CameraManager.CameraX != 0.0f) Motor.Strafe(0, true); } } 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; } } } } }