using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; public class GameController : MonoBehaviour { //Serialized fields to assign UI elements and initial values in the Inspector [SerializeField] TextMeshProUGUI deathsText; [SerializeField] TextMeshProUGUI scoreText; [SerializeField] TextMeshProUGUI mostDeathsText; [SerializeField] int subjectDeaths = 0; [SerializeField] int playerScore = 0; //Private variables to store checkpoint position and components Vector2 checkpointPos; PlayerMovement playerMovement; SpriteRenderer playerSpriteRenderer; Collider2D playerCollider; private float setSpeed = 8f; void Awake() { // Get required components playerSpriteRenderer = GetComponent(); playerCollider = GetComponent(); playerMovement = GetComponent(); } void Start() { // Initialize checkpoint position and UI text elements checkpointPos = transform.position; deathsText.text = subjectDeaths.ToString(); scoreText.text = playerScore.ToString(); UpdateMostDeaths(); } void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("laser")) { Die(); // If the player collides with a laser, handle player death } else if (collision.CompareTag("Goo")) { ScoreIncrease(); // If the player collides with Goo, increase the score } else if (collision.CompareTag("Checkpoints")) { // If the player collides with a checkpoint, update the checkpoint position and reset gravity checkpointPos = transform.position; playerMovement.DeathGravityReturn(); } } void Die() //handling the player death { AudioManager.Instance.PlaySFX("PlayerDeath"); playerMovement.speed = setSpeed; StartCoroutine(Respawn(0.5f)); subjectDeaths += 1; deathsText.text = subjectDeaths.ToString(); CheckMostDeaths(); } public void ScoreIncrease() //inceasing the score { playerScore += 1; AudioManager.Instance.PlaySFX("GooDeath"); scoreText.text = playerScore.ToString(); } IEnumerator Respawn(float duration) //respawning the player { playerCollider.enabled = false; playerSpriteRenderer.enabled = false; yield return new WaitForSeconds(duration); transform.position = checkpointPos; playerSpriteRenderer.enabled = true; playerMovement.DeathGravityReturn(); playerCollider.enabled = true; } void CheckMostDeaths() //checking the highest death count from a file { if(subjectDeaths > PlayerPrefs.GetInt("Most Deaths", 0)) { PlayerPrefs.SetInt("Most Deaths", subjectDeaths); } } void UpdateMostDeaths() //updating the highest death count to the file { mostDeathsText.text = $"{PlayerPrefs.GetInt("Most Deaths", 0)}"; } }