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

109 lines
2.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using UnityEngine;
2024-08-18 22:26:03 +00:00
using UnityEngine.Serialization;
2024-08-18 22:25:33 +00:00
public class DoubleConverter: MonoBehaviour, IResetable
{
public ProductSpawner Spawner;
2024-08-18 22:26:03 +00:00
public List<Product> inputTypeOne;
public List<Product> inputTypeTwo;
public List<Product> refuse;
2024-08-18 22:26:03 +00:00
public ProductType expectedReagentA;
public ProductType expectedReagentB;
public ProductType conversionProduct;
2024-08-18 15:48:35 +00:00
public int CurrentHealth;
public int MaxHealth;
public float conversionDuration = 5f;
private float _conversionTimer;
public Transform refuseLauncher;
2024-08-18 22:26:03 +00:00
public float launchPower = 5f;
public void OnTriggerEnter(Collider other)
{
var product = other.GetComponentInParent<Product>();
2024-08-18 22:26:03 +00:00
product.gameObject.SetActive(false);
if (product.Type == expectedReagentA)
{
inputTypeOne.Add(product);
}
else if (product.Type == expectedReagentB)
{
2024-08-18 22:26:03 +00:00
inputTypeTwo.Add(product);
}
else
{
refuse.Add(product);
}
}
2024-08-18 15:48:35 +00:00
public void Start()
2024-08-18 20:01:15 +00:00
{
ResetMachine();
}
public void ResetMachine()
2024-08-18 15:48:35 +00:00
{
_conversionTimer = conversionDuration;
CurrentHealth = MaxHealth;
2024-08-18 20:01:15 +00:00
2024-08-18 22:26:03 +00:00
inputTypeOne.Clear();
inputTypeTwo.Clear();
refuse.Clear();
2024-08-18 15:48:35 +00:00
}
public void Update()
{
2024-08-18 15:48:35 +00:00
if (CurrentHealth <= 0)
{
return;
}
2024-08-18 22:26:03 +00:00
if (refuse.Count == 0 && inputTypeOne.Count == 0 && inputTypeTwo.Count == 0)
{
return;
}
if (_conversionTimer <= 0f)
{
2024-08-18 22:26:03 +00:00
if (refuse.Count > 0)
{
2024-08-18 22:26:03 +00:00
Expel(refuse[0]);
refuse.RemoveAt(0);
_conversionTimer = conversionDuration;
return;
}
2024-08-18 22:26:03 +00:00
if (inputTypeOne.Count > 0 && inputTypeTwo.Count > 0)
{
2024-08-18 22:26:03 +00:00
DefectType defectType = inputTypeOne[0].Defect & inputTypeTwo[0].Defect;
inputTypeOne.RemoveAt(0);
inputTypeTwo.RemoveAt(0);
Spawner.SpawnProduct(conversionProduct, defectType);
return;
}
2024-08-18 15:48:35 +00:00
_conversionTimer = conversionDuration;
}
_conversionTimer -= Time.deltaTime;
}
private void Expel(Product product)
{
product.transform.position = refuseLauncher.position;
product.transform.rotation = refuseLauncher.rotation;
product.gameObject.SetActive(true);
product.GetComponent<Rigidbody>().velocity = launchPower * refuseLauncher.forward;
}
}