1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 09:22:58 +08:00
osu-lazer/osu.Game/Modes/ScoreProcessor.cs

164 lines
5.1 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-11-29 19:30:16 +08:00
using osu.Framework.Configuration;
using System;
using System.Collections.Generic;
using osu.Game.Modes.Judgements;
using osu.Game.Modes.UI;
using osu.Game.Modes.Objects;
using osu.Game.Beatmaps;
2016-11-29 19:30:16 +08:00
namespace osu.Game.Modes
{
public abstract class ScoreProcessor
2016-11-29 19:30:16 +08:00
{
/// <summary>
2017-03-17 01:00:58 +08:00
/// Invoked when the ScoreProcessor is in a failed state.
/// </summary>
public event Action Failed;
/// <summary>
/// The current total score.
/// </summary>
public readonly BindableDouble TotalScore = new BindableDouble { MinValue = 0 };
/// <summary>
/// The current accuracy.
/// </summary>
public readonly BindableDouble Accuracy = new BindableDouble { MinValue = 0, MaxValue = 1 };
2017-01-10 16:01:53 +08:00
/// <summary>
/// The current health.
/// </summary>
2017-01-10 16:01:53 +08:00
public readonly BindableDouble Health = new BindableDouble { MinValue = 0, MaxValue = 1 };
/// <summary>
/// The current combo.
/// </summary>
2017-03-10 15:05:05 +08:00
public readonly BindableInt Combo = new BindableInt();
2017-01-20 15:51:43 +08:00
/// <summary>
/// THe highest combo achieved by this score.
2017-01-20 15:51:43 +08:00
/// </summary>
public readonly BindableInt HighestCombo = new BindableInt();
2017-01-20 15:51:43 +08:00
/// <summary>
/// Whether the score is in a failed state.
/// </summary>
2017-03-17 01:00:06 +08:00
public virtual bool HasFailed => false;
/// <summary>
/// Whether this ScoreProcessor has already triggered the failed state.
/// </summary>
private bool alreadyFailed;
protected ScoreProcessor()
{
Combo.ValueChanged += delegate { HighestCombo.Value = Math.Max(HighestCombo.Value, Combo.Value); };
Reset();
}
2017-03-17 01:03:12 +08:00
/// <summary>
/// Creates a Score applicable to the game mode in which this ScoreProcessor resides.
/// </summary>
/// <returns>The Score.</returns>
public virtual Score CreateScore() => new Score
{
TotalScore = TotalScore,
Combo = Combo,
MaxCombo = HighestCombo,
Accuracy = Accuracy,
Health = Health,
};
2017-01-20 15:51:43 +08:00
/// <summary>
2017-03-17 00:21:59 +08:00
/// Resets this ScoreProcessor to a default state.
2017-01-20 15:51:43 +08:00
/// </summary>
protected virtual void Reset()
{
TotalScore.Value = 0;
Accuracy.Value = 0;
Health.Value = 0;
Combo.Value = 0;
HighestCombo.Value = 0;
2017-03-17 01:00:06 +08:00
alreadyFailed = false;
}
2017-01-20 15:51:43 +08:00
2017-03-16 11:57:50 +08:00
/// <summary>
2017-03-17 01:00:06 +08:00
/// Checks if the score is in a failed state and notifies subscribers.
/// <para>
/// This can only ever notify subscribers once.
/// </para>
2017-03-16 11:57:50 +08:00
/// </summary>
2017-03-17 01:00:06 +08:00
protected void UpdateFailed()
{
2017-03-17 01:00:06 +08:00
if (alreadyFailed || !HasFailed)
return;
alreadyFailed = true;
Failed?.Invoke();
}
}
public abstract class ScoreProcessor<TObject, TJudgement> : ScoreProcessor
where TObject : HitObject
where TJudgement : JudgementInfo
{
2017-01-20 15:51:43 +08:00
/// <summary>
/// All judgements held by this ScoreProcessor.
2017-01-20 15:51:43 +08:00
/// </summary>
2017-03-16 12:49:16 +08:00
protected readonly List<TJudgement> Judgements = new List<TJudgement>();
2016-11-29 20:57:53 +08:00
public override bool HasFailed => Health.Value == Health.MinValue;
protected ScoreProcessor()
{
}
protected ScoreProcessor(HitRenderer<TObject, TJudgement> hitRenderer)
{
2017-03-16 12:49:16 +08:00
Judgements.Capacity = hitRenderer.Beatmap.HitObjects.Count;
2017-03-17 01:00:06 +08:00
hitRenderer.OnJudgement += addJudgement;
ComputeTargets(hitRenderer.Beatmap);
Reset();
}
/// <summary>
/// Computes target scoring values for this ScoreProcessor. This is equivalent to performing an auto-play of the score to find the values.
/// </summary>
/// <param name="beatmap">The Beatmap containing the objects that will be judged by this ScoreProcessor.</param>
protected virtual void ComputeTargets(Beatmap<TObject> beatmap) { }
/// <summary>
/// Adds a judgement to this ScoreProcessor.
/// </summary>
/// <param name="judgement">The judgement to add.</param>
private void addJudgement(TJudgement judgement)
{
Judgements.Add(judgement);
UpdateCalculations(judgement);
judgement.ComboAtHit = (ulong)Combo.Value;
2017-03-17 01:00:06 +08:00
UpdateFailed();
}
protected override void Reset()
{
Judgements.Clear();
}
/// <summary>
/// Update any values that potentially need post-processing on a judgement change.
/// </summary>
/// <param name="newJudgement">A new JudgementInfo that triggered this calculation. May be null.</param>
protected abstract void UpdateCalculations(TJudgement newJudgement);
2016-11-29 19:30:16 +08:00
}
}