MIF_E31221357/Assets/Scripts/ShowingScore.cs

92 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ShowingScore : MonoBehaviour
{
[Header("Panel Skor")]
public GameObject panelLevelComplete;
public GameObject mazeParent;
public TextMeshProUGUI scoreText;
public TextMeshProUGUI highScoreText;
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
private void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
StartCoroutine(FindUIElementsNextFrame());
}
private IEnumerator FindUIElementsNextFrame()
{
yield return null; // Tunggu 1 frame agar semua objek UI sudah aktif
if (panelLevelComplete == null)
panelLevelComplete = GameObject.FindGameObjectWithTag("PanelComplete");
if (mazeParent == null)
mazeParent = GameObject.Find("GameObject");
if (scoreText == null)
{
GameObject scoreObj = GameObject.FindGameObjectWithTag("ScoreText");
if (scoreObj != null)
scoreText = scoreObj.GetComponent<TextMeshProUGUI>();
}
if (highScoreText == null)
{
GameObject highScoreObj = GameObject.FindGameObjectWithTag("HighScoreText");
if (highScoreObj != null)
highScoreText = highScoreObj.GetComponent<TextMeshProUGUI>();
}
if (GameSession.Instance != null && GameSession.Instance.IsLevelCompleted)
{
levelComplete(); // panggil ulang fungsi ini
}
}
public void levelComplete()
{
GameSession.Instance.MarkLevelComplete();
GameSession.Instance.SaveLevelScore();
string currentLevelName = SceneManager.GetActiveScene().name;
int highScore = GameSession.Instance.GetHighScore(currentLevelName);
if (panelLevelComplete != null)
panelLevelComplete.SetActive(true);
if (mazeParent != null)
{
SpriteRenderer[] mazeRenderers = mazeParent.GetComponentsInChildren<SpriteRenderer>();
foreach (SpriteRenderer sr in mazeRenderers)
{
sr.sortingOrder = -10;
}
}
if (scoreText != null)
scoreText.text = "Skor kamu: " + GameSession.Instance.CurrentScore.ToString();
if (highScoreText != null)
highScoreText.text = "High Score: " + highScore.ToString();
}
}