PlayerController.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. enteringDoor=false;
  77. }
  78. if(insideDoor && !enteringDoor && Input.GetKey(InputManager.data().interactingKey)){
  79. Debug.Log("Exiting door");
  80. transform.position = SceneData.netSceneData.door.transform.position;
  81. insideDoor=false;
  82. enteringDoor=true;
  83. if(GetComponent<NetPlayer>()!=null){GetComponent<NetPlayer>().CallChangeInsideDoor(insideDoor);}
  84. }
  85. if (listenToInput)
  86. {
  87. if (isGrounded)
  88. {
  89. moveInput = HorizontalAxis;
  90. }
  91. else
  92. {
  93. //Change moveInput while in-air | IF there is an input
  94. if (Input.GetKey(InputManager.data().leftInput) || Input.GetKey(InputManager.data().rightInput))
  95. {
  96. moveInput = Mathf.Lerp(moveInput, HorizontalAxis, 0.2f);
  97. }
  98. }
  99. //Enter the door
  100. if(inDoor && Input.GetKey(InputManager.data().interactingKey) && !enteringDoor){
  101. if(SceneData.netSceneData.doorExit!=null){
  102. Debug.Log("Entering door");
  103. transform.position = SceneData.netSceneData.doorExit.position;
  104. insideDoor = true;
  105. enteringDoor=true;
  106. if(GetComponent<NetPlayer>()!=null){GetComponent<NetPlayer>().CallChangeInsideDoor(insideDoor);}
  107. }
  108. }
  109. }
  110. if (isSwimming || inWater) { moveInput = moveInput / 2f; }
  111. //Update moving value on Animation
  112. animator.SetBool("moving", (moveInput != 0));
  113. animator.SetBool("isSwimming", isSwimming);
  114. //Flip Image to face moving Direction
  115. if (moveInput < 0)
  116. {
  117. sprite.flipX = (invertSprite) ? false : true;
  118. }
  119. else if (moveInput > 0)
  120. {
  121. sprite.flipX = (invertSprite) ? true : false;
  122. }
  123. //Apply moving input to player
  124. if(GetComponent<NetPlayer>()!=null){
  125. if(GetComponent<NetPlayer>().frndTrans!=null){
  126. rigidbody.velocity = GetComponent<NetPlayer>().frndTrans.GetComponent<Rigidbody2D>().velocity;
  127. }
  128. }
  129. rigidbody.transform.Translate(new Vector2(moveSpeed * moveInput, 0));
  130. bool _canJump = canJump();
  131. if ((Input.GetKey(InputManager.data().jumpInput)) && _canJump)
  132. {
  133. jumpT = 0;
  134. jumpReleased = false;
  135. rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
  136. if(jumpSFX!=null)AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
  137. }
  138. //Blocks continous jump button
  139. if (!Input.GetKey(InputManager.data().jumpInput)) { jumpReleased = true; }
  140. //Apply Jump to player
  141. if (jumpT < jumpDuration)
  142. {
  143. jumpT += Time.deltaTime;
  144. float progress = (jumpDuration - jumpT) / jumpDuration;
  145. //|| jumpT < jumpDuration/2f
  146. if ((Input.GetKey(InputManager.data().jumpInput)) || b) { rigidbody.AddForce(new Vector2(0, jumpForce * progress)); }
  147. }
  148. else { b = false; }
  149. _isSwimming = inWater;
  150. }
  151. bool b;
  152. bool _isSwimming;
  153. public bool waterBoost;
  154. public bool canJump()
  155. {
  156. return jumpT >= jumpDuration && isGrounded && listenToInput && jumpReleased && !isSwimming;//&& !GameManager.isPaused
  157. }
  158. public bool canBotJump()
  159. {
  160. return isGrounded || isSwimming || inWater;
  161. }
  162. //External Player Control Exposure
  163. public void Jump()
  164. {
  165. jumpT = 0;
  166. rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
  167. b = true;
  168. AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
  169. }
  170. public void move(float input)
  171. {
  172. input = Mathf.Clamp(input, -1, 1);
  173. moveInput = input;
  174. }
  175. void OnLand()
  176. {
  177. // Debug.Log("Landed Like a boss!");
  178. }
  179. void OnFly()
  180. {
  181. // Debug.Log("Hey look im flying");
  182. }
  183. public bool getGrounded()
  184. {
  185. //return (Physics2D.Linecast(transform.position, groundChecker.position, groundLayerMask));
  186. Collider2D col = GetComponentInChildren<Collider2D>();
  187. 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, groundLayerMask));
  188. }
  189. // public bool getInWater(){Collider2D col = GetComponentInChildren<Collider2D>();
  190. // 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));
  191. // }
  192. }
  193. // public static class SettingsInstance{
  194. // public static Settings instance;
  195. // }
  196. public static class AudioSingleton{
  197. private static AudioSource music;
  198. private static AudioSource sfx;
  199. public static AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
  200. public static AudioSource getMusicSource(){
  201. if(music == null){
  202. GameObject go = new GameObject("Music Audio Source");
  203. go.AddComponent<AudioSource>();
  204. music = go.GetComponent<AudioSource>();
  205. music.outputAudioMixerGroup = mixer.FindMatchingGroups("Music")[0];
  206. }
  207. return music;
  208. }
  209. public static AudioSource getSFXSource(){
  210. //AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
  211. if(sfx == null){
  212. GameObject go = new GameObject("SFX Audio Source");
  213. go.AddComponent<AudioSource>();
  214. sfx= go.GetComponent<AudioSource>();
  215. Debug.Log("sfx : " + (sfx==null).ToString() + ", mixer: " + (mixer == null).ToString() );
  216. sfx.outputAudioMixerGroup = mixer.FindMatchingGroups("SFX")[0];
  217. }
  218. return sfx;
  219. }
  220. }