quality-control/Assets/Scripts/Machines/TrashBin.cs

99 lines
2.4 KiB
C#
Raw Normal View History

2024-08-17 18:38:25 +00:00
using System.Collections.Generic;
using UnityEngine;
public class TrashBin : MonoBehaviour
{
public float CooldownDuration;
private float _cooldownTimer;
2024-08-19 16:45:27 +00:00
private float _incinerationTimer;
2024-08-17 18:38:25 +00:00
public float IncinerationDuration = 1f;
public float doorVelocity = 5f;
public Transform DoorTransform;
public Transform OpenAnchor;
public Transform ClosedAnchor;
private Vector3 _targetPosition;
2024-08-19 16:45:27 +00:00
public AudioClip destroyClip;
2024-08-17 18:38:25 +00:00
List<Product> enteredProducts = new List<Product>();
public void Start()
{
_targetPosition = OpenAnchor.position;
2024-08-19 16:45:27 +00:00
_incinerationTimer = IncinerationDuration;
2024-08-17 18:38:25 +00:00
}
public void OnTriggerEnter(Collider otherCollider)
{
2024-08-19 16:45:27 +00:00
var product = otherCollider.GetComponentInParent<Product>();
2024-08-17 18:38:25 +00:00
2024-08-19 16:45:27 +00:00
if (product == null)
2024-08-17 18:38:25 +00:00
{
return;
}
2024-08-19 16:45:27 +00:00
if (!enteredProducts.Contains(product))
2024-08-17 18:38:25 +00:00
{
2024-08-19 16:45:27 +00:00
enteredProducts.Add(product);
2024-08-17 18:38:25 +00:00
}
2024-08-19 16:45:27 +00:00
_incinerationTimer = IncinerationDuration;
_targetPosition = ClosedAnchor.position;
2024-08-17 18:38:25 +00:00
}
public void OnTriggerExit(Collider collider)
{
var product = collider.GetComponentInParent<Product>();
if (product)
2024-08-17 18:38:25 +00:00
{
if (enteredProducts.Contains(product))
{
enteredProducts.Remove(product);
}
}
}
private void Update()
{
DoorTransform.position = Vector3.Lerp(
DoorTransform.position,
_targetPosition,
Time.deltaTime * doorVelocity);
2024-08-19 16:45:27 +00:00
if (_cooldownTimer > 0)
{
_cooldownTimer -= Time.deltaTime;
if (_cooldownTimer <= 0)
{
_targetPosition = OpenAnchor.position;
}
return;
}
if (enteredProducts.Count == 0)
2024-08-17 18:38:25 +00:00
{
return;
}
_incinerationTimer -= Time.deltaTime;
if (_incinerationTimer <= 0)
{
foreach (var product in enteredProducts)
{
Destroy(product.gameObject);
}
enteredProducts.Clear();
2024-08-19 16:45:27 +00:00
NAudio.Play(destroyClip, transform.position, 0.33f);
enteredProducts.Clear();
_cooldownTimer = CooldownDuration;
2024-08-17 18:38:25 +00:00
}
}
}