PlayerController.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Mirror;
  4. using UnityEngine;
  5. using UnityEngine.Audio;
  6. public class PlayerController : NetworkBehaviour
  7. {
  8. public Rigidbody2D rigidbody;
  9. public SpriteRenderer sprite;
  10. public bool invertSprite;
  11. public Animator animator;
  12. public LayerMask groundLayerMask;
  13. // public float drownedDepth;
  14. public bool isGrounded = true;
  15. public bool isSwimming = false;
  16. public Transform groundChecker;
  17. public Vector2 gravity;
  18. public float buoyantForce;
  19. public float buoyantSpd;
  20. public float moveSpeed = 1;
  21. public float jumpForce = 10;
  22. public float jumpDuration = 0.2f;
  23. public AudioClip jumpSFX;
  24. float jumpT = 0;
  25. public bool listenToInput = true;
  26. public bool inDoor = false;
  27. public bool insideDoor = false;
  28. bool enteringDoor = false;
  29. void Start()
  30. {
  31. jumpT = jumpDuration;
  32. }
  33. bool _grounded = false;
  34. float moveInput = 0;
  35. bool jumpReleased = true;
  36. [HideInInspector]
  37. public bool inWater;
  38. void FixedUpdate()
  39. {
  40. isGrounded = getGrounded();
  41. if (_grounded != isGrounded)
  42. {
  43. if (isGrounded)
  44. {
  45. OnLand();
  46. }
  47. else
  48. {
  49. OnFly();
  50. }
  51. _grounded = isGrounded;
  52. }
  53. if (isSwimming)
  54. {
  55. rigidbody.velocity = new Vector2(rigidbody.velocity.x, Mathf.Lerp(rigidbody.velocity.y, buoyantForce, buoyantSpd));
  56. if ((Input.GetKey(InputManager.data().jumpInput)))
  57. {
  58. rigidbody.velocity = new Vector2(rigidbody.velocity.x, jumpForce / 12f);
  59. }
  60. }
  61. else
  62. {
  63. gravity = new Vector2(gravity.x, 0);
  64. }
  65. //Gravity Application
  66. rigidbody.AddForce(gravity);
  67. //Update In-Air value on animation
  68. animator.SetBool("inAir", !isGrounded);
  69. animator.SetBool("isOnWater", inWater);
  70. float HorizontalAxis = 0;
  71. if (Input.GetKey(InputManager.data().leftInput)) { HorizontalAxis = -1; } else if (Input.GetKey(InputManager.data().rightInput)) { HorizontalAxis = 1; }
  72. // if (GameManager.isPaused) { HorizontalAxis = 0; }
  73. //Move according to input
  74. //Exit the door
  75. if (enteringDoor && !Input.GetKey(InputManager.data().interactingKey))
  76. {
  77. enteringDoor = false;
  78. }
  79. if (insideDoor && !enteringDoor && Input.GetKey(InputManager.data().interactingKey))
  80. {
  81. Debug.Log("Exiting door");
  82. transform.position = SceneData.netSceneData.door.transform.position;
  83. insideDoor = false;
  84. enteringDoor = true;
  85. if (GetComponent<NetPlayer>() != null) { GetComponent<NetPlayer>().CallChangeInsideDoor(insideDoor); }
  86. }
  87. if (listenToInput)
  88. {
  89. if (isGrounded)
  90. {
  91. moveInput = HorizontalAxis;
  92. }
  93. else
  94. {
  95. //Change moveInput while in-air | IF there is an input
  96. if (Input.GetKey(InputManager.data().leftInput) || Input.GetKey(InputManager.data().rightInput))
  97. {
  98. moveInput = Mathf.Lerp(moveInput, HorizontalAxis, 0.2f);
  99. }
  100. }
  101. //Enter the door
  102. if (inDoor && Input.GetKey(InputManager.data().interactingKey) && !enteringDoor)
  103. {
  104. if (SceneData.netSceneData.doorExit != null)
  105. {
  106. Debug.Log("Entering door");
  107. transform.position = SceneData.netSceneData.doorExit.position;
  108. insideDoor = true;
  109. enteringDoor = true;
  110. if (GetComponent<NetPlayer>() != null) { GetComponent<NetPlayer>().CallChangeInsideDoor(insideDoor); }
  111. }
  112. }
  113. }
  114. if (isSwimming || inWater) { moveInput = moveInput / 2f; }
  115. //Update moving value on Animation
  116. animator.SetBool("moving", (moveInput != 0));
  117. animator.SetBool("isSwimming", isSwimming);
  118. //Flip Image to face moving Direction
  119. if (moveInput < 0)
  120. {
  121. sprite.flipX = (invertSprite) ? false : true;
  122. }
  123. else if (moveInput > 0)
  124. {
  125. sprite.flipX = (invertSprite) ? true : false;
  126. }
  127. //Apply moving input to player
  128. // if (GetComponent<NetPlayer>() != null)
  129. // {
  130. // if (GetComponent<NetPlayer>().parentFrnd != null)
  131. // {
  132. // rigidbody.velocity = GetComponent<NetPlayer>().parentFrnd.GetComponent<Rigidbody2D>().velocity;
  133. // }
  134. // }
  135. rigidbody.transform.Translate(new Vector2(moveSpeed * moveInput, 0));
  136. bool _canJump = canJump();
  137. if ((Input.GetKey(InputManager.data().jumpInput)) && _canJump)
  138. {
  139. jumpT = 0;
  140. jumpReleased = false;
  141. rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
  142. if (jumpSFX != null) AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
  143. }
  144. //Blocks continous jump button
  145. if (!Input.GetKey(InputManager.data().jumpInput)) { jumpReleased = true; }
  146. //Apply Jump to player
  147. if (jumpT < jumpDuration)
  148. {
  149. jumpT += Time.deltaTime;
  150. float progress = (jumpDuration - jumpT) / jumpDuration;
  151. //|| jumpT < jumpDuration/2f
  152. if ((Input.GetKey(InputManager.data().jumpInput)) || b) { rigidbody.AddForce(new Vector2(0, jumpForce * progress)); }
  153. }
  154. else { b = false; }
  155. _isSwimming = inWater;
  156. }
  157. bool b;
  158. bool _isSwimming;
  159. public bool waterBoost;
  160. public bool canJump()
  161. {
  162. return jumpT >= jumpDuration && isGrounded && listenToInput && jumpReleased && !isSwimming && transform.childCount <= 0;//&& !GameManager.isPaused
  163. }
  164. public bool canBotJump()
  165. {
  166. return isGrounded || isSwimming || inWater;
  167. }
  168. //External Player Control Exposure
  169. public void Jump()
  170. {
  171. jumpT = 0;
  172. rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
  173. b = true;
  174. AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
  175. }
  176. public void move(float input)
  177. {
  178. input = Mathf.Clamp(input, -1, 1);
  179. moveInput = input;
  180. }
  181. void OnLand()
  182. {
  183. // Debug.Log("Landed Like a boss!");
  184. }
  185. void OnFly()
  186. {
  187. // Debug.Log("Hey look im flying");
  188. }
  189. public float groundCheckerDist = 0.1f;
  190. public float groundCheckerHeighMultipler = 1f;
  191. public bool getGrounded()
  192. {
  193. //return (Physics2D.Linecast(transform.position, groundChecker.position, groundLayerMask));
  194. Collider2D col = GetComponentInChildren<Collider2D>();
  195. return (Physics2D.BoxCast(col.bounds.center, new Vector2(col.bounds.size.x - (col.bounds.size.x / 5f), col.bounds.size.y * groundCheckerHeighMultipler), 0, Vector2.down, groundCheckerDist, groundLayerMask));
  196. }
  197. void OnDrawGizmos()
  198. {
  199. Collider2D col = GetComponentInChildren<Collider2D>();
  200. Gizmos.color = Color.red;
  201. Gizmos.DrawWireCube(col.bounds.center - new Vector3(0, groundCheckerDist), new Vector2(col.bounds.size.x - (col.bounds.size.x / 5f), col.bounds.size.y * groundCheckerHeighMultipler));
  202. }
  203. // public bool getInWater(){Collider2D col = GetComponentInChildren<Collider2D>();
  204. // return (Physics2D.BoxCast(col.bounds.center, new Vector2(col.bounds.size.x - (col.bounds.size.x / 5f), col.bounds.size.y), 0, Vector2.down, 0.1f, waterLayerMask));
  205. // }
  206. }
  207. // public static class SettingsInstance{
  208. // public static Settings instance;
  209. // }
  210. public static class AudioSingleton
  211. {
  212. private static AudioSource music;
  213. private static AudioSource sfx;
  214. public static AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
  215. public static AudioSource getMusicSource()
  216. {
  217. if (music == null)
  218. {
  219. GameObject go = new GameObject("Music Audio Source");
  220. go.AddComponent<AudioSource>();
  221. music = go.GetComponent<AudioSource>();
  222. music.outputAudioMixerGroup = mixer.FindMatchingGroups("Music")[0];
  223. }
  224. return music;
  225. }
  226. public static AudioSource getSFXSource()
  227. {
  228. //AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
  229. if (sfx == null)
  230. {
  231. GameObject go = new GameObject("SFX Audio Source");
  232. go.AddComponent<AudioSource>();
  233. sfx = go.GetComponent<AudioSource>();
  234. Debug.Log("sfx : " + (sfx == null).ToString() + ", mixer: " + (mixer == null).ToString());
  235. sfx.outputAudioMixerGroup = mixer.FindMatchingGroups("SFX")[0];
  236. }
  237. return sfx;
  238. }
  239. }