using UnityEngine; using System.Linq; using System.Collections; public class HealthManager : MonoBehaviour { public int MaxHealth = 5; public int hitpoints = 2; public BulletSpawner spawner; public GameObject deathEffectPrefab; public GameObject player; public Animator scoreAnimation; public Animator heatButtonAnimator; public Animator exitButtonAnimator; public GameObject restartButton; public InterstitialAdsManager interstitialAdsManager; public ScoreDataManager scoreDataManager; public SFXManager sfxManager; public AudioSource audioSource; private bool isDead = false; private void Awake() { scoreAnimation.StopPlayback(); hitpoints = MaxHealth; } private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Bullet")) { GameObject.Destroy(collision.gameObject); hitpoints--; audioSource.Play(); if (hitpoints <= 0) { // Player is dead, handle death logic here Debug.Log("Player is dead!"); sfxManager.PlaySFX(SFX.playerDeath); sfxManager.StopSong(songs.gameplay); sfxManager.PlaySong(songs.gameOver); ScoreManager.Instance.heatBar.gameObject.SetActive(false); ScoreManager.Instance.heatButton.gameObject.SetActive(false); VFXManager.Instance.ScreenShake(0.5f, 1f); spawner.gameEnded = true; spawner.StopAllCoroutines(); Instantiate(deathEffectPrefab, player.transform.position, Quaternion.identity); VFXManager.Instance.SlowMo(0.5f, 2f); GameObject.FindGameObjectsWithTag("Bullet").ToList().ForEach(b => Destroy(b)); StartCoroutine(WaitForScoreAnimation()); player.SetActive(false); scoreAnimation.Play("ScoreTextEndgame"); heatButtonAnimator.Play("HeatButtonEndgame"); // restartButton.SetActive(true); } } } IEnumerator WaitForScoreAnimation() { scoreAnimation.Play("ScoreTextEndgame"); // wait 1 frame so Animator updates state yield return null; while (true) { AnimatorStateInfo state = scoreAnimation.GetCurrentAnimatorStateInfo(0); if (state.IsName("ScoreTextEndgame") && state.normalizedTime >= 1f) break; yield return null; } interstitialAdsManager.OnRunEnded(); yield return new WaitForSeconds(0.1f); scoreDataManager.AddScore(ScoreManager.Instance.totalScore); exitButtonAnimator.Play("ExitButton"); //DataStorageManager.Instance.SaveScores(scoreDataManager.GetLeaderboardScores()); RestartButton(); } public void RestartButton() { restartButton.SetActive(true); } }