2020-03-17 21:32:07 +00:00
// 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 07:37:17 +00:00
#nullable disable
2020-03-17 21:32:07 +00:00
using System ;
using osu.Framework.Allocation ;
2020-03-30 10:59:39 +00:00
using osu.Framework.Bindables ;
2020-04-09 05:33:11 +00:00
using osu.Framework.Extensions.Color4Extensions ;
2020-03-17 21:32:07 +00:00
using osu.Framework.Graphics ;
2020-04-09 05:33:11 +00:00
using osu.Framework.Graphics.Colour ;
using osu.Framework.Graphics.Containers ;
2020-03-17 21:32:07 +00:00
using osu.Framework.Graphics.Shapes ;
2020-03-26 11:14:44 +00:00
using osu.Framework.Utils ;
2020-03-30 10:59:39 +00:00
using osu.Game.Configuration ;
2020-03-17 21:32:07 +00:00
using osu.Game.Graphics ;
2020-04-09 05:31:25 +00:00
using osu.Game.Rulesets.Scoring ;
2020-04-09 05:33:11 +00:00
using osuTK.Graphics ;
2020-03-17 21:32:07 +00:00
namespace osu.Game.Screens.Play.HUD
{
/// <summary>
2020-06-26 17:23:42 +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="low_health_threshold"/>.
2020-03-17 21:32:07 +00:00
/// </summary>
2020-03-18 20:16:54 +00:00
public partial class FailingLayer : HealthDisplay
2020-03-17 21:32:07 +00:00
{
2020-06-26 17:23:42 +00:00
/// <summary>
/// Whether the current player health should be shown on screen.
/// </summary>
public readonly Bindable < bool > ShowHealth = new Bindable < bool > ( ) ;
2020-03-17 21:32:07 +00:00
private const float max_alpha = 0.4f ;
2020-03-26 11:14:44 +00:00
private const int fade_time = 400 ;
2020-07-28 07:38:31 +00:00
private const float gradient_size = 0.2f ;
2020-03-30 10:59:39 +00:00
2020-03-17 21:32:07 +00:00
/// <summary>
/// The threshold under which the current player life should be considered low and the layer should start fading in.
/// </summary>
2020-06-26 17:23:42 +00:00
private const double low_health_threshold = 0.20f ;
2020-06-26 17:03:41 +00:00
2020-04-09 05:33:11 +00:00
private readonly Container boxes ;
2020-04-14 06:09:31 +00:00
2020-06-28 21:32:04 +00:00
private Bindable < bool > fadePlayfieldWhenHealthLow ;
2020-04-09 05:33:11 +00:00
2020-03-18 20:16:54 +00:00
public FailingLayer ( )
2020-03-17 21:32:07 +00:00
{
2020-03-17 21:57:47 +00:00
RelativeSizeAxes = Axes . Both ;
2021-05-11 03:21:31 +00:00
InternalChildren = new Drawable [ ]
2020-03-17 21:32:07 +00:00
{
2020-04-09 05:33:11 +00:00
boxes = new Container
{
2020-04-09 05:49:09 +00:00
Alpha = 0 ,
2020-04-09 05:33:11 +00:00
Blending = BlendingParameters . Additive ,
RelativeSizeAxes = Axes . Both ,
Children = new Drawable [ ]
{
new Box
{
RelativeSizeAxes = Axes . Both ,
2020-07-28 07:38:31 +00:00
Colour = ColourInfo . GradientHorizontal ( Color4 . White , Color4 . White . Opacity ( 0 ) ) ,
Width = gradient_size ,
2020-04-09 05:33:11 +00:00
} ,
new Box
{
RelativeSizeAxes = Axes . Both ,
2020-07-28 07:38:31 +00:00
Width = gradient_size ,
Colour = ColourInfo . GradientHorizontal ( Color4 . White . Opacity ( 0 ) , Color4 . White ) ,
Anchor = Anchor . TopRight ,
Origin = Anchor . TopRight ,
2020-04-09 05:33:11 +00:00
} ,
}
} ,
2020-03-17 21:32:07 +00:00
} ;
}
[BackgroundDependencyLoader]
2020-03-30 10:59:39 +00:00
private void load ( OsuColour color , OsuConfigManager config )
2020-03-17 21:32:07 +00:00
{
2020-04-09 05:33:11 +00:00
boxes . Colour = color . Red ;
2020-06-28 21:32:04 +00:00
fadePlayfieldWhenHealthLow = config . GetBindable < bool > ( OsuSetting . FadePlayfieldWhenHealthLow ) ;
fadePlayfieldWhenHealthLow . BindValueChanged ( _ = > updateState ( ) ) ;
ShowHealth . BindValueChanged ( _ = > updateState ( ) ) ;
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-06-28 21:32:04 +00:00
updateState ( ) ;
2020-03-17 21:32:07 +00:00
}
2020-06-26 17:06:41 +00:00
private void updateState ( )
2020-06-26 12:32:01 +00:00
{
2020-06-28 21:32:04 +00:00
// Don't display ever if the ruleset is not using a draining health display.
2021-10-27 04:04:41 +00:00
bool showLayer = HealthProcessor is DrainingHealthProcessor & & fadePlayfieldWhenHealthLow . Value & & ShowHealth . Value ;
2020-06-26 17:06:41 +00:00
this . FadeTo ( showLayer ? 1 : 0 , fade_time , Easing . OutQuint ) ;
2020-06-26 12:32:01 +00:00
}
2020-03-17 21:32:07 +00:00
protected override void Update ( )
{
2020-06-26 17:23:42 +00:00
double target = Math . Clamp ( max_alpha * ( 1 - Current . Value / low_health_threshold ) , 0 , max_alpha ) ;
2020-04-09 05:51:50 +00:00
boxes . Alpha = ( float ) Interpolation . Lerp ( boxes . Alpha , target , Clock . ElapsedFrameTime * 0.01f ) ;
2020-03-26 11:14:44 +00:00
2020-03-17 21:32:07 +00:00
base . Update ( ) ;
}
}
}