Ekosistem_Game/Assets/Script/ManagerMencocokan.cs

382 lines
12 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using TMPro;
using UnityEngine.SceneManagement;
public class ManagerMencocokan : MonoBehaviour
{
[Header("--- PANEL NAVIGASI & POP UP ---")]
public GameObject panelPilihLevel;
public GameObject panelGameMencocokan;
public GameObject panelPopUpBenar;
public static bool mauUlangGame = false;
public static bool mauKembaliKePeta = false;
public static bool sudahPernahSapaCocok = false;
[Header("--- KOLEKSI SUARA MENCOCOKAN ---")]
public AudioSource speakerGame;
public AudioClip kasetNarator;
public AudioClip kasetBenar;
public AudioClip kasetSalah;
public AudioClip kasetPopUpHebat;
public AudioClip kasetPanelHasil;
public AudioClip kasetKlikTombol;
public AudioClip kasetKalahWaktu;
[Header("--- DAFTAR STAGE (EMPTY OBJECT) ---")]
public GameObject[] daftarStage;
public int[] targetGarisPerStage;
private int stageSekarang = 0;
private int garisBenarDiStageIni = 0;
private int totalGarisBenarGlobal = 0;
private int totalTargetSemuaStage = 0;
private bool gameSelesai = false;
[Header("--- SISTEM SKOR KIRI ---")]
public Image barSkor;
public TextMeshProUGUI teksSkorKiri;
public Image[] bintangSkorProgress;
[Header("--- PENGATURAN WAKTU ---")]
public float durasiWaktuMain = 60f;
private float waktuSisa;
public Image barWaktu;
private bool waktuBolehJalan = false;
[Header("--- PANEL HASIL AKHIR & ANIMASI ---")]
public GameObject panelHasil;
public RectTransform kontenPanelHasil;
public Image[] slotBintangPanel;
public TextMeshProUGUI teksSkorAkhir;
[Header("--- FITUR ANTI-KLIK ---")]
public CanvasGroup groupKlikGame;
void Start()
{
StopAllCoroutines();
if (speakerGame != null) speakerGame.Stop();
waktuSisa = durasiWaktuMain;
gameSelesai = false;
waktuBolehJalan = false;
totalGarisBenarGlobal = 0;
stageSekarang = 0;
garisBenarDiStageIni = 0;
if (groupKlikGame != null) groupKlikGame.blocksRaycasts = true;
totalTargetSemuaStage = 0;
foreach (int t in targetGarisPerStage) totalTargetSemuaStage += t;
if (panelHasil != null) panelHasil.SetActive(false);
if (panelPopUpBenar != null) panelPopUpBenar.SetActive(false);
if (barSkor != null) barSkor.fillAmount = 0;
if (teksSkorKiri != null) teksSkorKiri.text = "0";
foreach (Image b in bintangSkorProgress) if(b != null) b.color = Color.gray;
foreach (Image b in slotBintangPanel) if(b != null) b.color = Color.gray;
if (barWaktu != null) { barWaktu.fillAmount = 1; barWaktu.color = Color.white; }
UpdateTampilanStage();
if (mauUlangGame)
{
if (panelPilihLevel != null) panelPilihLevel.SetActive(false);
if (panelGameMencocokan != null) panelGameMencocokan.SetActive(true);
mauUlangGame = false;
MulaiSuaraNaratorSatuPintu();
}
else if (mauKembaliKePeta)
{
if (panelPilihLevel != null) panelPilihLevel.SetActive(true);
if (panelGameMencocokan != null) panelGameMencocokan.SetActive(false);
mauKembaliKePeta = false;
}
}
public void MulaiSuaraNaratorSatuPintu()
{
StopAllCoroutines();
if (speakerGame != null)
{
speakerGame.Stop();
StartCoroutine(LogikaTungguNarator());
}
}
IEnumerator LogikaTungguNarator()
{
if (groupKlikGame != null) groupKlikGame.blocksRaycasts = false;
waktuBolehJalan = false;
yield return new WaitForSeconds(0.2f);
if (!sudahPernahSapaCocok)
{
if (kasetNarator != null)
{
speakerGame.PlayOneShot(kasetNarator);
yield return new WaitForSeconds(kasetNarator.length);
sudahPernahSapaCocok = true;
}
}
if (groupKlikGame != null) groupKlikGame.blocksRaycasts = true;
waktuBolehJalan = true;
}
public void MasukGameDanNgomongCocok()
{
ManagerSuaraBermain satpamMenu = FindObjectOfType<ManagerSuaraBermain>();
if (satpamMenu != null) satpamMenu.HentikanPaksaSuara();
if (panelPilihLevel != null) panelPilihLevel.SetActive(false);
if (panelGameMencocokan != null) panelGameMencocokan.SetActive(true);
MulaiSuaraNaratorSatuPintu();
}
void Update()
{
if (gameSelesai) return;
if (!waktuBolehJalan) return;
if (waktuSisa > 0) {
waktuSisa -= Time.deltaTime;
if (barWaktu != null) {
barWaktu.fillAmount = waktuSisa / durasiWaktuMain;
if (waktuSisa <= 10f) barWaktu.color = Color.red;
}
} else {
waktuSisa = 0;
if (barWaktu != null) barWaktu.fillAmount = 0;
GagalMain();
}
}
public void JawabanSalah()
{
if (gameSelesai) return;
if (speakerGame != null && kasetSalah != null)
{
speakerGame.Stop();
speakerGame.clip = kasetSalah;
speakerGame.Play();
}
}
public void GarisTersambung(AudioClip suaraEkosistem = null)
{
if (gameSelesai) return;
if (speakerGame != null && kasetBenar != null)
{
speakerGame.Stop();
speakerGame.clip = kasetBenar;
speakerGame.Play();
}
if (speakerGame != null && suaraEkosistem != null)
{
StartCoroutine(JedaNarasiEkosistem(suaraEkosistem));
}
garisBenarDiStageIni++;
totalGarisBenarGlobal++;
UpdateUISkorKiri();
if (garisBenarDiStageIni >= targetGarisPerStage[stageSekarang]) {
StartCoroutine(PindahStageNext());
}
}
IEnumerator JedaNarasiEkosistem(AudioClip clip)
{
yield return new WaitForSeconds(0.4f);
if (speakerGame != null && speakerGame.clip == kasetBenar)
{
speakerGame.Stop();
speakerGame.clip = clip;
speakerGame.Play();
}
}
IEnumerator PindahStageNext()
{
if (groupKlikGame != null) groupKlikGame.blocksRaycasts = false;
waktuBolehJalan = false;
yield return new WaitForSeconds(0.4f);
if (panelPopUpBenar != null) panelPopUpBenar.SetActive(true);
// =======================================================
// REVISI: DIKEMBALIKAN KE PLAYONESHOT BIAR SUARA BARENGAN
// =======================================================
if (speakerGame != null && kasetPopUpHebat != null)
{
// Tidak pakai speakerGame.Stop() lagi di sini
speakerGame.PlayOneShot(kasetPopUpHebat);
}
yield return new WaitForSeconds(1.5f);
if (panelPopUpBenar != null) panelPopUpBenar.SetActive(false);
if (stageSekarang < daftarStage.Length - 1) {
GameObject stageLama = daftarStage[stageSekarang];
float animT = 0;
while(animT < 1f){
animT += Time.deltaTime * 5f;
stageLama.transform.localScale = Vector3.Lerp(Vector3.one, Vector3.zero, animT);
yield return null;
}
stageLama.SetActive(false);
stageSekarang++;
garisBenarDiStageIni = 0;
daftarStage[stageSekarang].transform.localScale = Vector3.zero;
UpdateTampilanStage();
animT = 0;
while(animT < 1f){
animT += Time.deltaTime * 4f;
float curve = Mathf.Sin(animT * Mathf.PI * 0.7f) * 1.1f;
daftarStage[stageSekarang].transform.localScale = new Vector3(curve, curve, curve);
yield return null;
}
daftarStage[stageSekarang].transform.localScale = Vector3.one;
if (groupKlikGame != null) groupKlikGame.blocksRaycasts = true;
waktuBolehJalan = true;
} else {
if (daftarStage[stageSekarang] != null) daftarStage[stageSekarang].SetActive(false);
MenangMain();
}
}
void UpdateTampilanStage()
{
for (int i = 0; i < daftarStage.Length; i++) {
if (daftarStage[i] != null) daftarStage[i].SetActive(i == stageSekarang);
}
}
void UpdateUISkorKiri()
{
if (totalTargetSemuaStage == 0) return;
float persentase = (float)totalGarisBenarGlobal / totalTargetSemuaStage;
if (barSkor != null) barSkor.fillAmount = persentase;
if (teksSkorKiri != null) teksSkorKiri.text = Mathf.RoundToInt(persentase * 100f).ToString();
if (bintangSkorProgress.Length >= 3) {
if (persentase >= 0.33f && bintangSkorProgress[0] != null) bintangSkorProgress[0].color = Color.white;
if (persentase >= 0.66f && bintangSkorProgress[1] != null) bintangSkorProgress[1].color = Color.white;
if (persentase >= 1.00f && bintangSkorProgress[2] != null) bintangSkorProgress[2].color = Color.white;
}
}
void MenangMain()
{
if (gameSelesai) return;
gameSelesai = true;
waktuBolehJalan = false;
StartCoroutine(AnimasiPanelDanBintang(3, false));
}
void GagalMain()
{
if (gameSelesai) return;
gameSelesai = true;
waktuBolehJalan = false;
if (stageSekarang < daftarStage.Length && daftarStage[stageSekarang] != null) {
daftarStage[stageSekarang].SetActive(false);
}
StartCoroutine(AnimasiPanelDanBintang(HitungBintang(), true));
}
int HitungBintang()
{
float persentase = (float)totalGarisBenarGlobal / totalTargetSemuaStage;
if (persentase >= 1.00f) return 3;
else if (persentase >= 0.66f) return 2;
else if (persentase >= 0.33f) return 1;
return 0;
}
IEnumerator AnimasiPanelDanBintang(int jumlahBintang, bool waktuHabis)
{
if (groupKlikGame != null) groupKlikGame.blocksRaycasts = true;
if (kontenPanelHasil != null)
{
kontenPanelHasil.localScale = Vector3.zero;
panelHasil.SetActive(true);
panelHasil.transform.SetAsLastSibling();
float t = 0;
while (t < 1f)
{
t += Time.deltaTime * 4f;
float curve = Mathf.Sin(t * Mathf.PI * 0.7f) * 1.15f;
kontenPanelHasil.localScale = new Vector3(curve, curve, curve);
yield return null;
}
kontenPanelHasil.localScale = Vector3.one;
}
else { panelHasil.SetActive(true); }
if (teksSkorAkhir != null) teksSkorAkhir.text = teksSkorKiri.text;
if (waktuHabis) {
if (speakerGame != null && kasetKalahWaktu != null) speakerGame.PlayOneShot(kasetKalahWaktu);
} else {
if (speakerGame != null && kasetPanelHasil != null) speakerGame.PlayOneShot(kasetPanelHasil);
}
yield return new WaitForSeconds(0.3f);
for (int i = 0; i < jumlahBintang; i++)
{
if (i < slotBintangPanel.Length && slotBintangPanel[i] != null)
{
if (speakerGame != null && kasetBenar != null) speakerGame.PlayOneShot(kasetBenar);
slotBintangPanel[i].color = Color.white;
float bT = 0;
Vector3 startScale = Vector3.one * 2f;
while(bT < 1f){
bT += Time.deltaTime * 5f;
slotBintangPanel[i].rectTransform.localScale = Vector3.Lerp(startScale, Vector3.one, bT);
yield return null;
}
yield return new WaitForSeconds(0.4f);
}
}
}
public void TombolUlangi() {
if (speakerGame != null && kasetKlikTombol != null) speakerGame.PlayOneShot(kasetKlikTombol);
sudahPernahSapaCocok = false;
mauUlangGame = true;
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
public void TombolKeluar() {
if (speakerGame != null && kasetKlikTombol != null) speakerGame.PlayOneShot(kasetKlikTombol);
sudahPernahSapaCocok = false;
mauKembaliKePeta = true;
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}