quality-control/Assets/Scripts/Game Schedule/Scoreboard.cs

245 lines
6.1 KiB
C#
Raw Normal View History

2024-08-20 01:30:10 +00:00
using System.Collections.Generic;
2024-08-19 16:45:27 +00:00
using System.Text;
2024-08-20 01:30:10 +00:00
using KinematicCharacterController.Examples;
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;
2024-08-20 01:30:10 +00:00
public ExampleCharacterController Controller;
public ExamplePlayer Player;
public ExampleCharacterCamera Camera;
public Transform StartPosition;
public Transform StartLook;
public StagingManager.StageEnum CurrentStage;
2024-08-18 16:42:01 +00:00
public LevelObjective CurrentObjective;
2024-08-19 23:43:15 +00:00
public LevelObjective Stage1Objective;
public LevelObjective Stage2Objective;
public LevelObjective Stage3Objective;
2024-08-18 16:42:01 +00:00
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
}
2024-08-19 23:43:15 +00:00
public void StartStage(StagingManager.StageEnum stage)
2024-08-18 20:01:15 +00:00
{
2024-08-20 01:30:10 +00:00
CurrentStage = stage;
2024-08-19 23:43:15 +00:00
switch (stage)
{
case StagingManager.StageEnum.Level1:
SetObjective(Stage1Objective);
break;
case StagingManager.StageEnum.Level2:
SetObjective(Stage2Objective);
break;
case StagingManager.StageEnum.Level3:
SetObjective(Stage3Objective);
break;
}
2024-08-20 01:30:10 +00:00
RestartCurrentStage();
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
2024-08-20 01:30:10 +00:00
public static void RestartCurrentStage()
2024-08-18 20:01:15 +00:00
{
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-20 01:30:10 +00:00
Instance.Controller.Motor.SetPosition(Instance.StartPosition.position);
Instance.Controller.Motor.SetRotation(Instance.StartPosition.rotation);
Instance.Player.enabled = false;
Instance.Camera.transform.rotation = Instance.StartLook.rotation;
Instance.Player.enabled = true;
2024-08-18 20:01:15 +00:00
}
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()
{
2024-08-19 16:45:27 +00:00
StringBuilder sb = new();
2024-08-20 01:30:10 +00:00
int totalSeconds = (int) _timeLeft;
2024-08-19 16:45:27 +00:00
int minutes = (int) _timeLeft / 60;
int seconds = (int) _timeLeft % 60;
2024-08-20 01:30:10 +00:00
sb.Append("Time left: ");
if (totalSeconds < 30)
{
sb.Append("<color=red>");
}
else
{
sb.Append("<color=green>");
}
if (seconds < 10)
{
sb.Append($"{minutes}:0{seconds}");
}
else
{
sb.Append($"{minutes}:{seconds}");
}
sb.Append("</color>\n");
2024-08-19 16:45:27 +00:00
sb.Append("\n");
2024-08-18 16:55:24 +00:00
2024-08-19 16:45:27 +00:00
sb.Append(CurrentObjective.LevelMessage);
sb.Append("\n");
sb.Append("\n");
2024-08-18 16:55:24 +00:00
foreach (var quota in CurrentObjective.Quotas)
{
if (ProductCounts.ContainsKey(quota.Type))
{
2024-08-19 16:45:27 +00:00
sb.Append($"{quota.Type.name}: {TotalCount(quota.Type)}/{quota.Quantity}\n");
2024-08-18 16:55:24 +00:00
}
else
{
2024-08-19 16:45:27 +00:00
sb.Append($"{quota.Type.name}: 0/{quota.Quantity}\n");
2024-08-18 16:55:24 +00:00
}
}
2024-08-19 16:45:27 +00:00
textMesh.text = sb.ToString();
2024-08-18 16:55:24 +00:00
}
2024-08-18 16:42:01 +00:00
public void CountScores()
{
bool success = true;
2024-08-20 01:30:10 +00:00
StringBuilder sb = new();
2024-08-18 16:42:01 +00:00
foreach (var quota in CurrentObjective.Quotas)
{
2024-08-18 16:55:24 +00:00
if (!ProductCounts.ContainsKey(quota.Type))
{
success = false;
2024-08-20 01:30:10 +00:00
sb.Append($"Not a single product of type {quota.Type.name} was produced!\n");
2024-08-18 16:55:24 +00:00
break;
}
2024-08-18 16:42:01 +00:00
if (quota.Quantity > TotalCount(quota.Type))
{
success = false;
2024-08-20 01:30:10 +00:00
sb.Append($"Not enough {quota.Type.name} were produced.\n");
2024-08-18 16:42:01 +00:00
break;
}
Debug.Log($"{quota.Type.name}: {TotalCount(quota.Type)}/{quota.Quantity}");
2024-08-19 21:07:47 +00:00
if (quota.MaxDefectivePercentage < DefectPercentage(quota.Type))
2024-08-18 16:42:01 +00:00
{
success = false;
2024-08-20 01:30:10 +00:00
sb.Append($"Too many of {quota.Type.name} were defective!\n");
2024-08-18 16:42:01 +00:00
break;
}
2024-08-20 01:30:10 +00:00
sb.Append($"Overall {quota.Type.name} defect percentage: {DefectPercentage(quota.Type)}%\n");
2024-08-18 16:42:01 +00:00
}
2024-08-20 01:30:10 +00:00
sb.Append(success ? CurrentObjective.SuccessMessage : CurrentObjective.FailureMessage);
UIManager.Instance.PushStageComplete(success, sb.ToString());
2024-08-18 16:42:01 +00:00
}
}