1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 03:47:26 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/PerformancePointsCounter.cs

237 lines
8.0 KiB
C#
Raw Normal View History

2021-09-30 16:00:15 +08:00
// 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.IO;
using System.Linq;
2021-10-05 10:26:13 +08:00
using System.Threading;
using System.Threading.Tasks;
2021-09-30 16:54:56 +08:00
using JetBrains.Annotations;
2021-09-30 16:00:15 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio.Track;
using osu.Framework.Extensions;
2021-09-30 16:00:15 +08:00
using osu.Framework.Graphics;
2021-09-30 16:54:56 +08:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
2021-09-30 16:00:15 +08:00
using osu.Framework.Graphics.Textures;
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;
using osu.Game.Rulesets.Difficulty;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Scoring;
using osu.Game.Skinning;
2021-09-30 16:54:56 +08:00
using osuTK;
2021-09-30 16:00:15 +08:00
namespace osu.Game.Screens.Play.HUD
{
2021-10-04 19:34:08 +08:00
public class PerformancePointsCounter : RollingCounter<int>, ISkinnableDrawable
2021-09-30 16:00:15 +08:00
{
public bool UsesFixedAnchor { get; set; }
2021-10-05 15:51:49 +08:00
protected override bool IsRollingProportional => true;
protected override double RollingDuration => 1000;
private const float alpha_when_invalid = 0.3f;
2021-09-30 16:54:56 +08:00
[CanBeNull]
[Resolved(CanBeNull = true)]
2021-09-30 16:00:15 +08:00
private ScoreProcessor scoreProcessor { get; set; }
2021-10-04 19:59:31 +08:00
[Resolved(CanBeNull = true)]
2021-09-30 16:54:56 +08:00
[CanBeNull]
2021-10-04 19:33:54 +08:00
private GameplayState gameplayState { get; set; }
2021-09-30 16:00:15 +08:00
2021-10-05 10:26:13 +08:00
[CanBeNull]
2021-10-05 15:59:54 +08:00
private List<TimedDifficultyAttributes> timedAttributes;
2021-10-05 10:26:13 +08:00
private readonly CancellationTokenSource loadCancellationSource = new CancellationTokenSource();
private JudgementResult lastJudgement;
2021-10-04 19:34:08 +08:00
public PerformancePointsCounter()
2021-09-30 16:00:15 +08:00
{
Current.Value = DisplayedCount = 0;
}
private Mod[] clonedMods;
2021-09-30 16:00:15 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours, BeatmapDifficultyCache difficultyCache)
2021-09-30 16:00:15 +08:00
{
Colour = colours.BlueLighter;
2021-10-04 19:33:54 +08:00
if (gameplayState != null)
2021-09-30 16:54:56 +08:00
{
clonedMods = gameplayState.Mods.Select(m => m.DeepClone()).ToArray();
2021-10-05 14:10:56 +08:00
var gameplayWorkingBeatmap = new GameplayWorkingBeatmap(gameplayState.Beatmap);
2021-10-06 20:30:30 +08:00
difficultyCache.GetTimedDifficultyAttributesAsync(gameplayWorkingBeatmap, gameplayState.Ruleset, clonedMods, loadCancellationSource.Token)
.ContinueWith(task => Schedule(() =>
{
2022-01-06 21:54:43 +08:00
timedAttributes = task.GetResultSafely();
IsValid = true;
if (lastJudgement != null)
onJudgementChanged(lastJudgement);
}), TaskContinuationOptions.OnlyOnRanToCompletion);
2021-09-30 16:54:56 +08:00
}
2021-09-30 16:00:15 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2021-09-30 16:54:56 +08:00
if (scoreProcessor != null)
{
scoreProcessor.NewJudgement += onJudgementChanged;
scoreProcessor.JudgementReverted += onJudgementChanged;
}
if (gameplayState?.LastJudgementResult.Value != null)
onJudgementChanged(gameplayState.LastJudgementResult.Value);
}
private bool isValid;
protected bool IsValid
{
set
{
if (value == isValid)
return;
isValid = value;
DrawableCount.FadeTo(isValid ? 1 : alpha_when_invalid, 1000, Easing.OutQuint);
}
2021-09-30 16:00:15 +08:00
}
private void onJudgementChanged(JudgementResult judgement)
2021-09-30 16:00:15 +08:00
{
lastJudgement = judgement;
2021-10-05 15:40:07 +08:00
var attrib = getAttributeAtTime(judgement);
2021-09-30 16:54:56 +08:00
2021-10-05 15:40:07 +08:00
if (gameplayState == null || attrib == null)
{
IsValid = false;
2021-10-05 15:40:07 +08:00
return;
}
2021-09-30 16:00:15 +08:00
// awkward but we need to make sure the true mods are not passed to PerformanceCalculator as it makes a mess of track applications.
var scoreInfo = gameplayState.Score.ScoreInfo.DeepClone();
scoreInfo.Mods = clonedMods;
var calculator = gameplayState.Ruleset.CreatePerformanceCalculator(attrib, scoreInfo);
2021-10-05 14:10:56 +08:00
2021-12-21 18:08:31 +08:00
Current.Value = (int)Math.Round(calculator?.Calculate().Total ?? 0, MidpointRounding.AwayFromZero);
IsValid = true;
}
2021-10-05 15:40:07 +08:00
[CanBeNull]
private DifficultyAttributes getAttributeAtTime(JudgementResult judgement)
{
2021-10-05 15:59:54 +08:00
if (timedAttributes == null || timedAttributes.Count == 0)
2021-10-05 15:40:07 +08:00
return null;
2021-10-05 15:59:54 +08:00
int attribIndex = timedAttributes.BinarySearch(new TimedDifficultyAttributes(judgement.HitObject.GetEndTime(), null));
2021-10-05 15:40:07 +08:00
if (attribIndex < 0)
attribIndex = ~attribIndex - 1;
2021-10-05 15:59:54 +08:00
return timedAttributes[Math.Clamp(attribIndex, 0, timedAttributes.Count - 1)].Attributes;
2021-09-30 16:00:15 +08:00
}
2021-09-30 16:54:52 +08:00
protected override LocalisableString FormatCount(int count) => count.ToString(@"D");
2021-09-30 16:00:15 +08:00
protected override IHasText CreateText() => new TextComponent
{
Alpha = alpha_when_invalid
};
2021-09-30 16:00:15 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (scoreProcessor != null)
{
scoreProcessor.NewJudgement -= onJudgementChanged;
scoreProcessor.JudgementReverted -= onJudgementChanged;
}
loadCancellationSource?.Cancel();
2021-09-30 16:00:15 +08:00
}
2021-09-30 16:54:52 +08:00
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,
Spacing = new Vector2(2),
Children = new Drawable[]
{
text = new OsuSpriteText
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Font = OsuFont.Numeric.With(size: 16, fixedWidth: true)
2021-09-30 16:54:52 +08:00
},
new OsuSpriteText
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
Text = @"pp",
Font = OsuFont.Numeric.With(size: 8),
Padding = new MarginPadding { Bottom = 1.5f }, // align baseline better
2021-09-30 16:54:52 +08:00
}
}
};
}
}
2021-10-05 14:10:56 +08:00
// TODO: This class shouldn't exist, but requires breaking changes to allow DifficultyCalculator to receive an IBeatmap.
2021-09-30 16:00:15 +08:00
private class GameplayWorkingBeatmap : WorkingBeatmap
{
2021-10-04 19:33:54 +08:00
private readonly IBeatmap gameplayBeatmap;
2021-09-30 16:00:15 +08:00
2021-10-04 19:33:54 +08:00
public GameplayWorkingBeatmap(IBeatmap gameplayBeatmap)
2021-09-30 16:00:15 +08:00
: base(gameplayBeatmap.BeatmapInfo, null)
{
this.gameplayBeatmap = gameplayBeatmap;
}
public override IBeatmap GetPlayableBeatmap(IRulesetInfo ruleset, IReadOnlyList<Mod> mods, CancellationToken cancellationToken)
2021-10-04 19:33:54 +08:00
=> gameplayBeatmap;
2021-09-30 16:00:15 +08:00
2021-10-04 19:33:54 +08:00
protected override IBeatmap GetBeatmap() => gameplayBeatmap;
2021-09-30 16:00:15 +08:00
protected override Texture GetBackground() => throw new NotImplementedException();
protected override Track GetBeatmapTrack() => throw new NotImplementedException();
protected internal override ISkin GetSkin() => throw new NotImplementedException();
public override Stream GetStream(string storagePath) => throw new NotImplementedException();
}
}
}