mirror of
https://github.com/osukey/osukey.git
synced 2025-08-05 23:53:51 +09:00
Simplify calculation method
This commit is contained in:
@ -2,7 +2,6 @@
|
|||||||
// See the LICENCE file in the repository root for full licence text.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Game.Rulesets.UI;
|
using osu.Game.Rulesets.UI;
|
||||||
@ -11,56 +10,48 @@ namespace osu.Game.Screens.Play.HUD.ClicksPerSecond
|
|||||||
{
|
{
|
||||||
public class ClicksPerSecondCalculator : Component
|
public class ClicksPerSecondCalculator : Component
|
||||||
{
|
{
|
||||||
private readonly List<double> timestamps;
|
private readonly List<double> timestamps = new List<double>();
|
||||||
|
|
||||||
[Resolved]
|
[Resolved]
|
||||||
private IGameplayClock gameplayClock { get; set; } = null!;
|
private IGameplayClock gameplayClock { get; set; } = null!;
|
||||||
|
|
||||||
[Resolved]
|
[Resolved(canBeNull: true)]
|
||||||
private DrawableRuleset drawableRuleset { get; set; } = null!;
|
private DrawableRuleset? drawableRuleset { get; set; }
|
||||||
|
|
||||||
private double rate;
|
|
||||||
|
|
||||||
// The latest timestamp GC seeked. Does not affect normal gameplay
|
|
||||||
// but prevents duplicate inputs on replays.
|
|
||||||
private double latestTime = double.NegativeInfinity;
|
|
||||||
|
|
||||||
public int Value { get; private set; }
|
public int Value { get; private set; }
|
||||||
|
|
||||||
|
private IGameplayClock clock => drawableRuleset?.FrameStableClock ?? gameplayClock;
|
||||||
|
|
||||||
public ClicksPerSecondCalculator()
|
public ClicksPerSecondCalculator()
|
||||||
{
|
{
|
||||||
RelativeSizeAxes = Axes.Both;
|
RelativeSizeAxes = Axes.Both;
|
||||||
timestamps = new List<double>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void AddInputTimestamp() => timestamps.Add(clock.CurrentTime);
|
||||||
|
|
||||||
protected override void Update()
|
protected override void Update()
|
||||||
{
|
{
|
||||||
base.Update();
|
base.Update();
|
||||||
|
|
||||||
// When pausing in replays (using the space bar) GC.TrueGameplayRate returns 0
|
double latestValidTime = clock.CurrentTime;
|
||||||
// To prevent CPS value being 0, we store and use the last non-zero TrueGameplayRate
|
double earliestTimeValid = latestValidTime - 1000 * gameplayClock.TrueGameplayRate;
|
||||||
if (gameplayClock.TrueGameplayRate > 0)
|
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
for (int i = timestamps.Count - 1; i >= 0; i--)
|
||||||
{
|
{
|
||||||
rate = gameplayClock.TrueGameplayRate;
|
// handle rewinding by removing future timestamps as we go
|
||||||
|
if (timestamps[i] > latestValidTime)
|
||||||
|
{
|
||||||
|
timestamps.RemoveAt(i);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timestamps[i] >= earliestTimeValid)
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value = timestamps.Count(timestamp =>
|
Value = count;
|
||||||
{
|
|
||||||
double window = 1000 * rate;
|
|
||||||
double relativeTime = drawableRuleset.FrameStableClock.CurrentTime - timestamp;
|
|
||||||
return relativeTime > 0 && relativeTime <= window;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddTimestamp()
|
|
||||||
{
|
|
||||||
// Discard inputs if current gameplay time is not the latest
|
|
||||||
// to prevent duplicate inputs
|
|
||||||
if (drawableRuleset.FrameStableClock.CurrentTime >= latestTime)
|
|
||||||
{
|
|
||||||
timestamps.Add(drawableRuleset.FrameStableClock.CurrentTime);
|
|
||||||
latestTime = drawableRuleset.FrameStableClock.CurrentTime;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user