ici_recycle_vr/Assets/Scripts/TrashBin.cs
Diaconescu Andrei-Alexandru 19674bfe5d Am adaugat ICI Recycle
2025-05-26 12:54:36 +03:00

161 lines
5.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<Tutorial>();
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<Tutorial>();
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<Tutorial>();
if (tutorialRef != null)
{
tutorialRef.OutputText("Corect boss, ești pe val!");
}
}
else
{
Debug.Log("Gunoi greșit");
Tutorial tutorialRef = FindObjectOfType<Tutorial>();
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<Transform>();
return RecursiveSearch(target, visited);
}
private Trash RecursiveSearch(Transform target, HashSet<Transform> visited)
{
if (target == null || visited.Contains(target))
return null;
visited.Add(target);
// verifică pe obiectul curent
Trash trash = target.GetComponent<Trash>();
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<Trash>();
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;
}
}