274 lines
9.4 KiB
C#
274 lines
9.4 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using UnityEngine.SceneManagement;
|
|
using System.Collections;
|
|
|
|
public class GameManagerTangkap : MonoBehaviour
|
|
{
|
|
public static GameManagerTangkap instance;
|
|
|
|
[Header("Game Settings")]
|
|
public int score = 0;
|
|
public int targetScore = 100;
|
|
public int health = 3;
|
|
public float timer = 60f;
|
|
private bool gameAktif = false;
|
|
|
|
[Header("UI References")]
|
|
public TextMeshProUGUI scoreText;
|
|
public TextMeshProUGUI timerText;
|
|
public Slider progressSlider;
|
|
public GameObject[] hearts;
|
|
|
|
[Header("Sistem Countdown (BARU)")]
|
|
public GameObject countdownParent;
|
|
public TextMeshProUGUI countdownText;
|
|
|
|
[Header("Sistem Hasil Akhir")]
|
|
public Sprite bintangNyala;
|
|
public GameObject panelHasil;
|
|
public GameObject latarHitam;
|
|
public TextMeshProUGUI textNilaiAkhir;
|
|
public Image[] bintangHasilAkhir;
|
|
|
|
[Header("Sistem Reward Skin")]
|
|
public Image slotHadiah;
|
|
public Sprite spriteHadiahKosong;
|
|
public int idSkinLevelIni;
|
|
|
|
[Header("Sistem Level")]
|
|
public Button buttonNext;
|
|
public Sprite spriteNextOn;
|
|
public Sprite spriteNextOff;
|
|
|
|
[Header("Sistem Audio")]
|
|
public AudioSource audioSource;
|
|
public AudioClip sfxMakanSayur;
|
|
public AudioClip sfxMakanJunkfood;
|
|
|
|
[Header("Sistem Suara")]
|
|
public AudioClip suaraBintang;
|
|
|
|
[Header("Referensi Objek Dino")]
|
|
public UnityEngine.UI.Image dinoImageUtama;
|
|
public RectTransform mataKiriTransform;
|
|
public RectTransform mataKananTransform;
|
|
public RectTransform mulutTransform;
|
|
|
|
[Header("Sfx Countdown")]
|
|
public AudioClip sfxBeepPendek;
|
|
public AudioClip sfxBeepPanjang;
|
|
|
|
void Awake() {
|
|
if (instance == null) instance = this;
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
Time.timeScale = 1;
|
|
gameAktif = false;
|
|
|
|
int id = PlayerPrefs.GetInt("SkinDipakai", 0);
|
|
if (SkinData.instance != null)
|
|
{
|
|
Skin skinAktif = SkinData.instance.daftarSkin[id];
|
|
if (dinoImageUtama != null) dinoImageUtama.sprite = skinAktif.spriteGameplay;
|
|
if (mataKiriTransform != null) mataKiriTransform.anchoredPosition = skinAktif.posisiMataKiri;
|
|
if (mataKananTransform != null) mataKananTransform.anchoredPosition = skinAktif.posisiMataKanan;
|
|
if (mulutTransform != null) mulutTransform.anchoredPosition = skinAktif.posisiMulut;
|
|
}
|
|
|
|
if (progressSlider != null) {
|
|
progressSlider.minValue = 0;
|
|
progressSlider.maxValue = targetScore;
|
|
progressSlider.value = 0;
|
|
}
|
|
|
|
UpdateUI();
|
|
ResetPopups();
|
|
StartCoroutine(ProsesCountdown());
|
|
}
|
|
|
|
IEnumerator ProsesCountdown() {
|
|
if (countdownParent != null) countdownParent.SetActive(true);
|
|
|
|
int hitungan = 3;
|
|
while (hitungan > 0) {
|
|
if (countdownText != null) countdownText.text = hitungan.ToString();
|
|
if (audioSource != null && sfxBeepPendek != null) audioSource.PlayOneShot(sfxBeepPendek);
|
|
yield return new WaitForSeconds(1f);
|
|
hitungan--;
|
|
}
|
|
|
|
if (countdownText != null) countdownText.text = "Mulai!";
|
|
if (audioSource != null && sfxBeepPanjang != null) audioSource.PlayOneShot(sfxBeepPanjang);
|
|
|
|
yield return new WaitForSeconds(0.8f);
|
|
|
|
if (countdownParent != null) countdownParent.SetActive(false);
|
|
gameAktif = true;
|
|
|
|
ObjectSpawner spawner = FindObjectOfType<ObjectSpawner>();
|
|
if (spawner != null) spawner.MulaiHujanSayur();
|
|
}
|
|
|
|
void Update() {
|
|
if (gameAktif && timer > 0) {
|
|
timer -= Time.deltaTime;
|
|
UpdateDisplayTimer();
|
|
} else if (gameAktif && timer <= 0) {
|
|
GameOver();
|
|
}
|
|
}
|
|
|
|
void UpdateDisplayTimer() {
|
|
if (timerText == null) return;
|
|
int minutes = Mathf.FloorToInt(timer / 60);
|
|
int seconds = Mathf.FloorToInt(timer % 60);
|
|
timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
|
|
}
|
|
|
|
public void AddScore(int value) {
|
|
if (!gameAktif) return;
|
|
score += value;
|
|
UpdateUI();
|
|
|
|
if (AnimasiMulutDino.instance != null) AnimasiMulutDino.instance.MulaiNgunyah();
|
|
if (audioSource != null && sfxMakanSayur != null) audioSource.PlayOneShot(sfxMakanSayur);
|
|
|
|
if (score >= targetScore) WinLevel();
|
|
}
|
|
|
|
public void TakeDamage() {
|
|
if (!gameAktif) return;
|
|
health--;
|
|
UpdateUI();
|
|
if (audioSource != null && sfxMakanJunkfood != null) audioSource.PlayOneShot(sfxMakanJunkfood);
|
|
if (health <= 0) GameOver();
|
|
}
|
|
|
|
void UpdateUI() {
|
|
if (scoreText != null) scoreText.text = score.ToString();
|
|
if (progressSlider != null) progressSlider.value = score;
|
|
|
|
for (int i = 0; i < hearts.Length; i++) {
|
|
if (hearts[i] != null) hearts[i].SetActive(i < health);
|
|
}
|
|
}
|
|
|
|
void ResetPopups() {
|
|
if (panelHasil != null) panelHasil.SetActive(false);
|
|
if (latarHitam != null) latarHitam.SetActive(false);
|
|
}
|
|
|
|
void WinLevel() { if (gameAktif) { gameAktif = false; StartCoroutine(MunculkanHasil()); } }
|
|
void GameOver() { if (gameAktif) { gameAktif = false; StartCoroutine(MunculkanHasil()); } }
|
|
|
|
IEnumerator MunculkanHasil() {
|
|
// 1. Ambil durasi dari TimeTracker
|
|
float durasiMain = 0;
|
|
TimeTracker tt = FindObjectOfType<TimeTracker>(); // Ini cara nyari stopwatchnya
|
|
if (tt != null) {
|
|
durasiMain = tt.GetDurasiBermain(); // Ambil angka detiknya
|
|
}
|
|
|
|
// 2. Kirim ke SaveSystem barengan sama skor
|
|
if (SaveSystem.instance != null) {
|
|
// Angka 1 itu index Tangkap Sayur, 'score' itu skor sesi ini, 'durasiMain' itu waktunya
|
|
SaveSystem.instance.SimpanHasilGame(1, score, durasiMain);
|
|
}
|
|
|
|
// --- 2. PEMBERSIHAN GAMEPLAY ---
|
|
ObjectSpawner spawner = FindObjectOfType<ObjectSpawner>();
|
|
if (spawner != null) spawner.enabled = false;
|
|
ClearAllObjectsByTag("Sayur");
|
|
ClearAllObjectsByTag("JunkFood");
|
|
|
|
// --- 3. LOGIKA BINTANG & PLAYERPREFS ---
|
|
int jumlahBintang = 0;
|
|
if (score >= targetScore) {
|
|
jumlahBintang = health;
|
|
} else {
|
|
if (score >= targetScore / 2) jumlahBintang = 1;
|
|
else jumlahBintang = 0;
|
|
}
|
|
|
|
string namaScene = SceneManager.GetActiveScene().name;
|
|
int bintangLama = PlayerPrefs.GetInt("Bintang_" + namaScene, 0);
|
|
if (jumlahBintang > bintangLama) PlayerPrefs.SetInt("Bintang_" + namaScene, jumlahBintang);
|
|
|
|
if (jumlahBintang >= 3) {
|
|
PlayerPrefs.SetInt("SkinUnlocked_" + idSkinLevelIni, 1);
|
|
int levelTerbukaSkg = PlayerPrefs.GetInt("LevelTerbuka", 1);
|
|
int levelUrutan = 1;
|
|
if(namaScene == "Easy") levelUrutan = 1;
|
|
else if(namaScene == "Medium") levelUrutan = 2;
|
|
else if(namaScene == "Hard") levelUrutan = 3;
|
|
|
|
if (levelUrutan >= levelTerbukaSkg) PlayerPrefs.SetInt("LevelTerbuka", levelUrutan + 1);
|
|
}
|
|
PlayerPrefs.Save();
|
|
|
|
// --- 4. TAMPILKAN BOARD HASIL ---
|
|
if (slotHadiah != null) {
|
|
if (PlayerPrefs.GetInt("SkinUnlocked_" + idSkinLevelIni, 0) == 1) {
|
|
if (SkinData.instance != null) slotHadiah.sprite = SkinData.instance.daftarSkin[idSkinLevelIni].spriteCard;
|
|
} else {
|
|
slotHadiah.sprite = spriteHadiahKosong;
|
|
}
|
|
}
|
|
|
|
if (latarHitam != null) latarHitam.SetActive(true);
|
|
if (panelHasil != null) panelHasil.SetActive(true);
|
|
if (textNilaiAkhir != null) textNilaiAkhir.text = score.ToString();
|
|
|
|
if (buttonNext != null) {
|
|
buttonNext.interactable = (jumlahBintang >= 1);
|
|
buttonNext.image.sprite = buttonNext.interactable ? spriteNextOn : spriteNextOff;
|
|
}
|
|
|
|
// PAUSE GAME (TimeScale 0 dilakukan SETELAH data terkirim)
|
|
AudioSource audio = GetComponent<AudioSource>();
|
|
if(audio != null) audio.ignoreListenerPause = true;
|
|
Time.timeScale = 0;
|
|
|
|
// --- 5. ANIMASI BINTANG ---
|
|
for (int i = 0; i < jumlahBintang; i++) {
|
|
if (i < bintangHasilAkhir.Length && bintangHasilAkhir[i] != null) {
|
|
bintangHasilAkhir[i].sprite = bintangNyala;
|
|
if(audio != null && suaraBintang != null) {
|
|
audio.pitch = 1f + (i * 0.1f);
|
|
audio.PlayOneShot(suaraBintang);
|
|
}
|
|
if(bintangHasilAkhir[i].GetComponent<EfekPopKenyal>()) bintangHasilAkhir[i].GetComponent<EfekPopKenyal>().enabled = true;
|
|
yield return new WaitForSecondsRealtime(0.4f);
|
|
}
|
|
}
|
|
if(audio != null) audio.pitch = 1f;
|
|
}
|
|
|
|
void ClearAllObjectsByTag(string tag) {
|
|
GameObject[] objects = GameObject.FindGameObjectsWithTag(tag);
|
|
foreach (GameObject obj in objects) Destroy(obj);
|
|
}
|
|
|
|
public void TombolNextLevel() {
|
|
Time.timeScale = 1;
|
|
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
|
|
if (currentSceneIndex + 1 < SceneManager.sceneCountInBuildSettings)
|
|
SceneManager.LoadScene(currentSceneIndex + 1);
|
|
else
|
|
SceneManager.LoadScene("LevelGame");
|
|
}
|
|
|
|
public void TombolReplay() {
|
|
Time.timeScale = 1;
|
|
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
|
}
|
|
|
|
public void TombolKembaliMenu() {
|
|
Time.timeScale = 1;
|
|
SceneManager.LoadScene("LevelGame");
|
|
}
|
|
} |