PlayerController.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. public Vector3 neighboursDetectorSize;
  30. void Start()
  31. {
  32. jumpT = jumpDuration;
  33. if(!isServer){
  34. rigidbody.simulated=false;
  35. }
  36. }
  37. [SyncVar]
  38. bool _grounded = false;
  39. bool jumpReleased = true;
  40. [HideInInspector]
  41. public bool inWater;
  42. [SyncVar]
  43. public bool InteractionKeyPressed;
  44. [SyncVar]
  45. public bool JumpKeyPressed;
  46. [SyncVar]
  47. public float HorizontalAxis;
  48. public float moveInput = 0;
  49. [Command]
  50. void CmdUpdateInput(float _horizontal, bool jump, bool interact){
  51. HorizontalAxis = _horizontal;
  52. JumpKeyPressed=jump;
  53. InteractionKeyPressed=interact;
  54. }
  55. void ListenInput(){
  56. HorizontalAxis=0;
  57. if (Input.GetKey(InputManager.data().leftInput)) { HorizontalAxis = -1; } else if (Input.GetKey(InputManager.data().rightInput)) { HorizontalAxis = 1; }
  58. JumpKeyPressed = Input.GetKey(InputManager.data().jumpInput);
  59. InteractionKeyPressed = Input.GetKey(InputManager.data().interactingKey);
  60. }
  61. void FixedUpdate()
  62. {
  63. if(!isServer){
  64. if(isLocalPlayer){
  65. //Command Inputs to server
  66. ListenInput();
  67. CmdUpdateInput(HorizontalAxis, JumpKeyPressed, InteractionKeyPressed);
  68. }
  69. return;
  70. }
  71. if(isLocalPlayer){
  72. ListenInput();
  73. }
  74. isGrounded = getGrounded();
  75. if (_grounded != isGrounded)
  76. {
  77. if (isGrounded)
  78. {
  79. OnLand();
  80. }
  81. else
  82. {
  83. OnFly();
  84. }
  85. _grounded = isGrounded;
  86. }
  87. if (isSwimming)
  88. {
  89. rigidbody.velocity = new Vector2(rigidbody.velocity.x, Mathf.Lerp(rigidbody.velocity.y, buoyantForce, buoyantSpd));
  90. if ((JumpKeyPressed))
  91. {
  92. rigidbody.velocity = new Vector2(rigidbody.velocity.x, jumpForce / 12f);
  93. }
  94. }
  95. else
  96. {
  97. gravity = new Vector2(gravity.x, 0);
  98. }
  99. //Gravity Application
  100. rigidbody.AddForce(gravity);
  101. //Update In-Air value on animation
  102. animator.SetBool("inAir", !isGrounded);
  103. animator.SetBool("isOnWater", inWater);
  104. // if (GameManager.isPaused) { HorizontalAxis = 0; }
  105. //Move according to input
  106. //Exit the door
  107. if (enteringDoor && !InteractionKeyPressed)
  108. {
  109. enteringDoor = false;
  110. }
  111. if (insideDoor && !enteringDoor && InteractionKeyPressed)
  112. {
  113. Debug.Log("Exiting door");
  114. transform.position = SceneData.netSceneData.door.transform.position;
  115. insideDoor = false;
  116. enteringDoor = true;
  117. if (GetComponent<NetPlayer>() != null) { GetComponent<NetPlayer>().CallChangeInsideDoor(insideDoor); }
  118. }
  119. if (listenToInput)
  120. {
  121. if (isGrounded)
  122. {
  123. moveInput = HorizontalAxis;
  124. }
  125. else
  126. {
  127. //Change moveInput while in-air | IF there is an input
  128. // if (Input.GetKey(InputManager.data().leftInput) || Input.GetKey(InputManager.data().rightInput))
  129. if (HorizontalAxis!=0)
  130. {
  131. moveInput = Mathf.Lerp(moveInput, HorizontalAxis, 0.2f);
  132. }
  133. }
  134. //Enter the door
  135. if (inDoor && InteractionKeyPressed && !enteringDoor)
  136. {
  137. if (SceneData.netSceneData.doorExit != null)
  138. {
  139. Debug.Log("Entering door");
  140. transform.DetachChildren();
  141. transform.position = SceneData.netSceneData.doorExit.position;
  142. insideDoor = true;
  143. enteringDoor = true;
  144. if (GetComponent<NetPlayer>() != null) { GetComponent<NetPlayer>().CallChangeInsideDoor(insideDoor); }
  145. }
  146. }
  147. }
  148. if (isSwimming || inWater) { moveInput = moveInput / 2f; }
  149. //Update moving value on Animation
  150. animator.SetBool("moving", (moveInput != 0));
  151. animator.SetBool("isSwimming", isSwimming);
  152. //Flip Image to face moving Direction
  153. if (moveInput < 0)
  154. {
  155. sprite.flipX = (invertSprite) ? false : true;
  156. }
  157. else if (moveInput > 0)
  158. {
  159. sprite.flipX = (invertSprite) ? true : false;
  160. }
  161. //Apply moving input to player
  162. // if (GetComponent<NetPlayer>() != null)
  163. // {
  164. // if (GetComponent<NetPlayer>().parentFrnd != null)
  165. // {
  166. // rigidbody.velocity = GetComponent<NetPlayer>().parentFrnd.GetComponent<Rigidbody2D>().velocity;
  167. // }
  168. // }
  169. rigidbody.transform.Translate(new Vector2(moveSpeed * moveInput, 0));
  170. if(moveInput == 0){
  171. rigidbody.constraints = RigidbodyConstraints2D.FreezePositionX|RigidbodyConstraints2D.FreezeRotation;
  172. }else{
  173. rigidbody.constraints = RigidbodyConstraints2D.FreezeRotation;
  174. }
  175. bool _canJump = canJump();
  176. if ((JumpKeyPressed) && _canJump)
  177. {
  178. jumpT = 0;
  179. jumpReleased = false;
  180. rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
  181. if (jumpSFX != null) AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
  182. }
  183. //Blocks continous jump button
  184. if (!JumpKeyPressed) { jumpReleased = true; }
  185. //Apply Jump to player
  186. if (jumpT < jumpDuration)
  187. {
  188. jumpT += Time.deltaTime;
  189. float progress = (jumpDuration - jumpT) / jumpDuration;
  190. //|| jumpT < jumpDuration/2f
  191. if ((JumpKeyPressed) || b) { rigidbody.AddForce(new Vector2(0, jumpForce * progress)); }
  192. }
  193. else { b = false; }
  194. _isSwimming = inWater;
  195. }
  196. bool b;
  197. bool _isSwimming;
  198. public bool waterBoost;
  199. public bool canJump()
  200. {
  201. return jumpT >= jumpDuration && isGrounded && listenToInput && jumpReleased && !isSwimming && transform.childCount <= 0;//&& !GameManager.isPaused
  202. }
  203. public bool canBotJump()
  204. {
  205. return isGrounded || isSwimming || inWater;
  206. }
  207. //External Player Control Exposure
  208. public void Jump()
  209. {
  210. jumpT = 0;
  211. rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0);
  212. b = true;
  213. AudioSingleton.getSFXSource().PlayOneShot(jumpSFX);
  214. }
  215. public void move(float input)
  216. {
  217. input = Mathf.Clamp(input, -1, 1);
  218. moveInput = input;
  219. }
  220. void OnLand()
  221. {
  222. // Debug.Log("Landed Like a boss!");
  223. }
  224. void OnFly()
  225. {
  226. // Debug.Log("Hey look im flying");
  227. }
  228. public float groundCheckerDist = 0.1f;
  229. public float groundCheckerHeighMultipler = 1f;
  230. public bool getGrounded()
  231. {
  232. //return (Physics2D.Linecast(transform.position, groundChecker.position, groundLayerMask));
  233. Collider2D col = GetComponentInChildren<Collider2D>();
  234. 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));
  235. }
  236. void OnDrawGizmos()
  237. {
  238. Collider2D col = GetComponentInChildren<Collider2D>();
  239. Gizmos.DrawWireCube(transform.position, neighboursDetectorSize);
  240. Gizmos.color = Color.red;
  241. 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));
  242. }
  243. // public bool getInWater(){Collider2D col = GetComponentInChildren<Collider2D>();
  244. // 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));
  245. // }
  246. }
  247. // public static class SettingsInstance{
  248. // public static Settings instance;
  249. // }
  250. public static class AudioSingleton
  251. {
  252. private static AudioSource music;
  253. private static AudioSource sfx;
  254. public static AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
  255. public static AudioSource getMusicSource()
  256. {
  257. if (music == null)
  258. {
  259. GameObject go = new GameObject("Music Audio Source");
  260. go.AddComponent<AudioSource>();
  261. music = go.GetComponent<AudioSource>();
  262. music.outputAudioMixerGroup = mixer.FindMatchingGroups("Music")[0];
  263. }
  264. return music;
  265. }
  266. public static AudioSource getSFXSource()
  267. {
  268. //AudioMixer mixer = Resources.Load("MasterMixer") as AudioMixer;
  269. if (sfx == null)
  270. {
  271. GameObject go = new GameObject("SFX Audio Source");
  272. go.AddComponent<AudioSource>();
  273. sfx = go.GetComponent<AudioSource>();
  274. Debug.Log("sfx : " + (sfx == null).ToString() + ", mixer: " + (mixer == null).ToString());
  275. sfx.outputAudioMixerGroup = mixer.FindMatchingGroups("SFX")[0];
  276. }
  277. return sfx;
  278. }
  279. }