PlayerController.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. void Start()
  27. {
  28. jumpT = jumpDuration;
  29. }
  30. bool _grounded = false;
  31. float moveInput = 0;
  32. bool jumpReleased = true;
  33. [HideInInspector]
  34. public bool inWater;
  35. void FixedUpdate()
  36. {
  37. isGrounded = getGrounded();
  38. if (_grounded != isGrounded)
  39. {
  40. if (isGrounded)
  41. {
  42. OnLand();
  43. }
  44. else
  45. {
  46. OnFly();
  47. }
  48. _grounded = isGrounded;
  49. }
  50. if (isSwimming)
  51. {
  52. rigidbody.velocity = new Vector2(rigidbody.velocity.x, Mathf.Lerp(rigidbody.velocity.y, buoyantForce, buoyantSpd));
  53. if ((Input.GetKey(InputManager.data().jumpInput)))
  54. {
  55. rigidbody.velocity = new Vector2(rigidbody.velocity.x, jumpForce / 12f);
  56. }
  57. }
  58. else
  59. {
  60. gravity = new Vector2(gravity.x, 0);
  61. }
  62. //Gravity Application
  63. rigidbody.AddForce(gravity);
  64. //Update In-Air value on animation
  65. animator.SetBool("inAir", !isGrounded);
  66. animator.SetBool("isOnWater", inWater);
  67. float HorizontalAxis = 0;
  68. if (Input.GetKey(InputManager.data().leftInput)) { HorizontalAxis = -1; } else if (Input.GetKey(InputManager.data().rightInput)) { HorizontalAxis = 1; }
  69. // if (GameManager.isPaused) { HorizontalAxis = 0; }
  70. //Move according to input
  71. if (listenToInput)
  72. {
  73. if (isGrounded)
  74. {
  75. moveInput = HorizontalAxis;
  76. }
  77. else
  78. {
  79. //Change moveInput while in-air | IF there is an input
  80. if (Input.GetKey(InputManager.data().leftInput) || Input.GetKey(InputManager.data().rightInput))
  81. {
  82. moveInput = Mathf.Lerp(moveInput, HorizontalAxis, 0.2f);
  83. }
  84. }
  85. }
  86. if (isSwimming || inWater) { moveInput = moveInput / 2f; }
  87. //Update moving value on Animation
  88. animator.SetBool("moving", (moveInput != 0));
  89. animator.SetBool("isSwimming", isSwimming);
  90. //Flip Image to face moving Direction
  91. if (moveInput < 0)
  92. {
  93. sprite.flipX = (invertSprite) ? false : true;
  94. }
  95. else if (moveInput > 0)
  96. {
  97. sprite.flipX = (invertSprite) ? true : false;
  98. }
  99. //Apply moving input to player
  100. rigidbody.transform.Translate(new Vector2(moveSpeed * moveInput, 0));
  101. bool _canJump = canJump();
  102. if ((Input.GetKey(InputManager.data().jumpInput)) && _canJump)
  103. {
  104. jumpT = 0;
  105. jumpReleased = false;
  106. rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
  107. if(jumpSFX!=null)AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
  108. }
  109. //Blocks continous jump button
  110. if (!Input.GetKey(InputManager.data().jumpInput)) { jumpReleased = true; }
  111. //Apply Jump to player
  112. if (jumpT < jumpDuration)
  113. {
  114. jumpT += Time.deltaTime;
  115. float progress = (jumpDuration - jumpT) / jumpDuration;
  116. //|| jumpT < jumpDuration/2f
  117. if ((Input.GetKey(InputManager.data().jumpInput)) || b) { rigidbody.AddForce(new Vector2(0, jumpForce * progress)); }
  118. }
  119. else { b = false; }
  120. _isSwimming = inWater;
  121. }
  122. bool b;
  123. bool _isSwimming;
  124. public bool waterBoost;
  125. public bool canJump()
  126. {
  127. return jumpT >= jumpDuration && isGrounded && listenToInput && jumpReleased && !isSwimming;//&& !GameManager.isPaused
  128. }
  129. public bool canBotJump()
  130. {
  131. return isGrounded || isSwimming || inWater;
  132. }
  133. //External Player Control Exposure
  134. public void Jump()
  135. {
  136. jumpT = 0;
  137. rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
  138. b = true;
  139. AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
  140. }
  141. public void move(float input)
  142. {
  143. input = Mathf.Clamp(input, -1, 1);
  144. moveInput = input;
  145. }
  146. void OnLand()
  147. {
  148. // Debug.Log("Landed Like a boss!");
  149. }
  150. void OnFly()
  151. {
  152. // Debug.Log("Hey look im flying");
  153. }
  154. public bool getGrounded()
  155. {
  156. //return (Physics2D.Linecast(transform.position, groundChecker.position, groundLayerMask));
  157. Collider2D col = GetComponentInChildren<Collider2D>();
  158. 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));
  159. }
  160. // public bool getInWater(){Collider2D col = GetComponentInChildren<Collider2D>();
  161. // 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));
  162. // }
  163. }
  164. // public static class SettingsInstance{
  165. // public static Settings instance;
  166. // }
  167. public static class AudioSingleton{
  168. private static AudioSource music;
  169. private static AudioSource sfx;
  170. public static AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
  171. public static AudioSource getMusicSource(){
  172. if(music == null){
  173. GameObject go = new GameObject("Music Audio Source");
  174. go.AddComponent<AudioSource>();
  175. music = go.GetComponent<AudioSource>();
  176. music.outputAudioMixerGroup = mixer.FindMatchingGroups("Music")[0];
  177. }
  178. return music;
  179. }
  180. public static AudioSource getSFXSource(){
  181. //AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
  182. if(sfx == null){
  183. GameObject go = new GameObject("SFX Audio Source");
  184. go.AddComponent<AudioSource>();
  185. sfx= go.GetComponent<AudioSource>();
  186. Debug.Log("sfx : " + (sfx==null).ToString() + ", mixer: " + (mixer == null).ToString() );
  187. sfx.outputAudioMixerGroup = mixer.FindMatchingGroups("SFX")[0];
  188. }
  189. return sfx;
  190. }
  191. }