Scoring and stuff.

This commit is contained in:
Khauvinkh
2024-08-18 18:42:01 +02:00
parent 1946669e64
commit 5bcc96a4a0
17 changed files with 587 additions and 42 deletions

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CreateAssetMenu(fileName = "New Level Objective", menuName = "Data/Level Objective", order = 0)]
public class LevelObjective : ScriptableObject
{
public StagingManager.StageEnum Stage;
[Serializable]
public struct ProductQuota
{
public ProductType Type;
public int Quantity;
}
public List<ProductQuota> Quotas;
public float TimeLimit;
[Range(0, 100)]
public int MaxDefectivePercentage;
[TextArea]
public string SuccessMessage;
[TextArea]
public string FailureMessage;
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 65221132d7bd4e79856cce65eeb5ce58
timeCreated: 1723996942

View File

@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
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;
public void Start()
{
if (!Instance)
{
Instance = this;
}
_running = true;
SetObjective(CurrentObjective);
}
public void SetObjective(LevelObjective objective)
{
CurrentObjective = objective;
ProductCounts = new();
_timeLeft = objective.TimeLimit;
}
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;
Debug.Log(_timeLeft);
if (_timeLeft <= 0)
{
CountScores();
_running = false;
}
}
public void CountScores()
{
bool success = true;
foreach (var quota in CurrentObjective.Quotas)
{
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);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 49f7d9fae60a4082a271f0a2fa185f79
timeCreated: 1723996883

View File

@@ -8,6 +8,8 @@ public class StagingManager: MonoBehaviour
public static StagingManager Instance;
public static List<StageProp> StageProps;
public List<LevelObjective> Objectives;
public StageEnum CurrentStage;
[Flags]
@@ -63,13 +65,17 @@ public class StagingManager: MonoBehaviour
}
}
#if UNITY_EDITOR
[MenuItem("Tools/Staging/Level1")]
#endif
public static void SetStage1()
{
SetStage(StageEnum.Level1);
}
#if UNITY_EDITOR
[MenuItem("Tools/Staging/Level2")]
#endif
public static void SetStage2()
{
SetStage(StageEnum.Level2);