osu/osu.Game/Screens/Play/HUD/FailingLayer.cs

118 lines
4.0 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;
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;
2020-04-09 05:31:25 +00:00
using osu.Game.Rulesets.Scoring;
using osuTK.Graphics;
namespace osu.Game.Screens.Play.HUD
{
/// <summary>
2020-03-18 20:16:54 +00:00
/// 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>
2020-03-18 20:16:54 +00:00
public class FailingLayer : HealthDisplay
{
private const float max_alpha = 0.4f;
private const int fade_time = 400;
2020-04-14 06:09:31 +00:00
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>
2020-03-18 20:41:43 +00:00
public double LowHealthThreshold = 0.20f;
2020-04-14 06:09:31 +00:00
private readonly Bindable<bool> enabled = new Bindable<bool>();
private readonly Container boxes;
2020-04-14 06:09:31 +00:00
private Bindable<bool> configEnabled;
2020-04-14 06:07:32 +00:00
private HealthProcessor healthProcessor;
2020-03-18 20:16:54 +00:00
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;
2020-04-14 06:07:32 +00:00
configEnabled = config.GetBindable<bool>(OsuSetting.FadePlayfieldWhenHealthLow);
enabled.BindValueChanged(e => this.FadeTo(e.NewValue ? 1 : 0, fade_time, Easing.OutQuint), true);
2020-04-14 06:52:38 +00:00
}
2020-04-14 06:07:32 +00:00
2020-04-14 06:52:38 +00:00
protected override void LoadComplete()
{
base.LoadComplete();
2020-04-14 06:07:32 +00:00
updateBindings();
}
2020-04-09 05:31:25 +00:00
public override void BindHealthProcessor(HealthProcessor processor)
{
base.BindHealthProcessor(processor);
2020-04-14 06:07:32 +00:00
healthProcessor = processor;
updateBindings();
}
private void updateBindings()
{
2020-04-14 06:52:38 +00:00
if (LoadState < LoadState.Ready)
2020-04-14 06:07:32 +00:00
return;
2020-04-14 06:21:56 +00:00
enabled.UnbindBindings();
2020-04-14 06:07:32 +00:00
// Don't display ever if the ruleset is not using a draining health display.
if (healthProcessor is DrainingHealthProcessor)
enabled.BindTo(configEnabled);
else
2020-04-09 05:31:25 +00:00
enabled.Value = false;
}
protected override void Update()
{
2020-04-09 05:51:50 +00:00
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();
}
}
}