Files
ichni_Official/Assets/Scripts/GameUI/ResultGraph.cs
SoulliesOfficial 7580c4d87c 大更
2026-03-14 03:13:10 -04:00

93 lines
2.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Ichni.RhythmGame
{
public class ResultGraph : MonoBehaviour
{
public List<Image> pillars;
public TMP_Text averageText;
public float maxPillarHeight = 150f;
public static List<(int, int)> ranges = new List<(int, int)>()
{
(int.MinValue, -150), //Miss
(-150, -125), //Bad
(-125, -100), //Good
(-100, -75), (-75, -50), (-50, 50), (50, 75), (75, 100), //Perfect
(100, 125), //Good
(125, 150), //Bad
(150, int.MaxValue) //Miss
};
public void Initialize(List<float> noteHitTimes)
{
SetAverageText(noteHitTimes);
SetPillarHeights(GetNoteCounts(noteHitTimes));
}
private void SetAverageText(List<float> noteHitTimes)
{
if (noteHitTimes.Count == 0)
{
averageText.text = "N/A";
return;
}
float average = noteHitTimes.Average();
float averageInMs = average * 1000f;
if (averageInMs > 0)
{
averageText.text = $"+{averageInMs:F2} ms";
}
else if (averageInMs < 0)
{
averageText.text = $"{averageInMs:F2} ms";
}
else
{
averageText.text = "0 ms";
}
}
private List<int> GetNoteCounts(List<float> noteHitTimes)
{
List<int> noteCounts = new List<int>(new int[ranges.Count]);
foreach (float hitTime in noteHitTimes)
{
for (int i = 0; i < ranges.Count; i++)
{
float hitTimeInMs = hitTime * 1000f;
if (hitTimeInMs >= ranges[i].Item1 && hitTimeInMs < ranges[i].Item2)
{
//Debug.Log($"Hit time in ms: {hitTimeInMs}, Range: {ranges[i].Item1} to {ranges[i].Item2}");
noteCounts[i]++;
break;
}
}
}
return noteCounts;
}
private void SetPillarHeights(List<int> noteCounts)
{
int maxCount = noteCounts.Max();
if(maxCount == 0) throw new System.Exception("There is no note count to display.");
for (int i = 0; i < noteCounts.Count; i++)
{
//Debug.Log($"Pillar {i} count: {noteCounts[i]}, Max count: {maxCount}");
float percentage = noteCounts[i] / (float)maxCount;
float height = Mathf.Sqrt(percentage) * maxPillarHeight;
pillars[i].rectTransform.sizeDelta = new Vector2(50, height);
}
}
}
}