using System.Collections; using System.Collections.Generic; using UnityEngine; public class TrashBin : MonoBehaviour { [Header("Trash Bin Settings")] public TrashBinType trashBinType; private Vector3 initialPosition; private Quaternion initialRotation; private void Start() { initialPosition = transform.position; initialRotation = transform.rotation; } private void FixedUpdate() { transform.position = Vector3.Lerp(transform.position, initialPosition, Time.deltaTime * 5); transform.rotation = Quaternion.Lerp(transform.rotation, initialRotation, Time.deltaTime * 5); } private void OnTriggerEnter(Collider other) { Trash trash = FindTrashComponent(other.transform); if (trash == null || trash.hasBeenCollected) { Debug.Log("No Trash component found or already collected"); return; } trash.hasBeenCollected = true; if (Tutorial.isInTutorial) { Tutorial tutorial = FindObjectOfType(); if (trash.trashType == trashBinType) { if (trash.liquidVolume != null && trash.liquidVolume.level != 0) { tutorial.OutputText("BA BOULE, AI UITAT SĂ GOLEȘTI LICHIDU’!\n\nARUNCĂ GUNOIU’ CA LUMEA!"); tutorial.PlayStep(); StartCoroutine(WaitAndDestroy(other)); return; } tutorial.OutputText("Bravo boss! Asta era la fix."); tutorial.NextStep(); } else { tutorial.OutputText($"AI ARUNCAT LA {trashBinType.ToString().ToUpper()}, dar trebuia să-l duci la {trash.trashType.ToString().ToUpper()}!\n\nFii atent boss, încearcă din nou."); tutorial.PlayStep(); } StartCoroutine(WaitAndDestroy(other)); return; } else { if (trash.trashType == trashBinType) { if (trash.liquidVolume != null && trash.liquidVolume.level != 0) { Debug.Log("Gunoi corect, DAR AVEA LICHID"); // Tung Tung tot ceartă Tutorial tutorial = FindObjectOfType(); if (tutorial != null) { tutorial.OutputText("Ai aruncat bine... da’ ce facem cu LICHIDU', șefu'?\n\nTrebuie să fie gol, altfel strici reciclarea!"); } StartCoroutine(WaitAndDestroy(other)); return; } Debug.Log("Gunoi corect"); Tutorial tutorialRef = FindObjectOfType(); if (tutorialRef != null) { tutorialRef.OutputText("Corect boss, ești pe val!"); } } else { Debug.Log("Gunoi greșit"); Tutorial tutorialRef = FindObjectOfType(); if (tutorialRef != null) { tutorialRef.OutputText($"Ai pus gunoiul la {trashBinType.ToString().ToUpper()} dar ăla e pentru altceva!\nGunoiul tău era pentru {trash.trashType.ToString().ToUpper()}.\n\nConcentrează-te, bossule!"); } } StartCoroutine(WaitAndDestroy(other)); } } private IEnumerator WaitAndDestroy(Collider other) { yield return new WaitForSeconds(5f); Destroy(other.transform.root.gameObject); } private Trash FindTrashComponent(Transform target) { var visited = new HashSet(); return RecursiveSearch(target, visited); } private Trash RecursiveSearch(Transform target, HashSet visited) { if (target == null || visited.Contains(target)) return null; visited.Add(target); // verifică pe obiectul curent Trash trash = target.GetComponent(); if (trash != null) return trash; // verifică în copii foreach (Transform child in target) { trash = RecursiveSearch(child, visited); if (trash != null) return trash; } // verifică în părinți și toți frații părinților Transform parent = target.parent; while (parent != null) { if (!visited.Contains(parent)) { visited.Add(parent); // pe părinte trash = parent.GetComponent(); if (trash != null) return trash; // pe frații părintelui foreach (Transform sibling in parent) { if (sibling != target && !visited.Contains(sibling)) { trash = RecursiveSearch(sibling, visited); if (trash != null) return trash; } } } parent = parent.parent; } return null; } }