| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | public class InputManager : MonoBehaviour { |
| 0 | 7 | | public static InputManager Instance { get; private set; } |
| | 8 | |
|
| 0 | 9 | | public bool mouseActive = true; |
| | 10 | | [System.Serializable] |
| | 11 | | public struct CameraPreset { |
| | 12 | | public Vector3 position; |
| | 13 | | public Quaternion rotation; |
| | 14 | | } |
| | 15 | |
|
| 0 | 16 | | public bool lockUserInput = false; |
| | 17 | |
|
| 0 | 18 | | private void Awake() { |
| 0 | 19 | | if (Instance == null) { |
| 0 | 20 | | Instance = this; |
| 0 | 21 | | DontDestroyOnLoad(gameObject); |
| 0 | 22 | | } else { |
| 0 | 23 | | Destroy(gameObject); |
| 0 | 24 | | } |
| 0 | 25 | | } |
| | 26 | |
|
| | 27 | | // Start is called before the first frame update |
| 0 | 28 | | void Start() {} |
| | 29 | |
|
| | 30 | | // Update is called once per frame |
| 0 | 31 | | void Update() { |
| 0 | 32 | | HandleInput(); |
| 0 | 33 | | } |
| | 34 | |
|
| 0 | 35 | | private void HandleMouseInput() { |
| 0 | 36 | | if (Input.GetMouseButton(0)) { |
| 0 | 37 | | CameraController.Instance.OrbitCamera(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); |
| | 38 | |
|
| 0 | 39 | | } else if (Input.GetMouseButton(1)) { |
| 0 | 40 | | CameraController.Instance.RotateCamera(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y")); |
| 0 | 41 | | } |
| 0 | 42 | | } |
| | 43 | |
|
| 0 | 44 | | private void HandleInput() { |
| 0 | 45 | | if (!lockUserInput) { |
| 0 | 46 | | HandleLockableInput(); |
| 0 | 47 | | } |
| 0 | 48 | | HandleNonLockableInput(); |
| 0 | 49 | | } |
| | 50 | |
|
| 0 | 51 | | void HandleScrollWheelInput() { |
| 0 | 52 | | if (Input.GetAxis("Mouse ScrollWheel") != 0) { |
| 0 | 53 | | CameraController.Instance.ZoomCamera(Input.GetAxis("Mouse ScrollWheel") * 500); |
| 0 | 54 | | } |
| 0 | 55 | | } |
| | 56 | |
|
| 0 | 57 | | void HandleLockableInput() { |
| 0 | 58 | | if (mouseActive) { |
| 0 | 59 | | HandleMouseInput(); |
| 0 | 60 | | HandleScrollWheelInput(); |
| 0 | 61 | | } |
| | 62 | |
|
| 0 | 63 | | if (Input.GetKey(KeyCode.LeftShift)) { |
| 0 | 64 | | CameraController.Instance.SetCameraSpeed(CameraController.Instance.GetCameraSpeedMax()); |
| 0 | 65 | | } else { |
| 0 | 66 | | CameraController.Instance.SetCameraSpeed(CameraController.Instance.GetCameraSpeedNormal()); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | // TRANSLATIONAL MOVEMENT |
| 0 | 70 | | if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) { |
| 0 | 71 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Forward); |
| 0 | 72 | | } |
| 0 | 73 | | if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) { |
| 0 | 74 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Left); |
| 0 | 75 | | } |
| 0 | 76 | | if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) { |
| 0 | 77 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Back); |
| 0 | 78 | | } |
| 0 | 79 | | if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) { |
| 0 | 80 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Right); |
| 0 | 81 | | } |
| 0 | 82 | | if (Input.GetKey(KeyCode.Q)) { |
| 0 | 83 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Up); |
| 0 | 84 | | } |
| 0 | 85 | | if (Input.GetKey(KeyCode.E)) { |
| 0 | 86 | | CameraController.Instance.TranslateCamera(CameraController.TranslationInput.Down); |
| 0 | 87 | | } |
| 0 | 88 | | } |
| | 89 | |
|
| 0 | 90 | | void HandleNonLockableInput() { |
| 0 | 91 | | if (Input.GetKeyDown(KeyCode.Escape)) { |
| 0 | 92 | | Application.Quit(); |
| 0 | 93 | | } |
| | 94 | |
|
| 0 | 95 | | if (Input.GetKeyDown(KeyCode.C)) { |
| 0 | 96 | | } |
| | 97 | |
|
| 0 | 98 | | if (Input.GetKeyDown(KeyCode.R)) { |
| 0 | 99 | | SimManager.Instance.RestartSimulation(); |
| 0 | 100 | | } |
| | 101 | |
|
| 0 | 102 | | if (Input.GetKeyDown(KeyCode.L)) // 'L' for Load |
| 0 | 103 | | { |
| 0 | 104 | | UIManager.Instance.ToggleConfigSelectorPanel(); |
| 0 | 105 | | } |
| | 106 | |
|
| 0 | 107 | | if (Input.GetKeyDown(KeyCode.Space)) { |
| | 108 | | // Pause the time |
| 0 | 109 | | if (!SimManager.Instance.IsSimulationPaused()) { |
| 0 | 110 | | SimManager.Instance.PauseSimulation(); |
| 0 | 111 | | } else { |
| 0 | 112 | | SimManager.Instance.ResumeSimulation(); |
| 0 | 113 | | } |
| 0 | 114 | | } |
| | 115 | |
|
| 0 | 116 | | if (Input.GetKeyDown(KeyCode.P)) { |
| 0 | 117 | | CameraController.Instance.SetAutoRotate(!CameraController.Instance.IsAutoRotate()); |
| 0 | 118 | | } |
| | 119 | |
|
| 0 | 120 | | if (Input.GetKeyDown(KeyCode.Alpha1)) { |
| 0 | 121 | | if (Input.GetKey(KeyCode.LeftControl)) { |
| 0 | 122 | | CameraController.Instance.FollowNextInterceptorSwarm(); |
| 0 | 123 | | } else { |
| 0 | 124 | | CameraController.Instance.SnapToNextInterceptorSwarm(); |
| 0 | 125 | | } |
| 0 | 126 | | } |
| | 127 | |
|
| 0 | 128 | | if (Input.GetKeyDown(KeyCode.Alpha2)) { |
| 0 | 129 | | if (Input.GetKey(KeyCode.LeftControl)) { |
| 0 | 130 | | CameraController.Instance.FollowNextThreatSwarm(); |
| 0 | 131 | | } else { |
| 0 | 132 | | CameraController.Instance.SnapToNextThreatSwarm(); |
| 0 | 133 | | } |
| 0 | 134 | | } |
| | 135 | |
|
| 0 | 136 | | if (Input.GetKeyDown(KeyCode.Alpha3)) { |
| 0 | 137 | | if (Input.GetKey(KeyCode.LeftControl)) { |
| 0 | 138 | | CameraController.Instance.FollowCenterAllAgents(); |
| 0 | 139 | | } else { |
| 0 | 140 | | CameraController.Instance.SnapToCenterAllAgents(); |
| 0 | 141 | | } |
| 0 | 142 | | } |
| | 143 | |
|
| 0 | 144 | | if (Input.GetKeyDown(KeyCode.Alpha4)) { |
| | 145 | | // Set pre-set view 4 |
| 0 | 146 | | } |
| | 147 | |
|
| 0 | 148 | | if (Input.GetKeyDown(KeyCode.Alpha5)) { |
| | 149 | | // Set pre-set view 5 |
| 0 | 150 | | } |
| | 151 | |
|
| 0 | 152 | | if (Input.GetKeyDown(KeyCode.Alpha6)) { |
| | 153 | | // Set pre-set view 6 |
| 0 | 154 | | } |
| 0 | 155 | | } |
| | 156 | | } |