Merge pull request #18626 from ggliv/decouple-mod-fails

Allow ModFailCondition to arbitrarily trigger failures
This commit is contained in:
Dean Herbert 2023-04-26 16:19:42 +09:00 committed by GitHub
commit 61b62b35f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 4 deletions

View File

@ -20,11 +20,31 @@ namespace osu.Game.Rulesets.Mods
public virtual bool RestartOnFail => Restart.Value;
private Action? triggerFailureDelegate;
public void ApplyToHealthProcessor(HealthProcessor healthProcessor)
{
triggerFailureDelegate = healthProcessor.TriggerFailure;
healthProcessor.FailConditions += FailCondition;
}
/// <summary>
/// Immediately triggers a failure on the loaded <see cref="HealthProcessor"/>.
/// </summary>
protected void TriggerFailure() => triggerFailureDelegate?.Invoke();
/// <summary>
/// Determines whether <paramref name="result"/> should trigger a failure. Called every time a
/// judgement is applied to <paramref name="healthProcessor"/>.
/// </summary>
/// <param name="healthProcessor">The loaded <see cref="HealthProcessor"/>.</param>
/// <param name="result">The latest <see cref="JudgementResult"/>.</param>
/// <returns>Whether the fail condition has been met.</returns>
/// <remarks>
/// This method should only be used to trigger failures based on <paramref name="result"/>.
/// Using outside values to evaluate failure may introduce event ordering discrepancies, use
/// an <see cref="IApplicableMod"/> with <see cref="TriggerFailure"/> instead.
/// </remarks>
protected abstract bool FailCondition(HealthProcessor healthProcessor, JudgementResult result);
}
}

View File

@ -31,6 +31,15 @@ namespace osu.Game.Rulesets.Scoring
/// </summary>
public bool HasFailed { get; private set; }
/// <summary>
/// Immediately triggers a failure for this HealthProcessor.
/// </summary>
public void TriggerFailure()
{
if (Failed?.Invoke() != false)
HasFailed = true;
}
protected override void ApplyResultInternal(JudgementResult result)
{
result.HealthAtJudgement = Health.Value;
@ -42,10 +51,7 @@ namespace osu.Game.Rulesets.Scoring
Health.Value += GetHealthIncreaseFor(result);
if (meetsAnyFailCondition(result))
{
if (Failed?.Invoke() != false)
HasFailed = true;
}
TriggerFailure();
}
protected override void RevertResultInternal(JudgementResult result)