281 lines
6.7 KiB
C#
281 lines
6.7 KiB
C#
using NUnit.Framework;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using JetBrains.Annotations;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using System.Data;
|
|
|
|
|
|
public class Data
|
|
{
|
|
public static int DataLevel, DataScore, DataWaktu, DataNyawa;
|
|
}
|
|
|
|
public class GameSystem : MonoBehaviour
|
|
{
|
|
public static GameSystem instance;
|
|
|
|
//public static bool NewGame = true; //ketika new game akan ngulang data, ketika enggak, akan melanjutkan dengan data yang ada
|
|
int MaxLevel = 10;
|
|
|
|
|
|
[Header("DataPermainan")]
|
|
public bool GameAktif;
|
|
public bool GameSelesai;
|
|
public bool GameFinish; //perubahan
|
|
public int Target,DataSaatIni;
|
|
//public int DataScore, DataWaktu, DataNyawa;
|
|
|
|
[Header("Komponen UI")]
|
|
public TMP_Text Teks_Level;
|
|
public TMP_Text Teks_Score;
|
|
public TMP_Text Teks_Waktu;
|
|
public RectTransform Ui_Darah;
|
|
|
|
[Header("Obj GUI")]
|
|
public GameObject Gui_Pause;
|
|
public GameObject Gui_Transisi;
|
|
|
|
|
|
[Space]
|
|
public bool SistemAcak;
|
|
|
|
[System.Serializable]
|
|
public class DataGame
|
|
{
|
|
public string Nama;
|
|
public Sprite Gambar;
|
|
|
|
}
|
|
|
|
[Header("Settingan Standar")]
|
|
|
|
public DataGame[] DataPermainan;
|
|
|
|
[Space]
|
|
|
|
public ObjTempatDrob[] Drop_Tempat;
|
|
public ObjDrag[] Drag_Obj;
|
|
|
|
//agar script lain dapat mengakses di game system
|
|
private void Awake() //saat pertama kali scene dibuka
|
|
{
|
|
instance = this;
|
|
}
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Start() //objek disable/enable bida diakses lagi
|
|
{
|
|
//AcakSoal();
|
|
GameAktif = false;
|
|
GameSelesai = false;
|
|
GameFinish = false; //tambahan
|
|
SetSoal();
|
|
ResetData();
|
|
Target = 1;
|
|
GameAktif = true;
|
|
}
|
|
|
|
void ResetData()
|
|
{
|
|
if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name == "game0") //mulai dari scene game0
|
|
{
|
|
//NewGame = false;
|
|
Data.DataLevel = 0;
|
|
Data.DataWaktu = 60 * 5;
|
|
Data.DataScore = 0;
|
|
Data.DataNyawa = 5;
|
|
}
|
|
}
|
|
|
|
|
|
float s;
|
|
void Update()
|
|
{
|
|
//if (Input.GetKeyDown(KeyCode.Space))
|
|
// AcakSoal();
|
|
|
|
if (GameAktif && !GameSelesai)
|
|
{
|
|
if (Data.DataWaktu > 0)
|
|
{
|
|
s += Time.deltaTime;
|
|
if (s >= 1)
|
|
{
|
|
Data.DataWaktu--;
|
|
s = 0;
|
|
}
|
|
}
|
|
|
|
if(Data.DataWaktu <= 0)
|
|
{
|
|
GameAktif = false;
|
|
GameSelesai = true;
|
|
GameFinish = false; //tambahan
|
|
|
|
//game kalah
|
|
KumpulanSuara.instance.Panggil_Sfx(5);
|
|
|
|
Gui_Transisi.GetComponent<UiControl>().Btn_Pindah("gameselesai");
|
|
}
|
|
|
|
if(Data.DataNyawa <= 0)
|
|
{
|
|
GameSelesai = true;
|
|
GameAktif = false;
|
|
GameFinish=false; //tambahan
|
|
|
|
//fungsi kalah
|
|
KumpulanSuara.instance.Panggil_Sfx(5);
|
|
|
|
Gui_Transisi.GetComponent<UiControl>().Btn_Pindah("gameselesai");
|
|
}
|
|
|
|
if(DataSaatIni >= Target)
|
|
{
|
|
GameFinish = true; //tambahan
|
|
//GameSelesai = true;
|
|
GameSelesai = false; //perubahan
|
|
GameAktif = false;
|
|
|
|
//kita menang
|
|
|
|
if(Data.DataLevel < (MaxLevel - 1))
|
|
{
|
|
Data.DataLevel++;
|
|
//pindah ke next level\
|
|
|
|
//tanpa transisi
|
|
UnityEngine.SceneManagement.SceneManager.LoadScene("game" + Data.DataLevel);
|
|
|
|
//Gui_Transisi.GetComponent<UiControl>().Btn_Pindah("game" + Data.DataLevel);
|
|
|
|
KumpulanSuara.instance.Panggil_Sfx(3);
|
|
}
|
|
else
|
|
{
|
|
//game selesai pindah ke menu selesai
|
|
//Gui_Transisi.GetComponent<UiControl>().Btn_Pindah("gameselesai");
|
|
Gui_Transisi.GetComponent<UiControl>().Btn_Pindah("gamefinish"); //perubahan
|
|
KumpulanSuara.instance.Panggil_Sfx(4);
|
|
KumpulanSuara.instance.Panggil_Sfx(6);
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
SetInfoUI();
|
|
}
|
|
|
|
|
|
|
|
public void SetSoal()
|
|
{
|
|
for (int i = 0; i < Drag_Obj.Length; i++)
|
|
{
|
|
Drag_Obj[i].ID = i; // ID sesuai urutan data
|
|
Drag_Obj[i].Teks.text = DataPermainan[i].Nama;
|
|
}
|
|
|
|
for (int i = 0; i < Drop_Tempat.Length; i++)
|
|
{
|
|
Drop_Tempat[i].Drob.ID = i; // ID sesuai urutan data
|
|
Drop_Tempat[i].Gambar.sprite = DataPermainan[i].Gambar;
|
|
}
|
|
}
|
|
|
|
//public List<int> _AcakSoal = new List<int>();
|
|
//public List<int> _AcakPos = new List<int>();
|
|
//int rand;
|
|
//int rand2;
|
|
|
|
//public void AcakSoal()
|
|
//{
|
|
// _AcakSoal.Clear();
|
|
// _AcakPos.Clear();
|
|
|
|
// //acak soal
|
|
// _AcakSoal = new List<int>(new int[Drag_Obj.Length]);
|
|
|
|
// for (int i = 0; i < _AcakSoal.Count; i++)
|
|
// {
|
|
// rand = Random.Range(1, DataPermainan.Length);
|
|
// while(_AcakSoal.Contains(rand))
|
|
// rand = Random.Range(1, DataPermainan.Length);
|
|
|
|
// _AcakSoal[i] = rand;
|
|
|
|
// Drag_Obj[i].ID = rand - 1;
|
|
// Drag_Obj[i].Teks.text = DataPermainan[rand - 1].Nama;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// _AcakPos = new List<int>(new int[Drop_Tempat.Length]);
|
|
// for (int i = 0; i < _AcakPos.Count; i++)
|
|
// {
|
|
// rand2 = Random.Range(1, _AcakSoal.Count + 1);
|
|
// while (_AcakPos.Contains(rand2))
|
|
// rand2 = Random.Range(1, _AcakSoal.Count + 1);
|
|
|
|
// _AcakPos[i] = rand2;
|
|
|
|
// Drop_Tempat[i].Drob.ID = _AcakSoal[rand2 - 1] - 1;
|
|
// Drop_Tempat[i].Gambar.sprite = DataPermainan[Drop_Tempat[i].Drob.ID].Gambar;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
|
|
|
// Update is called once per frame
|
|
|
|
|
|
//timmer
|
|
public void SetInfoUI()
|
|
{
|
|
Teks_Level.text = (Data.DataLevel + 1).ToString();
|
|
Teks_Score.text = Data.DataScore.ToString();
|
|
|
|
int Menit = Mathf.FloorToInt(Data.DataWaktu / 60); //01
|
|
int Detik = Mathf.FloorToInt(Data.DataWaktu % 60); //30
|
|
Teks_Waktu.text = Menit.ToString("00") + ":" + Detik.ToString("00");
|
|
|
|
Ui_Darah.sizeDelta = new Vector2(63f * Data.DataNyawa, 63f);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
//btn pause
|
|
public void Btn_Pause(bool pause)
|
|
{
|
|
if (pause)
|
|
{
|
|
GameAktif = false;
|
|
Gui_Pause.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
GameAktif = true;
|
|
Gui_Pause.SetActive(false);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|