2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
8.2 KiB

5 years ago
  1. using Unity.FPS.Game;
  2. using UnityEngine;
  3. namespace Unity.FPS.Gameplay
  4. {
  5. public class PlayerInputHandler : MonoBehaviour
  6. {
  7. [Tooltip("Sensitivity multiplier for moving the camera around")]
  8. public float LookSensitivity = 1f;
  9. [Tooltip("Additional sensitivity multiplier for WebGL")]
  10. public float WebglLookSensitivityMultiplier = 0.25f;
  11. [Tooltip("Limit to consider an input when using a trigger on a controller")]
  12. public float TriggerAxisThreshold = 0.4f;
  13. [Tooltip("Used to flip the vertical input axis")]
  14. public bool InvertYAxis = false;
  15. [Tooltip("Used to flip the horizontal input axis")]
  16. public bool InvertXAxis = false;
  17. GameFlowManager m_GameFlowManager;
  18. PlayerCharacterController m_PlayerCharacterController;
  19. bool m_FireInputWasHeld;
  20. void Start()
  21. {
  22. m_PlayerCharacterController = GetComponent<PlayerCharacterController>();
  23. DebugUtility.HandleErrorIfNullGetComponent<PlayerCharacterController, PlayerInputHandler>(
  24. m_PlayerCharacterController, this, gameObject);
  25. m_GameFlowManager = FindObjectOfType<GameFlowManager>();
  26. DebugUtility.HandleErrorIfNullFindObject<GameFlowManager, PlayerInputHandler>(m_GameFlowManager, this);
  27. Cursor.lockState = CursorLockMode.Locked;
  28. Cursor.visible = false;
  29. }
  30. void LateUpdate()
  31. {
  32. m_FireInputWasHeld = GetFireInputHeld();
  33. }
  34. public bool CanProcessInput()
  35. {
  36. return Cursor.lockState == CursorLockMode.Locked && !m_GameFlowManager.GameIsEnding;
  37. }
  38. public Vector3 GetMoveInput()
  39. {
  40. if (CanProcessInput())
  41. {
  42. Vector3 move = new Vector3(Input.GetAxisRaw(GameConstants.k_AxisNameHorizontal), 0f,
  43. Input.GetAxisRaw(GameConstants.k_AxisNameVertical));
  44. // constrain move input to a maximum magnitude of 1, otherwise diagonal movement might exceed the max move speed defined
  45. move = Vector3.ClampMagnitude(move, 1);
  46. return move;
  47. }
  48. return Vector3.zero;
  49. }
  50. public float GetLookInputsHorizontal()
  51. {
  52. return GetMouseOrStickLookAxis(GameConstants.k_MouseAxisNameHorizontal,
  53. GameConstants.k_AxisNameJoystickLookHorizontal);
  54. }
  55. public float GetLookInputsVertical()
  56. {
  57. return GetMouseOrStickLookAxis(GameConstants.k_MouseAxisNameVertical,
  58. GameConstants.k_AxisNameJoystickLookVertical);
  59. }
  60. public bool GetJumpInputDown()
  61. {
  62. if (CanProcessInput())
  63. {
  64. return Input.GetButtonDown(GameConstants.k_ButtonNameJump);
  65. }
  66. return false;
  67. }
  68. public bool GetJumpInputHeld()
  69. {
  70. if (CanProcessInput())
  71. {
  72. return Input.GetButton(GameConstants.k_ButtonNameJump);
  73. }
  74. return false;
  75. }
  76. public bool GetFireInputDown()
  77. {
  78. return GetFireInputHeld() && !m_FireInputWasHeld;
  79. }
  80. public bool GetFireInputReleased()
  81. {
  82. return !GetFireInputHeld() && m_FireInputWasHeld;
  83. }
  84. public bool GetFireInputHeld()
  85. {
  86. if (CanProcessInput())
  87. {
  88. bool isGamepad = Input.GetAxis(GameConstants.k_ButtonNameGamepadFire) != 0f;
  89. if (isGamepad)
  90. {
  91. return Input.GetAxis(GameConstants.k_ButtonNameGamepadFire) >= TriggerAxisThreshold;
  92. }
  93. else
  94. {
  95. return Input.GetButton(GameConstants.k_ButtonNameFire);
  96. }
  97. }
  98. return false;
  99. }
  100. public bool GetAimInputHeld()
  101. {
  102. if (CanProcessInput())
  103. {
  104. bool isGamepad = Input.GetAxis(GameConstants.k_ButtonNameGamepadAim) != 0f;
  105. bool i = isGamepad
  106. ? (Input.GetAxis(GameConstants.k_ButtonNameGamepadAim) > 0f)
  107. : Input.GetButton(GameConstants.k_ButtonNameAim);
  108. return i;
  109. }
  110. return false;
  111. }
  112. public bool GetSprintInputHeld()
  113. {
  114. if (CanProcessInput())
  115. {
  116. return Input.GetButton(GameConstants.k_ButtonNameSprint);
  117. }
  118. return false;
  119. }
  120. public bool GetCrouchInputDown()
  121. {
  122. if (CanProcessInput())
  123. {
  124. return Input.GetButtonDown(GameConstants.k_ButtonNameCrouch);
  125. }
  126. return false;
  127. }
  128. public bool GetCrouchInputReleased()
  129. {
  130. if (CanProcessInput())
  131. {
  132. return Input.GetButtonUp(GameConstants.k_ButtonNameCrouch);
  133. }
  134. return false;
  135. }
  136. public bool GetReloadButtonDown()
  137. {
  138. if (CanProcessInput())
  139. {
  140. return Input.GetButtonDown(GameConstants.k_ButtonReload);
  141. }
  142. return false;
  143. }
  144. public int GetSwitchWeaponInput()
  145. {
  146. if (CanProcessInput())
  147. {
  148. bool isGamepad = Input.GetAxis(GameConstants.k_ButtonNameGamepadSwitchWeapon) != 0f;
  149. string axisName = isGamepad
  150. ? GameConstants.k_ButtonNameGamepadSwitchWeapon
  151. : GameConstants.k_ButtonNameSwitchWeapon;
  152. if (Input.GetAxis(axisName) > 0f)
  153. return -1;
  154. else if (Input.GetAxis(axisName) < 0f)
  155. return 1;
  156. else if (Input.GetAxis(GameConstants.k_ButtonNameNextWeapon) > 0f)
  157. return -1;
  158. else if (Input.GetAxis(GameConstants.k_ButtonNameNextWeapon) < 0f)
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. public int GetSelectWeaponInput()
  164. {
  165. if (CanProcessInput())
  166. {
  167. if (Input.GetKeyDown(KeyCode.Alpha1))
  168. return 1;
  169. else if (Input.GetKeyDown(KeyCode.Alpha2))
  170. return 2;
  171. else if (Input.GetKeyDown(KeyCode.Alpha3))
  172. return 3;
  173. else if (Input.GetKeyDown(KeyCode.Alpha4))
  174. return 4;
  175. else if (Input.GetKeyDown(KeyCode.Alpha5))
  176. return 5;
  177. else if (Input.GetKeyDown(KeyCode.Alpha6))
  178. return 6;
  179. else if (Input.GetKeyDown(KeyCode.Alpha7))
  180. return 7;
  181. else if (Input.GetKeyDown(KeyCode.Alpha8))
  182. return 8;
  183. else if (Input.GetKeyDown(KeyCode.Alpha9))
  184. return 9;
  185. else
  186. return 0;
  187. }
  188. return 0;
  189. }
  190. float GetMouseOrStickLookAxis(string mouseInputName, string stickInputName)
  191. {
  192. if (CanProcessInput())
  193. {
  194. // Check if this look input is coming from the mouse
  195. bool isGamepad = Input.GetAxis(stickInputName) != 0f;
  196. float i = isGamepad ? Input.GetAxis(stickInputName) : Input.GetAxisRaw(mouseInputName);
  197. // handle inverting vertical input
  198. if (InvertYAxis)
  199. i *= -1f;
  200. // apply sensitivity multiplier
  201. i *= LookSensitivity;
  202. if (isGamepad)
  203. {
  204. // since mouse input is already deltaTime-dependant, only scale input with frame time if it's coming from sticks
  205. i *= Time.deltaTime;
  206. }
  207. else
  208. {
  209. // reduce mouse input amount to be equivalent to stick movement
  210. i *= 0.01f;
  211. #if UNITY_WEBGL
  212. // Mouse tends to be even more sensitive in WebGL due to mouse acceleration, so reduce it even more
  213. i *= WebglLookSensitivityMultiplier;
  214. #endif
  215. }
  216. return i;
  217. }
  218. return 0f;
  219. }
  220. }
  221. }