HeavenStudio/Assets/Scripts/LevelEditor/BPMText.cs

43 lines
1.1 KiB
C#
Raw Normal View History

2022-02-25 01:02:05 +00:00
using System.Collections.Generic;
using UnityEngine;
using TMPro;
2022-02-25 04:30:02 +00:00
using System.Linq;
2022-02-25 01:02:05 +00:00
namespace RhythmHeavenMania.Editor
{
public class BPMText : MonoBehaviour
{
2022-02-25 04:30:02 +00:00
public const int maxPressTimes = 50;
2022-02-25 01:02:05 +00:00
[SerializeField] private TMP_Text BPM;
[SerializeField] private TMP_Text BPMRounded;
2022-02-25 04:30:02 +00:00
private List<float> pressTimes = new List<float>();
2022-02-25 01:02:05 +00:00
public void ChangeText(float timePressed)
{
2022-02-25 04:30:02 +00:00
pressTimes.Add(timePressed);
// First press isn't good to work with.
if (pressTimes.Count < 2) return;
// Limit the number of press times stored.
if (pressTimes.Count > maxPressTimes)
pressTimes.RemoveAt(0);
var averageTime = pressTimes.GetRange(1, pressTimes.Count - 1).Average();
float thisBPM = 60 / averageTime; // BPM = 60/t
2022-02-25 01:02:05 +00:00
BPM.text = $"{thisBPM}";
BPMRounded.text = $"{Mathf.RoundToInt(thisBPM)}";
}
public void ResetText()
{
2022-02-25 04:30:02 +00:00
pressTimes.Clear();
2022-02-25 01:02:05 +00:00
BPM.text = "---";
BPMRounded.text = "---";
}
}
}