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