2024-08-18 12:44:44 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2024-08-18 20:01:15 +00:00
|
|
|
|
public class Converter: MonoBehaviour, IResetable
|
2024-08-18 12:44:44 +00:00
|
|
|
|
{
|
2024-08-18 21:36:45 +00:00
|
|
|
|
public ProductSpawner Spawner;
|
2024-08-18 12:44:44 +00:00
|
|
|
|
public List<Product> inputProducts;
|
|
|
|
|
|
|
|
|
|
public ProductType expectedReagent;
|
|
|
|
|
public Transform outputPoint;
|
|
|
|
|
public ProductType conversionProduct;
|
|
|
|
|
|
2024-08-18 15:48:35 +00:00
|
|
|
|
public int CurrentHealth;
|
|
|
|
|
public int MaxHealth;
|
|
|
|
|
|
|
|
|
|
public float conversionDuration = 5f;
|
2024-08-18 12:44:44 +00:00
|
|
|
|
private float _conversionTimer;
|
|
|
|
|
|
|
|
|
|
public Transform refuseLauncher;
|
|
|
|
|
public float launchPower = 10f;
|
|
|
|
|
|
|
|
|
|
public void OnTriggerEnter(Collider other)
|
|
|
|
|
{
|
|
|
|
|
var product = other.GetComponentInParent<Product>();
|
|
|
|
|
|
|
|
|
|
if (product)
|
|
|
|
|
{
|
|
|
|
|
product.gameObject.SetActive(false);
|
|
|
|
|
inputProducts.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
|
|
|
|
|
|
|
|
|
inputProducts.Clear();
|
2024-08-18 15:48:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-18 12:44:44 +00:00
|
|
|
|
public void Update()
|
|
|
|
|
{
|
2024-08-18 15:48:35 +00:00
|
|
|
|
if (CurrentHealth <= 0)
|
2024-08-18 12:44:44 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (inputProducts.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_conversionTimer <= 0f)
|
|
|
|
|
{
|
|
|
|
|
var currentProduct = inputProducts[0];
|
|
|
|
|
|
|
|
|
|
if (inputProducts[0].Type == expectedReagent)
|
|
|
|
|
{
|
2024-08-18 21:36:45 +00:00
|
|
|
|
Spawner.SpawnProduct(conversionProduct);
|
2024-08-18 12:44:44 +00:00
|
|
|
|
inputProducts.RemoveAt(0);
|
|
|
|
|
Destroy(currentProduct);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Expel(inputProducts[0]);
|
|
|
|
|
inputProducts.RemoveAt(0);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-18 15:48:35 +00:00
|
|
|
|
_conversionTimer = conversionDuration;
|
2024-08-18 12:44:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_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;
|
|
|
|
|
}
|
|
|
|
|
}
|