Simplify TotalScoreCounter tick playback logic

This commit is contained in:
Dean Herbert 2022-07-23 02:25:37 +09:00
parent 9f045209b9
commit bb46ba66e0

View File

@ -26,32 +26,35 @@ namespace osu.Game.Screens.Ranking.Expanded
protected override Easing RollingEasing => AccuracyCircle.ACCURACY_TRANSFORM_EASING;
private readonly bool playSound;
private bool isTicking;
private readonly bool playSamples;
private readonly Bindable<double> tickPlaybackRate = new Bindable<double>();
private double lastSampleTime;
private DrawableSample sampleTick;
public TotalScoreCounter(bool playSound = false)
public TotalScoreCounter(bool playSamples = false)
{
// Todo: AutoSize X removed here due to https://github.com/ppy/osu-framework/issues/3369
AutoSizeAxes = Axes.Y;
RelativeSizeAxes = Axes.X;
this.playSound = playSound;
this.playSamples = playSamples;
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
AddInternal(sampleTick = new DrawableSample(audio.Samples.Get(@"Results/score-tick-lesser")));
lastSampleTime = Time.Current;
}
protected override void LoadComplete()
{
base.LoadComplete();
Current.BindValueChanged(startTickingPlayback);
if (playSamples)
Current.BindValueChanged(_ => startTicking());
}
protected override LocalisableString FormatCount(long count) => count.ToString("N0");
@ -65,39 +68,36 @@ namespace osu.Game.Screens.Ranking.Expanded
s.Spacing = new Vector2(-5, 0);
});
protected override void Update()
public override long DisplayedCount
{
base.Update();
get => base.DisplayedCount;
set
{
if (base.DisplayedCount == value)
return;
if (playSound && isTicking) playTickSample();
base.DisplayedCount = value;
if (playSamples && Time.Current > lastSampleTime + tickPlaybackRate.Value)
{
sampleTick?.Play();
lastSampleTime = Time.Current;
}
}
}
private void startTickingPlayback(ValueChangedEvent<long> _)
private void startTicking()
{
const double tick_debounce_rate_start = 10f;
const double tick_debounce_rate_end = 100f;
const double tick_volume_start = 0.5f;
const double tick_volume_end = 1.0f;
double tickDuration = RollingDuration - AccuracyCircle.ACCURACY_TRANSFORM_DELAY - AccuracyCircle.RANK_CIRCLE_TRANSFORM_DELAY;
double tickDuration = RollingDuration;
this.TransformBindableTo(tickPlaybackRate, tick_debounce_rate_start);
this.TransformBindableTo(tickPlaybackRate, tick_debounce_rate_end, tickDuration, Easing.OutSine);
sampleTick.VolumeTo(tick_volume_start).Then().VolumeTo(tick_volume_end, tickDuration, Easing.OutSine);
Scheduler.AddDelayed(stopTickingPlayback, tickDuration);
isTicking = true;
}
private void stopTickingPlayback() => isTicking = false;
private void playTickSample()
{
if (Time.Current > lastSampleTime + tickPlaybackRate.Value)
{
sampleTick?.Play();
lastSampleTime = Time.Current;
}
}
}
}