1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-13 08:32:57 +08:00

Merge pull request #15470 from M0RGaming/master

Add unstable rate counter to HUD
This commit is contained in:
Bartłomiej Dach 2021-11-09 09:07:20 +01:00 committed by GitHub
commit 0cb5113601
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 245 additions and 0 deletions

View File

@ -0,0 +1,98 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Testing;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.Scoring;
using osu.Game.Rulesets.Scoring;
using osu.Game.Screens.Play.HUD;
using osuTK;
namespace osu.Game.Tests.Visual.Gameplay
{
public class TestSceneUnstableRateCounter : OsuTestScene
{
[Cached(typeof(ScoreProcessor))]
private TestScoreProcessor scoreProcessor = new TestScoreProcessor();
private readonly OsuHitWindows hitWindows = new OsuHitWindows();
private UnstableRateCounter counter;
private double prev;
[SetUpSteps]
public void SetUp()
{
AddStep("Reset Score Processor", () => scoreProcessor.Reset());
}
[Test]
public void TestBasic()
{
AddStep("Create Display", recreateDisplay);
// Needs multiples 2 by the nature of UR, and went for 4 to be safe.
// Creates a 250 UR by placing a +25ms then a -25ms judgement, which then results in a 250 UR
AddRepeatStep("Set UR to 250", () => applyJudgement(25, true), 4);
AddUntilStep("UR = 250", () => counter.Current.Value == 250.0);
AddRepeatStep("Revert UR", () =>
{
scoreProcessor.RevertResult(
new JudgementResult(new HitCircle { HitWindows = hitWindows }, new Judgement())
{
TimeOffset = 25,
Type = HitResult.Perfect,
});
}, 4);
AddUntilStep("UR is 0", () => counter.Current.Value == 0.0);
AddUntilStep("Counter is invalid", () => counter.Child.Alpha == 0.3f);
//Sets a UR of 0 by creating 10 10ms offset judgements. Since average = offset, UR = 0
AddRepeatStep("Set UR to 0", () => applyJudgement(10, false), 10);
//Applies a UR of 100 by creating 10 -10ms offset judgements. At the 10th judgement, offset should be 100.
AddRepeatStep("Bring UR to 100", () => applyJudgement(-10, false), 10);
}
private void recreateDisplay()
{
Clear();
Add(counter = new UnstableRateCounter
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Scale = new Vector2(5),
});
}
private void applyJudgement(double offsetMs, bool alt)
{
double placement = offsetMs;
if (alt)
{
placement = prev > 0 ? -offsetMs : offsetMs;
prev = placement;
}
scoreProcessor.ApplyResult(new JudgementResult(new HitCircle { HitWindows = hitWindows }, new Judgement())
{
TimeOffset = placement,
Type = HitResult.Perfect,
});
}
private class TestScoreProcessor : ScoreProcessor
{
public void Reset() => base.Reset(false);
}
}
}

View File

@ -0,0 +1,147 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Scoring;
using osu.Game.Skinning;
namespace osu.Game.Screens.Play.HUD
{
public class UnstableRateCounter : RollingCounter<int>, ISkinnableDrawable
{
public bool UsesFixedAnchor { get; set; }
protected override double RollingDuration => 750;
private const float alpha_when_invalid = 0.3f;
private readonly Bindable<bool> valid = new Bindable<bool>();
private readonly List<double> hitOffsets = new List<double>();
[Resolved]
private ScoreProcessor scoreProcessor { get; set; }
public UnstableRateCounter()
{
Current.Value = 0;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours, BeatmapDifficultyCache difficultyCache)
{
Colour = colours.BlueLighter;
valid.BindValueChanged(e =>
DrawableCount.FadeTo(e.NewValue ? 1 : alpha_when_invalid, 1000, Easing.OutQuint));
}
private bool changesUnstableRate(JudgementResult judgement)
=> !(judgement.HitObject.HitWindows is HitWindows.EmptyHitWindows) && judgement.IsHit;
protected override void LoadComplete()
{
base.LoadComplete();
scoreProcessor.NewJudgement += onJudgementAdded;
scoreProcessor.JudgementReverted += onJudgementReverted;
}
private void onJudgementAdded(JudgementResult judgement)
{
if (!changesUnstableRate(judgement)) return;
hitOffsets.Add(judgement.TimeOffset);
updateDisplay();
}
private void onJudgementReverted(JudgementResult judgement)
{
if (judgement.FailedAtJudgement || !changesUnstableRate(judgement)) return;
hitOffsets.RemoveAt(hitOffsets.Count - 1);
updateDisplay();
}
private void updateDisplay()
{
// At Count = 0, we get NaN, While we are allowing count = 1, it will be 0 since average = offset.
if (hitOffsets.Count > 0)
{
double mean = hitOffsets.Average();
double squares = hitOffsets.Select(offset => Math.Pow(offset - mean, 2)).Sum();
Current.Value = (int)(Math.Sqrt(squares / hitOffsets.Count) * 10);
valid.Value = true;
}
else
{
Current.Value = 0;
valid.Value = false;
}
}
protected override IHasText CreateText() => new TextComponent
{
Alpha = alpha_when_invalid,
};
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (scoreProcessor == null) return;
scoreProcessor.NewJudgement -= onJudgementAdded;
scoreProcessor.JudgementReverted -= onJudgementReverted;
}
private class TextComponent : CompositeDrawable, IHasText
{
public LocalisableString Text
{
get => text.Text;
set => text.Text = value;
}
private readonly OsuSpriteText text;
public TextComponent()
{
AutoSizeAxes = Axes.Both;
InternalChild = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
text = new OsuSpriteText
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Font = OsuFont.Numeric.With(size: 16, fixedWidth: true)
},
new OsuSpriteText
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Font = OsuFont.Numeric.With(size: 8, fixedWidth: true),
Text = "UR",
Padding = new MarginPadding { Bottom = 1.5f },
}
}
};
}
}
}
}