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

View File

@@ -6,7 +6,6 @@ using quaternion = Unity.Mathematics.quaternion;
public static class Grid
{
public static float CellSize = 0.25f;
public static float HalfCellSize => CellSize / 2;

View File

@@ -5,39 +5,36 @@ using static Unity.Mathematics.math;
using static Grid;
using Color = UnityEngine.Color;
#if UNITY_EDITOR
[ExecuteAlways]
[SelectionBase]
[DisallowMultipleComponent]
public class GridUnit : MonoBehaviour
{
public int3 Size = new (1, 1, 1);
private Vector3 SizeInMeters => new (Size.x * CellSize, Size.y * CellSize, Size.z * CellSize);
public int3 Size = new(1, 1, 1);
private Vector3 SizeInMeters => new(Size.x * CellSize, Size.y * CellSize, Size.z * CellSize);
public float3 CenterOffset;
[SerializeField, HideInInspector]
private int3 cachedSize = new (1, 1, 1);
[SerializeField, HideInInspector]
private float3 cachedOffset;
private bool isDirty => transform.hasChanged ||
!all(cachedOffset != CenterOffset) ||
[SerializeField, HideInInspector] private int3 cachedSize = new(1, 1, 1);
[SerializeField, HideInInspector] private float3 cachedOffset;
private bool isDirty => transform.hasChanged ||
!all(cachedOffset != CenterOffset) ||
!all(cachedSize == Size);
#if UNITY_EDITOR
public void Update()
{
if (!isDirty)
{
return;
}
SnapUnit(transform, Size, CenterOffset);
transform.hasChanged = false;
cachedOffset = CenterOffset;
cachedSize = Size;
}
@@ -48,11 +45,10 @@ public class GridUnit : MonoBehaviour
Transform t = unit.transform;
quaternion rotation = t.rotation;
var position = (float3)t.position + mul(rotation, unit.CenterOffset);
Gizmos.color = Color.white;
Gizmos.matrix = Matrix4x4.TRS(position, rotation, Vector3.one);
Gizmos.DrawWireCube(Vector3.zero, unit.SizeInMeters);
}
}
#endif
}

View File

@@ -15,14 +15,7 @@ public class ProductReceiver : MonoBehaviour
if (product != null)
{
if (product.Defect != DefectType.None)
{
defectiveProductCount++;
}
else
{
normalProductCount++;
}
Scoreboard.Instance.ScoreProduct(product);
Destroy(product.gameObject);
}