1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 05:27:26 +08:00
osu-lazer/osu.Game/Rulesets/Scoring/HealthProcessor.cs

140 lines
4.6 KiB
C#
Raw Normal View History

// 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;
2019-12-25 12:09:04 +08:00
using System.Collections.Generic;
using osu.Framework.Bindables;
using osu.Framework.MathUtils;
2019-12-25 12:32:58 +08:00
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Judgements;
2019-12-25 12:09:04 +08:00
using osu.Game.Rulesets.Objects;
namespace osu.Game.Rulesets.Scoring
{
public class HealthProcessor : JudgementProcessor
{
/// <summary>
/// Invoked when the <see cref="ScoreProcessor"/> is in a failed state.
/// Return true if the fail was permitted.
/// </summary>
public event Func<bool> Failed;
/// <summary>
/// Additional conditions on top of <see cref="DefaultFailCondition"/> that cause a failing state.
/// </summary>
public event Func<HealthProcessor, JudgementResult, bool> FailConditions;
/// <summary>
/// The current health.
/// </summary>
public readonly BindableDouble Health = new BindableDouble(1) { MinValue = 0, MaxValue = 1 };
2019-12-25 13:49:17 +08:00
/// <summary>
/// Whether gameplay is currently in a break.
/// </summary>
public readonly IBindable<bool> IsBreakTime = new Bindable<bool>();
/// <summary>
/// Whether this ScoreProcessor has already triggered the failed state.
/// </summary>
public bool HasFailed { get; private set; }
2019-12-25 12:34:13 +08:00
private List<(double time, double health)> healthIncreases;
2019-12-25 12:32:58 +08:00
private double targetMinimumHealth;
2019-12-25 12:09:04 +08:00
private double drainRate = 1;
2019-12-25 12:32:58 +08:00
public override void ApplyBeatmap(IBeatmap beatmap)
{
2019-12-25 12:34:13 +08:00
healthIncreases = new List<(double time, double health)>();
2019-12-25 12:32:58 +08:00
targetMinimumHealth = BeatmapDifficulty.DifficultyRange(beatmap.BeatmapInfo.BaseDifficulty.DrainRate, 0.95, 0.85, 0.65);
2019-12-25 12:34:13 +08:00
2019-12-25 12:32:58 +08:00
base.ApplyBeatmap(beatmap);
2019-12-25 12:34:13 +08:00
// Only required during the simulation stage
healthIncreases = null;
2019-12-25 12:32:58 +08:00
}
protected override void Update()
{
base.Update();
2019-12-25 13:49:17 +08:00
if (!IsBreakTime.Value)
Health.Value -= drainRate * Time.Elapsed;
}
2019-12-25 12:09:04 +08:00
protected override void ApplyResultInternal(JudgementResult result)
{
result.HealthAtJudgement = Health.Value;
result.FailedAtJudgement = HasFailed;
2019-12-25 12:33:50 +08:00
double healthIncrease = result.Judgement.HealthIncreaseFor(result);
2019-12-25 12:34:13 +08:00
healthIncreases?.Add((result.HitObject.GetEndTime() + result.TimeOffset, healthIncrease));
2019-12-25 12:09:04 +08:00
if (HasFailed)
return;
2019-12-25 12:09:04 +08:00
Health.Value += healthIncrease;
if (!DefaultFailCondition && FailConditions?.Invoke(this, result) != true)
return;
if (Failed?.Invoke() != false)
HasFailed = true;
}
protected override void RevertResultInternal(JudgementResult result)
{
Health.Value = result.HealthAtJudgement;
// Todo: Revert HasFailed state with proper player support
}
/// <summary>
/// The default conditions for failing.
/// </summary>
protected virtual bool DefaultFailCondition => Precision.AlmostBigger(Health.MinValue, Health.Value);
protected override void Reset(bool storeResults)
{
base.Reset(storeResults);
2019-12-25 12:09:04 +08:00
drainRate = 1;
if (storeResults)
{
int count = 1;
while (true)
{
double currentHealth = 1;
double lowestHealth = 1;
for (int i = 0; i < healthIncreases.Count; i++)
{
var lastTime = i > 0 ? healthIncreases[i - 1].time : 0;
currentHealth -= (healthIncreases[i].time - lastTime) * drainRate;
lowestHealth = Math.Min(lowestHealth, currentHealth);
currentHealth = Math.Min(1, currentHealth + healthIncreases[i].health);
// Common scenario for when the drain rate is definitely too harsh
if (lowestHealth < 0)
break;
}
2019-12-25 12:32:58 +08:00
if (Math.Abs(lowestHealth - targetMinimumHealth) <= 0.01)
2019-12-25 12:09:04 +08:00
break;
count *= 2;
2019-12-25 12:32:58 +08:00
drainRate += 1.0 / count * Math.Sign(lowestHealth - targetMinimumHealth);
2019-12-25 12:09:04 +08:00
}
}
healthIncreases.Clear();
Health.Value = 1;
HasFailed = false;
}
}
}