123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- public class Player_Health_Script : MonoBehaviour
- {
- public int fatalY = -10;
- public int playerLayer = 8;
- private float width;
- private float height;
- private GameObject healthDisplay;
- private GameObject lifesDisplay;
- private bool dying;
- private int hurtCooldown = 0;
- private AudioManager_Script audioManager;
- void Start()
- {
- width = GetComponent<SpriteRenderer>().bounds.size.x;
- height = GetComponent<SpriteRenderer>().bounds.size.y;
- healthDisplay = GameObject.FindGameObjectWithTag("HealthDisplay");
- lifesDisplay = GameObject.FindGameObjectWithTag("LifesDisplay");
- audioManager = GameObject.FindGameObjectWithTag("AudioManager").GetComponent<AudioManager_Script>();
- }
- void Update()
- {
- gameObject.GetComponent<Animator>().SetBool("hurt", false);
- Color oldColor = gameObject.GetComponent<SpriteRenderer>().color;
- if (oldColor != null && (oldColor.r < 1 || oldColor.g < 1 || oldColor.b < 1))
- {
- gameObject.GetComponent<SpriteRenderer>().color = new Color(oldColor.r / 0.6f, oldColor.r / 0.6f, oldColor.r / 0.6f, 1);
- }
- if (dying) return;
- if (hurtCooldown > 0) hurtCooldown--;
- int layerMask = ~(1 << playerLayer);
- for (int x = -1; x <= 1; x += 2)
- {
- for (float y = gameObject.transform.position.y - height * 0.4f; y <= gameObject.transform.position.y + height * 0.4f; y += 0.1f)
- {
- RaycastHit2D hit = Physics2D.Raycast(new Vector2(gameObject.transform.position.x, y), new Vector2(x, 0), width * 0.6f, layerMask);
- if (hit.collider && (hit.collider.tag == "Enemy" || hit.collider.tag == "Projectile"))
- {
- gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(-x * 1200, 200));
- hit.collider.GetComponent<Rigidbody2D>().AddForce(new Vector2(x * 1200, 200));
- if (hurtCooldown <= 0) hurtPlayer();
- if (hit.collider.tag == "Projectile")
- {
- DestroyImmediate(hit.collider.gameObject);
- }
- break;
- }
- }
- }
- if (gameObject.transform.position.y <= fatalY) Static_GlobalStats.health = 0;
- if (Static_GlobalStats.health <= 0)
- {
- int randomDir = Random.Range(0f, 1f) > 0.5f ? -1 : 1;
- gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.None;
- gameObject.GetComponent<Rigidbody2D>().AddForceAtPosition(new Vector2(randomDir * 40, 5), new Vector2(width, height));
- Static_GlobalStats.lifes--;
- if (Static_GlobalStats.lifes > 0)
- {
- StartCoroutine(restartLevel());
- }
- else
- {
- StartCoroutine(gameOver());
- }
- audioManager.playSfx("death");
- audioManager.playComment("death");
- dying = true;
- }
- if (healthDisplay)
- {
- healthDisplay.gameObject.GetComponent<Text>().text = "" + Static_GlobalStats.health + "%";
- float b = (float)Static_GlobalStats.health / 100;
- float g = 0.5f + b / 2;
- healthDisplay.gameObject.GetComponent<Text>().color = new Color(1, g, b, 1);
- }
- if (lifesDisplay) lifesDisplay.gameObject.GetComponent<Text>().text = "" + Static_GlobalStats.lifes;
- gameObject.GetComponent<Animator>().SetFloat("health", Static_GlobalStats.health);
- }
- void hurtPlayer()
- {
- hurtCooldown += 20;
- Static_GlobalStats.health = Mathf.Clamp(Static_GlobalStats.health - 15, 0, 100);
- if (audioManager) {
- if (Static_GlobalStats.health > 0) audioManager.playComment("hurt");
- }
- gameObject.GetComponent<Animator>().SetBool("hurt", true);
- gameObject.GetComponent<SpriteRenderer>().color = new Color(1, 0.5f, 0.3f, 1);
- }
- IEnumerator restartLevel()
- {
- yield return new WaitForSeconds(3);
- Static_GlobalStats.health = 100;
- SceneManager.LoadScene(SceneManager.GetActiveScene().name);
- }
- IEnumerator gameOver()
- {
- yield return new WaitForSeconds(3);
- Static_GlobalStats.totalScore = 0;
- Static_GlobalStats.levelScore = 0;
- Static_GlobalStats.bauteile = 0;
- Static_GlobalStats.bauteileLevel = 0;
- Static_GlobalStats.lifes = 3;
- Static_GlobalStats.health = 100;
- SceneManager.LoadScene("Menu_GameOver");
- }
- }
|