Initial implementation of HP drain

This commit is contained in:
smoogipoo 2019-12-25 13:09:04 +09:00
parent 985277da79
commit a4b1c79e18
2 changed files with 55 additions and 1 deletions

View File

@ -2,9 +2,11 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Collections.Generic;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.MathUtils; using osu.Framework.MathUtils;
using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Objects;
namespace osu.Game.Rulesets.Scoring namespace osu.Game.Rulesets.Scoring
{ {
@ -31,15 +33,23 @@ namespace osu.Game.Rulesets.Scoring
/// </summary> /// </summary>
public bool HasFailed { get; private set; } public bool HasFailed { get; private set; }
private readonly List<(double time, double health)> healthIncreases = new List<(double time, double health)>();
private double drainRate = 1;
public override void ApplyElapsedTime(double elapsedTime) => Health.Value -= drainRate * elapsedTime;
protected override void ApplyResultInternal(JudgementResult result) protected override void ApplyResultInternal(JudgementResult result)
{ {
result.HealthAtJudgement = Health.Value; result.HealthAtJudgement = Health.Value;
result.FailedAtJudgement = HasFailed; result.FailedAtJudgement = HasFailed;
double healthIncrease = HealthAdjustmentFactorFor(result) * result.Judgement.HealthIncreaseFor(result);
healthIncreases.Add((result.HitObject.GetEndTime() + result.TimeOffset, healthIncrease));
if (HasFailed) if (HasFailed)
return; return;
Health.Value += HealthAdjustmentFactorFor(result) * result.Judgement.HealthIncreaseFor(result); Health.Value += healthIncrease;
if (!DefaultFailCondition && FailConditions?.Invoke(this, result) != true) if (!DefaultFailCondition && FailConditions?.Invoke(this, result) != true)
return; return;
@ -71,6 +81,42 @@ namespace osu.Game.Rulesets.Scoring
{ {
base.Reset(storeResults); base.Reset(storeResults);
drainRate = 1;
if (storeResults)
{
const double percentage_target = 0.5;
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;
}
if (Math.Abs(lowestHealth - percentage_target) <= 0.01)
break;
count *= 2;
drainRate += 1.0 / count * Math.Sign(lowestHealth - percentage_target);
}
}
healthIncreases.Clear();
Health.Value = 1; Health.Value = 1;
HasFailed = false; HasFailed = false;
} }

View File

@ -342,6 +342,14 @@ namespace osu.Game.Screens.Play
this.Exit(); this.Exit();
} }
protected override void Update()
{
base.Update();
if (!GameplayClockContainer.IsPaused.Value)
HealthProcessor.ApplyElapsedTime(GameplayClockContainer.GameplayClock.ElapsedFrameTime);
}
/// <summary> /// <summary>
/// Restart gameplay via a parent <see cref="PlayerLoader"/>. /// Restart gameplay via a parent <see cref="PlayerLoader"/>.
/// <remarks>This can be called from a child screen in order to trigger the restart process.</remarks> /// <remarks>This can be called from a child screen in order to trigger the restart process.</remarks>