74 lines
1.9 KiB
C#
74 lines
1.9 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class TrashManager : MonoBehaviour
|
|
{
|
|
[SerializeField] private List<TrashItem> trashItems = new List<TrashItem>(); // 20 item tetap di Inspector
|
|
[SerializeField] private Image trashImage;
|
|
|
|
public TrashType currentTrashType { get; private set; }
|
|
|
|
private void Start()
|
|
{
|
|
// Acak daftar dengan Fisher-Yates Shuffle
|
|
ShuffleTrashItems();
|
|
|
|
// Ambil hanya 10 item pertama setelah diacak
|
|
trashItems = trashItems.GetRange(0, 10);
|
|
|
|
// Sembunyikan image dengan mengatur alpha ke 0
|
|
if (trashImage != null)
|
|
{
|
|
trashImage.color = new Color(1, 1, 1, 0);
|
|
}
|
|
|
|
// Tampilkan item pertama
|
|
SetNextTrash();
|
|
}
|
|
|
|
private void ShuffleTrashItems()
|
|
{
|
|
int n = trashItems.Count;
|
|
for (int i = n - 1; i > 0; i--)
|
|
{
|
|
int j = Random.Range(0, i + 1); // Ambil indeks acak
|
|
TrashItem temp = trashItems[i];
|
|
trashItems[i] = trashItems[j];
|
|
trashItems[j] = temp;
|
|
}
|
|
}
|
|
|
|
public void SetNextTrash()
|
|
{
|
|
if (trashItems.Count > 0)
|
|
{
|
|
int index = Random.Range(0, trashItems.Count);
|
|
TrashItem nextTrash = trashItems[index];
|
|
|
|
if (trashImage != null)
|
|
{
|
|
trashImage.sprite = nextTrash.sprite;
|
|
trashImage.color = Color.white;
|
|
|
|
currentTrashType = nextTrash.trashType;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("TrashImage belum diassign!");
|
|
}
|
|
|
|
trashItems.RemoveAt(index);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Semua sprite sampah telah digunakan!");
|
|
GameAManager gameAManager = FindObjectOfType<GameAManager>();
|
|
if (gameAManager != null)
|
|
{
|
|
gameAManager.ShowSuccess();
|
|
}
|
|
}
|
|
}
|
|
}
|