EcoQuest/Assets/Script/ItemSlot.cs

71 lines
2.5 KiB
C#

using UnityEngine;
using UnityEngine.EventSystems;
public class ItemSlot : MonoBehaviour, IDropHandler
{
[SerializeField] private TrashManager trashManager;
// Tipe sampah yang diterima oleh slot ini
[SerializeField] private TrashType requiredType;
[SerializeField] private AudioSource audioSource;
[SerializeField] private AudioClip correctClip;
[SerializeField] private AudioClip wrongClip;
public void OnDrop(PointerEventData eventData)
{
Debug.Log("On Drop");
if (eventData.pointerDrag != null)
{
// Cek apakah TrashManager sudah diassign
if (trashManager != null)
{
// Cek apakah tipe sampah yang di-drop cocok dengan tipe yang dibutuhkan
if (trashManager.currentTrashType == requiredType)
{
// Mainkan audio "correct" jika tersedia
if (audioSource != null && correctClip != null)
{
audioSource.PlayOneShot(correctClip);
}
// Jika tipe cocok, reset posisi dan tambah skor
DragDrop dragDropScript = eventData.pointerDrag.GetComponent<DragDrop>();
if (dragDropScript != null)
{
dragDropScript.SetDroppedSuccessful();
dragDropScript.ResetPosition();
}
// Tambah skor (misalnya +10)
ScoreScript.scoreValue += 10;
// Ganti sprite sampah dengan yang baru
trashManager.SetNextTrash();
}
else
{
// Mainkan audio "wrong" jika tersedia
if (audioSource != null && wrongClip != null)
{
audioSource.PlayOneShot(wrongClip);
}
// Jika tipe tidak cocok, tampilkan log bahwa pilihan salah
Debug.Log("Salah! Sampah yang kamu drop tidak sesuai dengan jenis tempat sampah.");
GameAManager gameAManager = FindObjectOfType<GameAManager>();
if (gameAManager != null)
{
gameAManager.ShowFailure();
}
}
}
else
{
Debug.LogWarning("TrashManager belum diassign di ItemSlot.");
}
}
}
}