Fixed level loading, added level reset

This commit is contained in:
Daniel Tyomin
2024-08-18 22:01:15 +02:00
parent 3036732a0d
commit 70d7cab3b9
13 changed files with 106 additions and 239 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using TMPro;
using UnityEditor;
using UnityEngine;
public class Scoreboard: MonoBehaviour
@@ -14,18 +15,41 @@ public class Scoreboard: MonoBehaviour
public bool _running;
public TextMeshPro textMesh;
public void Start()
public void OnEnable()
{
if (!Instance)
{
Instance = this;
}
}
public void Start()
{
SetObjective(CurrentObjective);
UpdateText();
}
#if UNITY_EDITOR
[MenuItem("Tools/Restart Level")]
#endif
public static void RestartLevel()
{
foreach (var machine in StagingManager.Instance.GetComponentsInChildren<IResetable>(true))
{
machine.ResetMachine();
}
var allProducts = FindObjectsOfType<Product>();
for (int i = 0; i < allProducts.Length; i++)
{
Destroy(allProducts[i].gameObject);
}
Instance.SetObjective(Instance.CurrentObjective);
Instance.UpdateText();
}
public void SetObjective(LevelObjective objective)
{
CurrentObjective = objective;

View File

@@ -3,15 +3,4 @@
public class StageProp: MonoBehaviour
{
public StagingManager.StageEnum ActiveAtStages;
public void Start()
{
StagingManager.RegisterStageProp(this);
gameObject.SetActive(false);
}
public void OnDestroy()
{
StagingManager.RemoveStageProp(this);
}
}

View File

@@ -6,7 +6,6 @@ using UnityEngine;
public class StagingManager: MonoBehaviour
{
public static StagingManager Instance;
public static List<StageProp> StageProps;
public List<LevelObjective> Objectives;
@@ -15,8 +14,9 @@ public class StagingManager: MonoBehaviour
[Flags]
public enum StageEnum
{
Level1 = 1,
Level2 = 2,
Level1 = 1 << 0,
Level2 = 1 << 1,
Level3 = 1 << 2,
}
public void OnEnable()
@@ -26,40 +26,15 @@ public class StagingManager: MonoBehaviour
Instance = this;
}
}
public void OnDestroy()
{
StageProps.Clear();
}
public static void RegisterStageProp(StageProp stageProp)
{
if (StageProps == null)
{
StageProps = new List<StageProp>();
}
if (!StageProps.Contains(stageProp))
{
StageProps.Add(stageProp);
}
}
public static void RemoveStageProp(StageProp stageProp)
{
if (StageProps.Contains(stageProp))
{
StageProps.Remove(stageProp);
}
}
public static void SetStage(StageEnum stage)
{
Instance.CurrentStage = stage;
foreach (var stageProp in StageProps)
foreach (var stageProp in FindObjectsOfType<StageProp>(true))
{
stageProp.gameObject.SetActive((stageProp.ActiveAtStages & stage) != 0);
Debug.Log(stageProp.ActiveAtStages.HasFlag(stage));
stageProp.gameObject.SetActive(stageProp.ActiveAtStages.HasFlag(stage));
}
}

View File

@@ -1,13 +0,0 @@
using UnityEngine;
public class AudioAlarm : MonoBehaviour
{
public float Duration;
private bool _isPlaying;
public void PlayAlarm()
{
Debug.Log("Playing alarm");
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: c996d1be4ecb4e8d92bc4344f7f98854
timeCreated: 1723921340

View File

@@ -0,0 +1,4 @@
public interface IResetable
{
public void ResetMachine();
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6b152b88cf564370bd89748f325284b6
timeCreated: 1724010368

View File

@@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
public class Converter: MonoBehaviour
public class Converter: MonoBehaviour, IResetable
{
public List<Product> inputProducts;
@@ -31,9 +31,16 @@ public class Converter: MonoBehaviour
}
public void Start()
{
ResetMachine();
}
public void ResetMachine()
{
_conversionTimer = conversionDuration;
CurrentHealth = MaxHealth;
inputProducts.Clear();
}
public void Update()

View File

@@ -3,12 +3,7 @@ using System.Collections.Generic;
using UnityEngine;
public class ProductReceiver : MonoBehaviour
{
///HashSet<Rigidbody> enteredBodies = new HashSet<Rigidbody>();
public int normalProductCount;
public int defectiveProductCount;
{
private void OnTriggerEnter(Collider otherCollider)
{
var product = otherCollider.GetComponentInParent<Product>();

View File

@@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
public class ProductSpawner : MonoBehaviour
public class ProductSpawner : MonoBehaviour, IResetable
{
public enum ProductionPhaseType
{
@@ -24,19 +24,34 @@ public class ProductSpawner : MonoBehaviour
}
public List<ProductionPhase> ProductionPhases;
private List<ProductionPhase> RuntimeProductionPhases;
public float _remainingDuration;
public float _spawnTimer;
public void Start()
{
ResetMachine();
}
public void ResetMachine()
{
enabled = true;
_remainingDuration = 0f;
_spawnTimer = 0f;
RuntimeProductionPhases = new(ProductionPhases);
}
private void Update()
{
if (ProductionPhases.Count == 0)
if (RuntimeProductionPhases.Count == 0)
{
enabled = false;
return;
}
var currentPhase = ProductionPhases[0];
var currentPhase = RuntimeProductionPhases[0];
if (_remainingDuration <= 0)
{
@@ -54,7 +69,7 @@ public class ProductSpawner : MonoBehaviour
{
if (_remainingDuration <= 0)
{
ProductionPhases.RemoveAt(0);
RuntimeProductionPhases.RemoveAt(0);
}
return;
@@ -67,7 +82,7 @@ public class ProductSpawner : MonoBehaviour
if (_remainingDuration <= 0)
{
ProductionPhases.RemoveAt(0);
RuntimeProductionPhases.RemoveAt(0);
}
}
}

View File

@@ -3,12 +3,17 @@ using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class StationaryDefectDetector : MonoBehaviour
public class StationaryDefectDetector : MonoBehaviour, IResetable
{
public List<Product> _knownProducts;
public AudioClip goodSound;
public AudioClip badSound;
public void ResetMachine()
{
_knownProducts.Clear();
}
private void OnTriggerEnter(Collider other)
{