using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.SceneManagement; public class GameSession : MonoBehaviour { public static GameSession Instance; private Dictionary levelScores = new Dictionary(); private Dictionary totalScorePerMateri = new Dictionary(); public int CurrentScore { get; private set; } private string currentLevelName; private string currentMateri; public bool IsLevelCompleted { get; private set; } = false; public Dictionary TotalScorePerMateri => totalScorePerMateri; private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); LoadScores(); LoadTotalScorePerMateri(); } else { Destroy(gameObject); } } public void StartLevel(string levelName) { //currentLevelName = levelName; currentLevelName = string.IsNullOrEmpty(levelName) ? SceneManager.GetActiveScene().name : levelName; currentMateri = GetMateriFromLevelName(currentLevelName); CurrentScore = 0; IsLevelCompleted = false; } public void AddScore(int value) { CurrentScore += value; } public void SaveLevelScore() { if (!string.IsNullOrEmpty(currentLevelName)) { if (levelScores.ContainsKey(currentLevelName)) { if (CurrentScore > levelScores[currentLevelName]) { levelScores[currentLevelName] = CurrentScore; PlayerPrefs.SetInt("skor" + currentLevelName, CurrentScore); } } else { levelScores[currentLevelName] = CurrentScore; PlayerPrefs.SetInt("skor" + currentLevelName, CurrentScore); } if (LevelControl.Instance != null && LevelControl.Instance.IsLastLevelOfMateri(currentLevelName)) { RecalculateTotalScoreForMateri(currentMateri); // hitung ulang dari semua level dalam materi SaveTotalScorePerMateri(); } PlayerPrefs.Save(); //levelScores[currentLevelName] = CurrentScore; } } public void RecalculateTotalScoreForMateri(string materiName) { int total = 0; if (LevelControl.Instance != null) { List levels = LevelControl.Instance.GetLevelsInMateri(materiName); foreach (string level in levels) { total += GetScoreForLevel(level); } totalScorePerMateri[materiName] = total; } } public void AddScoreToMateri(string materiName, int scoreToAdd) { if (totalScorePerMateri.ContainsKey(materiName)) { totalScorePerMateri[materiName] += scoreToAdd; } else { totalScorePerMateri[materiName] = scoreToAdd; } } public string GetMateriFromLevelName(string levelName) { if (LevelControl.Instance != null) return LevelControl.Instance.GetMateriByLevel(levelName); return "MateriTidakDikenal"; } public int GetTotalScore() { int total = 0; foreach (var score in levelScores.Values) { total += score; } return total; } public int GetScoreForLevel(string levelName) { if (levelScores.ContainsKey(levelName)) { return levelScores[levelName]; } return 0; } public int GetTotalScoreByMateri(string materiName) { int total = 0; foreach (var entry in levelScores) { string levelName = entry.Key; int score = entry.Value; string materi = GetMateriFromLevelName(levelName); if (materi == materiName) { total += score; } } return total; } private void LoadScores() { levelScores.Clear(); int sceneCount = SceneManager.sceneCountInBuildSettings; for (int i = 0; i < sceneCount; i++) { string path = SceneUtility.GetScenePathByBuildIndex(i); string sceneName = System.IO.Path.GetFileNameWithoutExtension(path); int score = PlayerPrefs.GetInt("skor" + sceneName, 0); if (score > 0) { levelScores[sceneName] = score; } } } public void ResetAllScores() { levelScores.Clear(); CurrentScore = 0; int sceneCount = SceneManager.sceneCountInBuildSettings; for (int i = 0; i < sceneCount; i++) { string path = SceneUtility.GetScenePathByBuildIndex(i); string sceneName = System.IO.Path.GetFileNameWithoutExtension(path); PlayerPrefs.DeleteKey("skor" + sceneName); } foreach (string key in new List(totalScorePerMateri.Keys)) { PlayerPrefs.DeleteKey("MateriScore" + key); } PlayerPrefs.Save(); } public int GetHighScore(string levelName) { return PlayerPrefs.GetInt("skor" + levelName, 0); } public void MarkLevelComplete() { IsLevelCompleted = true; } public void SaveTotalScorePerMateri() { foreach (var entry in totalScorePerMateri) { PlayerPrefs.SetInt("MateriScore" + entry.Key, entry.Value); } } public void LoadTotalScorePerMateri() { totalScorePerMateri.Clear(); int sceneCount = SceneManager.sceneCountInBuildSettings; for (int i = 0; i < sceneCount; i++) { string path = SceneUtility.GetScenePathByBuildIndex(i); string sceneName = System.IO.Path.GetFileNameWithoutExtension(path); string materiName = GetMateriFromLevelName(sceneName); int score = PlayerPrefs.GetInt("MateriScore" + materiName, 0); if (score > 0) { totalScorePerMateri[materiName] = score; } } } }