mirror of
https://github.com/nothke/quality-control.git
synced 2025-08-30 23:43:42 +00:00
Reworking item spawn, adding item conversion.
This commit is contained in:
74
Assets/Scripts/Machines/ProductConverter.cs
Normal file
74
Assets/Scripts/Machines/ProductConverter.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Converter: MonoBehaviour
|
||||
{
|
||||
public List<Product> inputProducts;
|
||||
|
||||
public ProductType expectedReagent;
|
||||
public Transform outputPoint;
|
||||
public ProductType conversionProduct;
|
||||
|
||||
public bool isBroken;
|
||||
public float conversionTime = 5f;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (isBroken)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (inputProducts.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_conversionTimer <= 0f)
|
||||
{
|
||||
var currentProduct = inputProducts[0];
|
||||
|
||||
if (inputProducts[0].Type == expectedReagent)
|
||||
{
|
||||
ProductType.SpawnProduct(conversionProduct, outputPoint);
|
||||
inputProducts.RemoveAt(0);
|
||||
Destroy(currentProduct);
|
||||
}
|
||||
else
|
||||
{
|
||||
Expel(inputProducts[0]);
|
||||
inputProducts.RemoveAt(0);
|
||||
}
|
||||
|
||||
_conversionTimer = conversionTime;
|
||||
}
|
||||
|
||||
_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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user