Player_Move_Script.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. public class Player_Move_Script : MonoBehaviour
  6. {
  7. public int maxSpeed = 8;
  8. public int jumpPower = 1400;
  9. public float speed;
  10. public bool grounded = true;
  11. public bool facingRight = true;
  12. public int playerLayer = 8;
  13. private float width;
  14. private float height;
  15. private Animator animator;
  16. private AudioManager_Script audioManager;
  17. void Start()
  18. {
  19. width = GetComponent<SpriteRenderer>().bounds.size.x;
  20. height = GetComponent<SpriteRenderer>().bounds.size.y;
  21. animator = GetComponent<Animator>();
  22. audioManager = GameObject.FindGameObjectWithTag("AudioManager").GetComponent<AudioManager_Script>();
  23. }
  24. void Update()
  25. {
  26. if (Static_GlobalStats.health <= 0) return;
  27. int layerMask = ~(1 << playerLayer);
  28. speed = Input.GetAxis("Horizontal");
  29. Rigidbody2D rigidbody = gameObject.GetComponent<Rigidbody2D>();
  30. if (Static_GlobalStats.paused) {
  31. rigidbody.velocity = Vector2.zero;
  32. animator.SetFloat("speed", 0);
  33. return;
  34. }
  35. // Ground Check
  36. // Jump On Enemy Check
  37. grounded = false;
  38. for (float x = gameObject.transform.position.x - width * 0.55f; x <= gameObject.transform.position.x + width * 0.55f; x += 0.1f)
  39. {
  40. RaycastHit2D groundHit = Physics2D.Raycast(new Vector2(x, gameObject.transform.position.y), Vector2.down, height * 0.8f, layerMask);
  41. if (groundHit.collider)
  42. {
  43. if (groundHit.collider.tag == "Enemy" || groundHit.collider.tag == "PseudoEnemy" || groundHit.collider.tag == "Boss") //Jump on Enemy
  44. {
  45. if (audioManager)
  46. {
  47. audioManager.playSfx("kill");
  48. if (Random.Range(0f, 1f) < 0.3f) audioManager.playComment("kill");
  49. }
  50. rigidbody.AddForce(Vector2.up * jumpPower);
  51. groundHit.collider.GetComponent<SpriteRenderer>().color = new Color(1, 0.5f, 0.3f, 1);
  52. groundHit.collider.enabled = false;
  53. if (groundHit.collider.tag == "Boss")
  54. {
  55. groundHit.collider.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
  56. groundHit.collider.GetComponent<Rigidbody2D>().AddForce(Vector2.down * 1000);
  57. StartCoroutine(WinTheFuckingGame());
  58. }
  59. break;
  60. }
  61. grounded = true;
  62. break;
  63. }
  64. }
  65. // Wall Check
  66. for (float y = gameObject.transform.position.y - height * 0.4f; y <= gameObject.transform.position.y + height * 0.4f; y += 0.1f)
  67. {
  68. RaycastHit2D wallHit = Physics2D.Raycast(new Vector2(gameObject.transform.position.x, y), new Vector2(speed, 0), width * 0.55f, layerMask);
  69. if (wallHit.collider && wallHit.collider.tag != "Collectable" && wallHit.collider.tag != "Projectile" && wallHit.collider.tag != "Finish")
  70. {
  71. speed = 0;
  72. break;
  73. }
  74. }
  75. // Controls
  76. if (Input.GetButtonDown("Jump") && grounded)
  77. {
  78. jump();
  79. }
  80. // Player Direction
  81. if ((speed < 0.0f && facingRight) || (speed > 0.0f && !facingRight))
  82. {
  83. flipPlayer();
  84. }
  85. // Physics
  86. rigidbody.velocity = new Vector2(speed * maxSpeed, rigidbody.velocity.y);
  87. // Animation
  88. animator.SetFloat("speed", Mathf.Abs(speed));
  89. animator.SetBool("grounded", grounded);
  90. }
  91. // Might as well...
  92. void jump()
  93. {
  94. gameObject.GetComponent<Rigidbody2D>().AddForce(Vector2.up * jumpPower);
  95. if (audioManager) audioManager.playSfx("jump");
  96. }
  97. void flipPlayer()
  98. {
  99. facingRight = !facingRight;
  100. Vector2 localScale = gameObject.transform.localScale;
  101. localScale.x *= -1;
  102. transform.localScale = localScale;
  103. }
  104. IEnumerator WinTheFuckingGame()
  105. {
  106. yield return new WaitForSeconds(0.2f);
  107. if (audioManager) audioManager.playSfx("kill");
  108. yield return new WaitForSeconds(0.2f);
  109. if (audioManager) audioManager.playSfx("kill");
  110. yield return new WaitForSeconds(0.2f);
  111. if (audioManager) audioManager.playSfx("kill");
  112. yield return new WaitForSeconds(0.2f);
  113. if (audioManager) audioManager.playSfx("kill");
  114. yield return new WaitForSeconds(0.2f);
  115. if (audioManager) audioManager.playSfx("kill");
  116. yield return new WaitForSeconds(0.2f);
  117. if (audioManager) audioManager.playSfx("kill");
  118. yield return new WaitForSeconds(0.2f);
  119. if (audioManager) audioManager.playSfx("kill");
  120. yield return new WaitForSeconds(0.2f);
  121. if (audioManager) audioManager.playSfx("kill");
  122. yield return new WaitForSeconds(0.2f);
  123. if (audioManager) audioManager.playSfx("kill");
  124. SceneManager.LoadScene("Menu_Finale");
  125. }
  126. }