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.
using System ;
using osu.Framework.Allocation ;
2020-03-30 10:59:39 +00:00
using osu.Framework.Bindables ;
2020-03-17 21:32:07 +00:00
using osu.Framework.Graphics ;
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-03-17 21:32:07 +00:00
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"/>.
2020-03-17 21:32:07 +00:00
/// </summary>
2020-03-18 20:16:54 +00:00
public class FailingLayer : HealthDisplay
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-03-17 21:32:07 +00:00
private readonly Box box ;
2020-03-30 10:59:39 +00:00
private Bindable < bool > enabled ;
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-03-18 20:41:43 +00:00
public double LowHealthThreshold = 0.20f ;
2020-03-17 21:32:07 +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 ;
2020-03-17 21:32:07 +00:00
Child = box = new Box
{
RelativeSizeAxes = Axes . Both ,
Alpha = 0
} ;
}
[BackgroundDependencyLoader]
2020-03-30 10:59:39 +00:00
private void load ( OsuColour color , OsuConfigManager config )
2020-03-17 21:32:07 +00:00
{
box . Colour = color . Red ;
2020-03-30 10:59:39 +00:00
enabled = config . GetBindable < bool > ( OsuSetting . FadePlayfieldWhenHealthLow ) ;
enabled . BindValueChanged ( e = > this . FadeTo ( e . NewValue ? 1 : 0 , fade_time , Easing . OutQuint ) , true ) ;
2020-03-17 21:32:07 +00:00
}
2020-04-09 05:31:25 +00:00
public override void BindHealthProcessor ( HealthProcessor processor )
{
base . BindHealthProcessor ( processor ) ;
if ( ! ( processor is DrainingHealthProcessor ) )
{
enabled . UnbindBindings ( ) ;
enabled . Value = false ;
}
}
2020-03-17 21:32:07 +00:00
protected override void Update ( )
{
2020-03-26 11:14:44 +00:00
box . Alpha = ( float ) Interpolation . ValueAt ( Math . Clamp ( Clock . ElapsedFrameTime , 0 , fade_time ) , box . Alpha ,
Math . Clamp ( max_alpha * ( 1 - Current . Value / LowHealthThreshold ) , 0 , max_alpha ) , 0 , fade_time , Easing . Out ) ;
2020-03-17 21:32:07 +00:00
base . Update ( ) ;
}
}
}