1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 13:02:56 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/FailingLayer.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

109 lines
3.8 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.
2022-06-17 15:37:17 +08:00
#nullable disable
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 13:31:25 +08:00
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="low_health_threshold"/>.
/// </summary>
2020-03-19 04:16:54 +08:00
public class FailingLayer : HealthDisplay
{
/// <summary>
/// Whether the current player health should be shown on screen.
/// </summary>
public readonly Bindable<bool> ShowHealth = new Bindable<bool>();
private const float max_alpha = 0.4f;
private const int fade_time = 400;
2020-07-28 15:38:31 +08:00
private const float gradient_size = 0.2f;
/// <summary>
/// The threshold under which the current player life should be considered low and the layer should start fading in.
/// </summary>
private const double low_health_threshold = 0.20f;
2020-06-27 01:03:41 +08:00
private readonly Container boxes;
2020-04-14 14:09:31 +08:00
2020-06-29 05:32:04 +08:00
private Bindable<bool> fadePlayfieldWhenHealthLow;
2020-03-19 04:16:54 +08:00
public FailingLayer()
{
RelativeSizeAxes = Axes.Both;
InternalChildren = new Drawable[]
{
boxes = new Container
{
Alpha = 0,
Blending = BlendingParameters.Additive,
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
2020-07-28 15:38:31 +08:00
Colour = ColourInfo.GradientHorizontal(Color4.White, Color4.White.Opacity(0)),
Width = gradient_size,
},
new Box
{
RelativeSizeAxes = Axes.Both,
2020-07-28 15:38:31 +08:00
Width = gradient_size,
Colour = ColourInfo.GradientHorizontal(Color4.White.Opacity(0), Color4.White),
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
},
}
},
};
}
[BackgroundDependencyLoader]
private void load(OsuColour color, OsuConfigManager config)
{
boxes.Colour = color.Red;
2020-06-29 05:32:04 +08:00
fadePlayfieldWhenHealthLow = config.GetBindable<bool>(OsuSetting.FadePlayfieldWhenHealthLow);
fadePlayfieldWhenHealthLow.BindValueChanged(_ => updateState());
ShowHealth.BindValueChanged(_ => updateState());
2020-04-14 14:52:38 +08:00
}
2020-04-14 14:07:32 +08:00
2020-04-14 14:52:38 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
2020-06-29 05:32:04 +08:00
updateState();
}
2020-06-27 01:06:41 +08:00
private void updateState()
{
2020-06-29 05:32:04 +08:00
// Don't display ever if the ruleset is not using a draining health display.
bool showLayer = HealthProcessor is DrainingHealthProcessor && fadePlayfieldWhenHealthLow.Value && ShowHealth.Value;
2020-06-27 01:06:41 +08:00
this.FadeTo(showLayer ? 1 : 0, fade_time, Easing.OutQuint);
}
protected override void Update()
{
double target = Math.Clamp(max_alpha * (1 - Current.Value / low_health_threshold), 0, max_alpha);
2020-04-09 13:51:50 +08:00
boxes.Alpha = (float)Interpolation.Lerp(boxes.Alpha, target, Clock.ElapsedFrameTime * 0.01f);
base.Update();
}
}
}