Score_Script.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.SceneManagement;
  5. public class Score_Script : MonoBehaviour
  6. {
  7. public string nextScene;
  8. private GameObject timeDisplay;
  9. private GameObject scoreDisplay;
  10. private GameObject bauteilDisplay;
  11. private void Start()
  12. {
  13. Cursor.visible = false;
  14. Static_GlobalStats.time = 0;
  15. Static_GlobalStats.levelScore = 0;
  16. Static_GlobalStats.bauteileLevel = 0;
  17. timeDisplay = GameObject.FindGameObjectWithTag("TimeDisplay");
  18. scoreDisplay = GameObject.FindGameObjectWithTag("ScoreDisplay");
  19. bauteilDisplay = GameObject.FindGameObjectWithTag("BauteilDisplay");
  20. }
  21. void Update()
  22. {
  23. if (Static_GlobalStats.health > 0) Static_GlobalStats.time += Time.deltaTime;
  24. if (timeDisplay) timeDisplay.gameObject.GetComponent<Text>().text = "" + (int)Static_GlobalStats.time;
  25. if (scoreDisplay) scoreDisplay.gameObject.GetComponent<Text>().text = "" + (int)(Static_GlobalStats.totalScore + Static_GlobalStats.levelScore);
  26. if (bauteilDisplay) bauteilDisplay.gameObject.GetComponent<Text>().text = "" + (int)(Static_GlobalStats.bauteile + Static_GlobalStats.bauteileLevel) + "/" + (int)Static_GlobalStats.bauteileMax;
  27. if (Input.GetKey(KeyCode.Escape))
  28. {
  29. Application.Quit();
  30. }
  31. }
  32. private void OnTriggerEnter2D(Collider2D collision)
  33. {
  34. if (collision.gameObject.tag == "Collectable")
  35. {
  36. Collectable_Script collectable = collision.gameObject.GetComponent<Collectable_Script>();
  37. Static_GlobalStats.levelScore += collectable.scoreValue;
  38. Static_GlobalStats.health = Mathf.Clamp(Static_GlobalStats.health + collectable.healthValue, 0, 100);
  39. Static_GlobalStats.lifes = Mathf.Clamp(Static_GlobalStats.lifes + collectable.lifeValue, 0, 99);
  40. if (collision.gameObject.GetComponent<AudioSource>()) collision.gameObject.GetComponent<AudioSource>().Play();
  41. collision.gameObject.GetComponent<Collider2D>().enabled = false;
  42. Rigidbody2D coinRB = collision.gameObject.GetComponent<Rigidbody2D>();
  43. coinRB.AddForce(Vector2.up * 1000);
  44. }
  45. else if (collision.gameObject.tag == "Finish")
  46. {
  47. gameObject.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Kinematic;
  48. Static_GlobalStats.totalScore += Static_GlobalStats.levelScore;
  49. Static_GlobalStats.levelScore = 0;
  50. Static_GlobalStats.bauteile += Static_GlobalStats.bauteileLevel;
  51. Static_GlobalStats.bauteileLevel = 0;
  52. if (nextScene != null && nextScene != "") SceneManager.LoadScene(nextScene);
  53. else SceneManager.LoadScene("Menu_Main");
  54. }
  55. }
  56. }