1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 14:25:05 +08:00

Merge pull request #8312 from Game4all/low-hp-red-layer

Fade playfield to red when player health is low
This commit is contained in:
Dean Herbert 2020-04-14 18:33:51 +09:00 committed by GitHub
commit ecd40072c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 218 additions and 2 deletions

View File

@ -0,0 +1,73 @@
// 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 NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Testing;
using osu.Game.Configuration;
using osu.Game.Rulesets.Scoring;
using osu.Game.Screens.Play.HUD;
namespace osu.Game.Tests.Visual.Gameplay
{
public class TestSceneFailingLayer : OsuTestScene
{
private FailingLayer layer;
[Resolved]
private OsuConfigManager config { get; set; }
[SetUpSteps]
public void SetUpSteps()
{
AddStep("create layer", () =>
{
Child = layer = new FailingLayer();
layer.BindHealthProcessor(new DrainingHealthProcessor(1));
});
AddStep("enable layer", () => config.Set(OsuSetting.FadePlayfieldWhenHealthLow, true));
AddUntilStep("layer is visible", () => layer.IsPresent);
}
[Test]
public void TestLayerFading()
{
AddSliderStep("current health", 0.0, 1.0, 1.0, val =>
{
if (layer != null)
layer.Current.Value = val;
});
AddStep("set health to 0.10", () => layer.Current.Value = 0.1);
AddUntilStep("layer fade is visible", () => layer.Child.Alpha > 0.1f);
AddStep("set health to 1", () => layer.Current.Value = 1f);
AddUntilStep("layer fade is invisible", () => !layer.Child.IsPresent);
}
[Test]
public void TestLayerDisabledViaConfig()
{
AddStep("disable layer", () => config.Set(OsuSetting.FadePlayfieldWhenHealthLow, false));
AddStep("set health to 0.10", () => layer.Current.Value = 0.1);
AddUntilStep("layer is not visible", () => !layer.IsPresent);
}
[Test]
public void TestLayerVisibilityWithAccumulatingProcessor()
{
AddStep("bind accumulating processor", () => layer.BindHealthProcessor(new AccumulatingHealthProcessor(1)));
AddStep("set health to 0.10", () => layer.Current.Value = 0.1);
AddUntilStep("layer is not visible", () => !layer.IsPresent);
}
[Test]
public void TestLayerVisibilityWithDrainingProcessor()
{
AddStep("bind accumulating processor", () => layer.BindHealthProcessor(new DrainingHealthProcessor(1)));
AddStep("set health to 0.10", () => layer.Current.Value = 0.1);
AddWaitStep("wait for potential fade", 10);
AddAssert("layer is still visible", () => layer.IsPresent);
}
}
}

View File

@ -88,6 +88,7 @@ namespace osu.Game.Configuration
Set(OsuSetting.ShowInterface, true);
Set(OsuSetting.ShowProgressGraph, true);
Set(OsuSetting.ShowHealthDisplayWhenCantFail, true);
Set(OsuSetting.FadePlayfieldWhenHealthLow, true);
Set(OsuSetting.KeyOverlay, false);
Set(OsuSetting.PositionalHitSounds, true);
Set(OsuSetting.ScoreMeter, ScoreMeterType.HitErrorBoth);
@ -184,6 +185,7 @@ namespace osu.Game.Configuration
ShowInterface,
ShowProgressGraph,
ShowHealthDisplayWhenCantFail,
FadePlayfieldWhenHealthLow,
MouseDisableButtons,
MouseDisableWheel,
AudioOffset,

View File

@ -53,6 +53,11 @@ namespace osu.Game.Overlays.Settings.Sections.Gameplay
Keywords = new[] { "hp", "bar" }
},
new SettingsCheckbox
{
LabelText = "Fade playfield to red when health is low",
Bindable = config.GetBindable<bool>(OsuSetting.FadePlayfieldWhenHealthLow),
},
new SettingsCheckbox
{
LabelText = "Always show key overlay",
Bindable = config.GetBindable<bool>(OsuSetting.KeyOverlay)

View File

@ -0,0 +1,117 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Utils;
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Rulesets.Scoring;
using osuTK.Graphics;
namespace osu.Game.Screens.Play.HUD
{
/// <summary>
/// An overlay layer on top of the playfield which fades to red when the current player health falls below a certain threshold defined by <see cref="LowHealthThreshold"/>.
/// </summary>
public class FailingLayer : HealthDisplay
{
private const float max_alpha = 0.4f;
private const int fade_time = 400;
private const float gradient_size = 0.3f;
/// <summary>
/// The threshold under which the current player life should be considered low and the layer should start fading in.
/// </summary>
public double LowHealthThreshold = 0.20f;
private readonly Bindable<bool> enabled = new Bindable<bool>();
private readonly Container boxes;
private Bindable<bool> configEnabled;
private HealthProcessor healthProcessor;
public FailingLayer()
{
RelativeSizeAxes = Axes.Both;
Children = new Drawable[]
{
boxes = new Container
{
Alpha = 0,
Blending = BlendingParameters.Additive,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = ColourInfo.GradientVertical(Color4.White, Color4.White.Opacity(0)),
Height = gradient_size,
},
new Box
{
RelativeSizeAxes = Axes.Both,
Height = gradient_size,
Colour = ColourInfo.GradientVertical(Color4.White.Opacity(0), Color4.White),
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
},
}
},
};
}
[BackgroundDependencyLoader]
private void load(OsuColour color, OsuConfigManager config)
{
boxes.Colour = color.Red;
configEnabled = config.GetBindable<bool>(OsuSetting.FadePlayfieldWhenHealthLow);
enabled.BindValueChanged(e => this.FadeTo(e.NewValue ? 1 : 0, fade_time, Easing.OutQuint), true);
}
protected override void LoadComplete()
{
base.LoadComplete();
updateBindings();
}
public override void BindHealthProcessor(HealthProcessor processor)
{
base.BindHealthProcessor(processor);
healthProcessor = processor;
updateBindings();
}
private void updateBindings()
{
if (LoadState < LoadState.Ready)
return;
enabled.UnbindBindings();
// Don't display ever if the ruleset is not using a draining health display.
if (healthProcessor is DrainingHealthProcessor)
enabled.BindTo(configEnabled);
else
enabled.Value = false;
}
protected override void Update()
{
double target = Math.Clamp(max_alpha * (1 - Current.Value / LowHealthThreshold), 0, max_alpha);
boxes.Alpha = (float)Interpolation.Lerp(boxes.Alpha, target, Clock.ElapsedFrameTime * 0.01f);
base.Update();
}
}
}

View File

@ -3,15 +3,29 @@
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Game.Rulesets.Scoring;
using osu.Game.Rulesets.UI;
namespace osu.Game.Screens.Play.HUD
{
/// <summary>
/// A container for components displaying the current player health.
/// Gets bound automatically to the <see cref="HealthProcessor"/> when inserted to <see cref="DrawableRuleset.Overlays"/> hierarchy.
/// </summary>
public abstract class HealthDisplay : Container
{
public readonly BindableDouble Current = new BindableDouble
public readonly BindableDouble Current = new BindableDouble(1)
{
MinValue = 0,
MaxValue = 1
};
/// <summary>
/// Bind the tracked fields of <see cref="HealthProcessor"/> to this health display.
/// </summary>
public virtual void BindHealthProcessor(HealthProcessor processor)
{
Current.BindTo(processor.Health);
}
}
}

View File

@ -37,6 +37,7 @@ namespace osu.Game.Screens.Play
public readonly HitErrorDisplay HitErrorDisplay;
public readonly HoldForMenuButton HoldToQuit;
public readonly PlayerSettingsOverlay PlayerSettingsOverlay;
public readonly FailingLayer FailingLayer;
public Bindable<bool> ShowHealthbar = new Bindable<bool>(true);
@ -75,6 +76,7 @@ namespace osu.Game.Screens.Play
Children = new Drawable[]
{
FailingLayer = CreateFailingLayer(),
visibilityContainer = new Container
{
RelativeSizeAxes = Axes.Both,
@ -260,6 +262,8 @@ namespace osu.Game.Screens.Play
Margin = new MarginPadding { Top = 20 }
};
protected virtual FailingLayer CreateFailingLayer() => new FailingLayer();
protected virtual KeyCounterDisplay CreateKeyCounter() => new KeyCounterDisplay
{
Anchor = Anchor.BottomRight,
@ -304,7 +308,8 @@ namespace osu.Game.Screens.Play
protected virtual void BindHealthProcessor(HealthProcessor processor)
{
HealthDisplay?.Current.BindTo(processor.Health);
HealthDisplay?.BindHealthProcessor(processor);
FailingLayer?.BindHealthProcessor(processor);
}
}
}