Reworking item spawn, adding item conversion.

This commit is contained in:
Khauvinkh
2024-08-18 14:44:44 +02:00
parent d2c3dff101
commit 683f839905
50 changed files with 2219 additions and 766 deletions

View 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;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 648bc447374048b0a38c2369cbb3939e
timeCreated: 1723978325

View File

@@ -19,16 +19,19 @@ public class ProductReceiver : MonoBehaviour
}
//enteredBodies.Add(rb);
if (rb.GetComponent<DefectiveProduct>())
if (rb.TryGetComponent(out Product product))
{
defectiveProductCount++;
}
else
{
normalProductCount++;
}
if (product.Defect != DefectType.None)
{
defectiveProductCount++;
}
else
{
normalProductCount++;
}
Destroy(rb.gameObject);
Destroy(rb.gameObject);
}
}
}

View File

@@ -14,7 +14,7 @@ public class ProductSpawner : MonoBehaviour
public struct ProductionPhase
{
public ProductionPhaseType Type;
public ProductDescription Description;
public ProductType ProductType;
[Min(1f)]
public float Duration;
[Min(1f)]
@@ -62,7 +62,7 @@ public class ProductSpawner : MonoBehaviour
_spawnTimer = currentPhase.SpawnInterval;
Instantiate(currentPhase.Description.GetRandomProduct(), transform.position, Quaternion.identity);
ProductType.SpawnProduct(currentPhase.ProductType, transform);
}
if (_remainingDuration <= 0)

View File

@@ -12,20 +12,20 @@ public class StationaryDefectDetector : MonoBehaviour
[Range(0, 100)]
public int FalseNegativeChance;
public List<Product> _detectedDefects;
public List<Product> _knownProducts;
private void OnTriggerEnter(Collider other)
{
if (other.TryGetComponent(out Product product))
{
if (_detectedDefects.Contains(product))
if (_knownProducts.Contains(product))
{
return;
}
_detectedDefects.Add(product);
_knownProducts.Add(product);
if (other.TryGetComponent(out DefectiveProduct defectiveProduct))
if (product.Defect == DefectType.None)
{
var falseNegativeRoll = Random.Range(0, 100);
@@ -48,11 +48,11 @@ public class StationaryDefectDetector : MonoBehaviour
private void OnTriggerExit(Collider other)
{
if (other.TryGetComponent(out DefectiveProduct product))
if (other.TryGetComponent(out Product product))
{
if (_detectedDefects.Contains(product))
if (_knownProducts.Contains(product))
{
_detectedDefects.Remove(product);
_knownProducts.Remove(product);
}
}
}

View File

@@ -24,14 +24,16 @@ public class TrashBin : MonoBehaviour
public void OnTriggerEnter(Collider otherCollider)
{
var rb = otherCollider.GetComponent<Rigidbody>();
var rb = otherCollider.GetComponentInParent<Rigidbody>();
if (!rb)
{
return;
}
if (otherCollider.TryGetComponent(out Product product))
var product = otherCollider.GetComponentInParent<Product>();
if (product)
{
if (!enteredProducts.Contains(product))
{
@@ -45,7 +47,9 @@ public class TrashBin : MonoBehaviour
public void OnTriggerExit(Collider collider)
{
if (collider.TryGetComponent(out Product product))
var product = collider.GetComponentInParent<Product>();
if (product)
{
if (enteredProducts.Contains(product))
{