Player_Health_Script.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UI;
  6. public class Player_Health_Script : MonoBehaviour
  7. {
  8. public int fatalY = -10;
  9. public int playerLayer = 8;
  10. private float width;
  11. private float height;
  12. private GameObject healthDisplay;
  13. private GameObject lifesDisplay;
  14. private bool dying;
  15. private int hurtCooldown = 0;
  16. private AudioManager_Script audioManager;
  17. void Start()
  18. {
  19. width = GetComponent<SpriteRenderer>().bounds.size.x;
  20. height = GetComponent<SpriteRenderer>().bounds.size.y;
  21. healthDisplay = GameObject.FindGameObjectWithTag("HealthDisplay");
  22. lifesDisplay = GameObject.FindGameObjectWithTag("LifesDisplay");
  23. audioManager = GameObject.FindGameObjectWithTag("AudioManager").GetComponent<AudioManager_Script>();
  24. }
  25. void Update()
  26. {
  27. gameObject.GetComponent<Animator>().SetBool("hurt", false);
  28. Color oldColor = gameObject.GetComponent<SpriteRenderer>().color;
  29. if (oldColor != null && (oldColor.r < 1 || oldColor.g < 1 || oldColor.b < 1))
  30. {
  31. gameObject.GetComponent<SpriteRenderer>().color = new Color(oldColor.r / 0.6f, oldColor.r / 0.6f, oldColor.r / 0.6f, 1);
  32. }
  33. if (dying) return;
  34. if (hurtCooldown > 0) hurtCooldown--;
  35. int layerMask = ~(1 << playerLayer);
  36. for (int x = -1; x <= 1; x += 2)
  37. {
  38. for (float y = gameObject.transform.position.y - height * 0.4f; y <= gameObject.transform.position.y + height * 0.4f; y += 0.1f)
  39. {
  40. RaycastHit2D hit = Physics2D.Raycast(new Vector2(gameObject.transform.position.x, y), new Vector2(x, 0), width * 0.6f, layerMask);
  41. if (hit.collider && (hit.collider.tag == "Enemy" || hit.collider.tag == "Projectile"))
  42. {
  43. gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(-x * 1200, 200));
  44. hit.collider.GetComponent<Rigidbody2D>().AddForce(new Vector2(x * 1200, 200));
  45. if (hurtCooldown <= 0) hurtPlayer();
  46. if (hit.collider.tag == "Projectile")
  47. {
  48. DestroyImmediate(hit.collider.gameObject);
  49. }
  50. break;
  51. }
  52. }
  53. }
  54. if (gameObject.transform.position.y <= fatalY) Static_GlobalStats.health = 0;
  55. if (Static_GlobalStats.health <= 0)
  56. {
  57. int randomDir = Random.Range(0f, 1f) > 0.5f ? -1 : 1;
  58. gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
  59. gameObject.GetComponent<Rigidbody2D>().AddForceAtPosition(new Vector2(randomDir * 40, 5), new Vector2(width, height));
  60. Static_GlobalStats.lifes--;
  61. if (Static_GlobalStats.lifes > 0)
  62. {
  63. StartCoroutine(restartLevel());
  64. }
  65. else
  66. {
  67. StartCoroutine(gameOver());
  68. }
  69. audioManager.playSfx("death");
  70. audioManager.playComment("death");
  71. dying = true;
  72. }
  73. if (healthDisplay)
  74. {
  75. healthDisplay.gameObject.GetComponent<Text>().text = "" + Static_GlobalStats.health + "%";
  76. float b = (float)Static_GlobalStats.health / 100;
  77. float g = 0.5f + b / 2;
  78. healthDisplay.gameObject.GetComponent<Text>().color = new Color(1, g, b, 1);
  79. }
  80. if (lifesDisplay) lifesDisplay.gameObject.GetComponent<Text>().text = "" + Static_GlobalStats.lifes;
  81. gameObject.GetComponent<Animator>().SetFloat("health", Static_GlobalStats.health);
  82. }
  83. void hurtPlayer()
  84. {
  85. hurtCooldown += 20;
  86. Static_GlobalStats.health = Mathf.Clamp(Static_GlobalStats.health - 15, 0, 100);
  87. if (audioManager) {
  88. if (Static_GlobalStats.health > 0) audioManager.playComment("hurt");
  89. }
  90. gameObject.GetComponent<Animator>().SetBool("hurt", true);
  91. gameObject.GetComponent<SpriteRenderer>().color = new Color(1, 0.5f, 0.3f, 1);
  92. }
  93. IEnumerator restartLevel()
  94. {
  95. yield return new WaitForSeconds(3);
  96. Static_GlobalStats.health = 100;
  97. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  98. }
  99. IEnumerator gameOver()
  100. {
  101. yield return new WaitForSeconds(3);
  102. Static_GlobalStats.totalScore = 0;
  103. Static_GlobalStats.levelScore = 0;
  104. Static_GlobalStats.bauteile = 0;
  105. Static_GlobalStats.bauteileLevel = 0;
  106. Static_GlobalStats.lifes = 3;
  107. Static_GlobalStats.health = 100;
  108. SceneManager.LoadScene("Menu_GameOver");
  109. }
  110. }