76 lines
2.4 KiB
C#
76 lines
2.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class LevelSelector : MonoBehaviour
|
|
{
|
|
[Header("Pengaturan Button & Gembok")]
|
|
public Button[] levelButtons;
|
|
public GameObject[] gembokImages;
|
|
|
|
[Header("Sistem Bintang")]
|
|
// Buat objek penampung bintang. Element 0 = Bintang-bintang Lvl 1, dst.
|
|
public KelompokBintang[] listBintangPerLevel;
|
|
|
|
[Header("Pindah Scene")]
|
|
public string[] namaLevel;
|
|
|
|
[System.Serializable]
|
|
public class KelompokBintang {
|
|
public GameObject[] bintangKuning; // Tarik 3 bintang kuning di setiap level ke sini
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
// 1. Cek level mana yang sudah terbuka (default level 1)
|
|
int levelTerbuka = PlayerPrefs.GetInt("LevelTerbuka", 1);
|
|
|
|
for (int i = 0; i < levelButtons.Length; i++)
|
|
{
|
|
// --- LOGIKA BUKA TUTUP GEMBOK ---
|
|
bool levelIniTerbuka = (i + 1 <= levelTerbuka);
|
|
|
|
levelButtons[i].interactable = levelIniTerbuka;
|
|
|
|
if(gembokImages[i] != null)
|
|
gembokImages[i].SetActive(!levelIniTerbuka);
|
|
|
|
// --- LOGIKA MENAMPILKAN BINTANG ---
|
|
if (levelIniTerbuka)
|
|
{
|
|
// Ambil data bintang (Key harus sama dengan yang di simpan di GameManager Level)
|
|
// Contoh key: Bintang_Level1
|
|
int bintangDidapat = PlayerPrefs.GetInt("Bintang_" + namaLevel[i], 0);
|
|
SetBintangVisual(i, bintangDidapat);
|
|
}
|
|
else
|
|
{
|
|
// Kalau kekunci, bintangnya matiin semua
|
|
SetBintangVisual(i, 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
void SetBintangVisual(int indexLevel, int jumlahBintang)
|
|
{
|
|
if (indexLevel < listBintangPerLevel.Length)
|
|
{
|
|
for (int j = 0; j < listBintangPerLevel[indexLevel].bintangKuning.Length; j++)
|
|
{
|
|
// Nyalakan bintang sesuai jumlah yang didapat (0, 1, 2, atau 3)
|
|
listBintangPerLevel[indexLevel].bintangKuning[j].SetActive(j < jumlahBintang);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void PilihLevel(int indexLevel)
|
|
{
|
|
SceneManager.LoadScene(namaLevel[indexLevel]);
|
|
}
|
|
|
|
public void ResetData() {
|
|
PlayerPrefs.DeleteAll();
|
|
Debug.Log("Data Game di Reset!");
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
|
}
|
|
} |