PlayerController.cs 7.2 KB

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