You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

402 lines
17 KiB
C#

4 years ago
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using SiegeSong;
namespace SiegeSong
{
public class InputManager : MonoBehaviour
4 years ago
{
public bool InputModeGamepad = true;
public Motor Motor;
public CameraManager CameraManager;
4 years ago
public InventoryManager InventoryManager;
public Selector Selector;
public ActorLoader loader;
public CommandConsole CommandConsole;
public Dictionary<string, bool> HandleButtonInputOldState;
public Dictionary<string, bool> HandleButtonChordedPressed;
public Dictionary<string, bool> HandleButtonDoublePressStarted;
public Dictionary<string, bool> HandleButtonTimerActive;
4 years ago
public Dictionary<string, bool> HandleButtonChordedTimerActive;
public Dictionary<string, bool> HandleButtonInputInUse;
public Dictionary<string, float> HandleButtonTimer;
public Dictionary<string, float> 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;
4 years ago
void Start()
{
}
public InputDirection GetInputDirection(bool leftStick = false)
{
var stickVerticalGamepad = Input.GetAxis(leftStick ? runGamepadInput : rotateCameraVerticalGamepadInput);
var stickHorizontalGamepad = Input.GetAxis(leftStick ? strafeGamepadInput : rotateCameraHorizontalGamepadInput);
4 years ago
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);
4 years ago
var changeCameraGamepad = Input.GetAxis(changeCameraGamepadInput);
4 years ago
var activateGamepadInput = Input.GetButton(this.activateGamepadInput);
var dropGamepadInput = Input.GetButton(this.dropGamepadInput);
var openMenuGamepadInput = Input.GetButton(menuOpenGamepadInput);
var crouchState = Input.GetButton(cancelGamepadInput);
4 years ago
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);
4 years ago
if (!InventoryManager.ActivateInputNewState && activateGamepadInput && !oldActivateState && Motor.UsingStation && Motor.TargetStation != null && Selector.Target == null)
Motor.StopUsingStation(Motor.TargetStation);
4 years ago
}
if (runVertical > inputMin || runVertical < -inputMin)
4 years ago
{
Motor.Run(runVertical);
4 years ago
shouldRotate = true;
}
else
{
Motor.Run(0);
4 years ago
}
if (runHorizontal != 0)
{
Motor.Strafe(runHorizontal);
4 years ago
shouldRotate = true;
}
else
{
Motor.Strafe(0);
4 years ago
}
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)
4 years ago
InventoryManager.UpArrowInputNewState = true;
else
InventoryManager.UpArrowInputNewState = false;
if (menuVertical < -inputMin && !zooming)
4 years ago
InventoryManager.DownArrowInputNewState = true;
else
InventoryManager.DownArrowInputNewState = false;
if (!zooming && !(runVertical > inputMin || runVertical < -inputMin) && !(runHorizontal > inputMin || runHorizontal < -inputMin))
Motor.Stop();
4 years ago
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)
4 years ago
CameraManager.RotateVertical(cameraVerticalKeyboad);
if (cameraHorizontalKeyboad > inputMin || cameraHorizontalKeyboad < -inputMin)
4 years ago
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);
4 years ago
shouldRotate = true;
}
if (runBackward && !changeCameraKeyboad && !zooming)
{
Motor.Run(-1);
4 years ago
shouldRotate = true;
}
if (strafeLeft && !zooming)
{
Motor.Strafe(-1);
4 years ago
shouldRotate = true;
}
if (strafeRight && !zooming)
{
Motor.Strafe(1);
4 years ago
shouldRotate = true;
}
if (!runForward && !runBackward && !strafeLeft && !strafeRight)
Motor.Stop();
4 years ago
}
if (shouldRotate || (CameraManager.FirstPersonActive && CameraManager.CameraX != 0.0f))
4 years ago
{
CameraManager.RealignCamera(true);
if (CameraManager.FirstPersonActive && CameraManager.CameraX != 0.0f)
Motor.Strafe(0, true);
4 years ago
}
}
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<string, bool>();
if (HandleButtonChordedPressed == null)
HandleButtonChordedPressed = new Dictionary<string, bool>();
if (HandleButtonDoublePressStarted == null)
HandleButtonDoublePressStarted = new Dictionary<string, bool>();
if (HandleButtonTimerActive == null)
HandleButtonTimerActive = new Dictionary<string, bool>();
if (HandleButtonChordedTimerActive == null)
HandleButtonChordedTimerActive = new Dictionary<string, bool>();
if (HandleButtonInputInUse == null)
HandleButtonInputInUse = new Dictionary<string, bool>();
if (HandleButtonTimer == null)
HandleButtonTimer = new Dictionary<string, float>();
if (HandleButtonChordedTimer == null)
HandleButtonChordedTimer = new Dictionary<string, float>();
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;
4 years ago
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;
}
}
}
}
}