2024-08-18 16:42:01 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2024-08-18 16:55:24 +00:00
|
|
|
|
using TMPro;
|
2024-08-18 20:01:15 +00:00
|
|
|
|
using UnityEditor;
|
2024-08-18 16:42:01 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
public class Scoreboard: MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public static Scoreboard Instance;
|
|
|
|
|
public LevelObjective CurrentObjective;
|
|
|
|
|
|
|
|
|
|
public Dictionary<ProductType, Vector2Int> ProductCounts = new ();
|
|
|
|
|
|
|
|
|
|
public float _timeLeft;
|
|
|
|
|
public bool _running;
|
2024-08-18 16:55:24 +00:00
|
|
|
|
|
|
|
|
|
public TextMeshPro textMesh;
|
2024-08-18 20:01:15 +00:00
|
|
|
|
|
|
|
|
|
public void OnEnable()
|
2024-08-18 16:42:01 +00:00
|
|
|
|
{
|
|
|
|
|
if (!Instance)
|
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
}
|
2024-08-18 20:01:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Start()
|
|
|
|
|
{
|
2024-08-18 16:42:01 +00:00
|
|
|
|
SetObjective(CurrentObjective);
|
2024-08-18 16:55:24 +00:00
|
|
|
|
UpdateText();
|
2024-08-18 16:42:01 +00:00
|
|
|
|
}
|
2024-08-18 20:01:15 +00:00
|
|
|
|
|
|
|
|
|
#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();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-18 16:42:01 +00:00
|
|
|
|
public void SetObjective(LevelObjective objective)
|
|
|
|
|
{
|
|
|
|
|
CurrentObjective = objective;
|
2024-08-18 16:55:24 +00:00
|
|
|
|
|
|
|
|
|
StagingManager.SetStage(objective.Stage);
|
2024-08-18 16:42:01 +00:00
|
|
|
|
|
|
|
|
|
ProductCounts = new();
|
|
|
|
|
|
|
|
|
|
_timeLeft = objective.TimeLimit;
|
2024-08-18 16:55:24 +00:00
|
|
|
|
_running = true;
|
2024-08-18 16:42:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ScoreProduct(Product product)
|
|
|
|
|
{
|
|
|
|
|
if (!ProductCounts.ContainsKey(product.Type))
|
|
|
|
|
{
|
|
|
|
|
ProductCounts[product.Type] = new Vector2Int();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var currentCount = ProductCounts[product.Type];
|
|
|
|
|
|
|
|
|
|
if (product.Defect == DefectType.None)
|
|
|
|
|
{
|
|
|
|
|
currentCount.x++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
currentCount.y++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ProductCounts[product.Type] = currentCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int TotalCount(ProductType type)
|
|
|
|
|
{
|
|
|
|
|
return ProductCounts[type].x + ProductCounts[type].y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int DefectPercentage(ProductType type)
|
|
|
|
|
{
|
|
|
|
|
if (TotalCount(type) == 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 100 * ProductCounts[type].y / TotalCount(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (!_running)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_timeLeft -= Time.deltaTime;
|
2024-08-18 16:55:24 +00:00
|
|
|
|
|
|
|
|
|
UpdateText();
|
2024-08-18 16:42:01 +00:00
|
|
|
|
|
|
|
|
|
if (_timeLeft <= 0)
|
|
|
|
|
{
|
|
|
|
|
CountScores();
|
|
|
|
|
_running = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-18 16:55:24 +00:00
|
|
|
|
|
|
|
|
|
public void UpdateText()
|
|
|
|
|
{
|
|
|
|
|
string text = $"Time left: {Mathf.Floor(_timeLeft)}\n";
|
|
|
|
|
|
|
|
|
|
foreach (var quota in CurrentObjective.Quotas)
|
|
|
|
|
{
|
|
|
|
|
if (ProductCounts.ContainsKey(quota.Type))
|
|
|
|
|
{
|
|
|
|
|
text += $"{quota.Type.name}: {TotalCount(quota.Type)}/{quota.Quantity}\n";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
text += $"{quota.Type.name}: 0/{quota.Quantity}\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
textMesh.text = text;
|
|
|
|
|
}
|
2024-08-18 16:42:01 +00:00
|
|
|
|
|
|
|
|
|
public void CountScores()
|
|
|
|
|
{
|
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
|
|
foreach (var quota in CurrentObjective.Quotas)
|
|
|
|
|
{
|
2024-08-18 16:55:24 +00:00
|
|
|
|
if (!ProductCounts.ContainsKey(quota.Type))
|
|
|
|
|
{
|
|
|
|
|
success = false;
|
|
|
|
|
Debug.LogError($"Not enough {quota.Type.name}");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-18 16:42:01 +00:00
|
|
|
|
if (quota.Quantity > TotalCount(quota.Type))
|
|
|
|
|
{
|
|
|
|
|
success = false;
|
|
|
|
|
Debug.LogError($"Not enough {quota.Type.name}");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Log($"{quota.Type.name}: {TotalCount(quota.Type)}/{quota.Quantity}");
|
|
|
|
|
|
|
|
|
|
if (CurrentObjective.MaxDefectivePercentage < DefectPercentage(quota.Type))
|
|
|
|
|
{
|
|
|
|
|
success = false;
|
|
|
|
|
Debug.LogError($"Too many broken {quota.Type.name}");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Log($"{quota.Type.name}: Quality level: {DefectPercentage(quota.Type)}%");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Log(success? CurrentObjective.SuccessMessage : CurrentObjective.FailureMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|