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,16 @@
using UnityEngine;
public class GameAssets: MonoBehaviour
{
public static GameAssets Instance;
public Material MissingMaterial;
private void Start()
{
if (Instance == null)
{
Instance = this;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ea024824a29348d580d9e07817800c84
timeCreated: 1723978793

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))
{

View File

@@ -0,0 +1,16 @@
using System;
[Flags]
public enum DefectType
{
None = 0,
Hex_MissingMaterial_1 = 1 << 0,
Hex_MissingMaterial_2 = 1 << 1,
Hex_MissingMaterial_3 = 1 << 2,
Hex_MissingMaterial_4 = 1 << 3,
Hex_MissingMaterial_5 = 1 << 4,
E_No_Cutout = 1 << 10,
E_Made_6 = 1 << 11,
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: e48fe9fec9d04b89a7d3e8878e55a054
timeCreated: 1723978865

View File

@@ -0,0 +1,29 @@
using System.Collections.Generic;
using UnityEngine;
public class DefectMaterialVisualizer: MonoBehaviour, IDefectVisualizer
{
public MeshRenderer MeshRenderer;
private List<DefectType> supportedDefects = new()
{
DefectType.Hex_MissingMaterial_1,
DefectType.Hex_MissingMaterial_2,
DefectType.Hex_MissingMaterial_3,
DefectType.Hex_MissingMaterial_4,
DefectType.Hex_MissingMaterial_5,
};
public void VisualizeDefect(DefectType defectType)
{
for (var i = 0; i < supportedDefects.Count; i++)
{
if (!defectType.HasFlag(supportedDefects[i]))
{
continue;
}
MeshRenderer.materials[i] = GameAssets.Instance.MissingMaterial;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1432478c6b38458188a508ec961872f2
timeCreated: 1723979252

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using UnityEngine;
public class DefectMeshSelector: MonoBehaviour, IDefectVisualizer
{
public GameObject NormalPrefab;
[Serializable]
public struct PrefabVariant
{
public DefectType DefectType;
public GameObject Prefab;
}
public List<PrefabVariant> DefectivePrefabVariants;
public void VisualizeDefect(DefectType defectType)
{
if (defectType == DefectType.None)
{
NormalPrefab.SetActive(true);
foreach (var variant in DefectivePrefabVariants)
{
variant.Prefab.SetActive(false);
}
}
else
{
NormalPrefab.SetActive(false);
foreach (var variant in DefectivePrefabVariants)
{
variant.Prefab.SetActive(false);
if (defectType.HasFlag(variant.DefectType))
{
variant.Prefab.SetActive(true);
return;
}
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 168e96bc0e2e4ddfa90949e485bcd0cd
timeCreated: 1723979516

View File

@@ -1,6 +0,0 @@
using UnityEngine;
public class DefectiveProduct : Product
{
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 05b73befcbdd4bbfaaee667a8da86b1f
timeCreated: 1723897738

View File

@@ -0,0 +1,4 @@
public interface IDefectVisualizer
{
public void VisualizeDefect(DefectType defectType);
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8ffa14c7e9444d31898f9446de6628ea
timeCreated: 1723979215

View File

@@ -2,5 +2,16 @@
public class Product : MonoBehaviour
{
public ProductType Type;
public DefectType Defect;
public void ApplyDefect(DefectType defectType)
{
Defect = defectType;
foreach (var visualizer in GetComponents<IDefectVisualizer>())
{
visualizer.VisualizeDefect(defectType);
}
}
}

View File

@@ -2,8 +2,8 @@
using UnityEngine;
using Random = UnityEngine.Random;
[CreateAssetMenu(fileName = "New Product Description", menuName = "Data/Product Description", order = 0)]
public class ProductDescription : ScriptableObject
[CreateAssetMenu(fileName = "New Product Spawn Schedule", menuName = "Data/Product Spawn Schedule", order = 0)]
public class ProductSpawnSchedule : ScriptableObject
{
[Serializable]
public struct Product

View File

@@ -0,0 +1,45 @@
using System;
using UnityEngine;
using Random = UnityEngine.Random;
[CreateAssetMenu(fileName = "New Product Type", menuName = "Data/Product Type", order = 0)]
public class ProductType : ScriptableObject
{
public Product Prefab;
[Serializable]
public struct DefectProbability
{
public DefectType Defect;
[Range(0,100)]
public int Probability;
}
public DefectProbability[] DefectProbabilities;
public static void SpawnProduct(ProductType type, Transform origin)
{
var newProduct = Instantiate(type.Prefab, origin);
newProduct.Type = type;
newProduct.ApplyDefect(type.SelectDefect());
}
public DefectType SelectDefect()
{
float randomValue = Random.Range(0, 100);
float sum = 0;
for (var i = 0; i < DefectProbabilities.Length; i++)
{
var defect = DefectProbabilities[i];
sum += defect.Probability;
if (randomValue <= sum)
{
return defect.Defect;
}
}
return DefectType.None;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bcf4ec4eb272446d850eb3c9e72cd87f
timeCreated: 1723979773