2019-01-24 16:43:03 +08: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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-05-07 15:56:24 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2023-06-23 23:59:36 +08:00
|
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2021-05-10 14:40:33 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2017-01-10 16:01:53 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2023-10-02 16:01:56 +08:00
|
|
|
|
using osu.Framework.Threading;
|
2020-10-16 12:42:50 +08:00
|
|
|
|
using osu.Game.Rulesets.Judgements;
|
2023-10-02 16:01:56 +08:00
|
|
|
|
using osu.Game.Rulesets.Objects;
|
2020-03-18 05:57:47 +08:00
|
|
|
|
using osu.Game.Rulesets.Scoring;
|
2020-03-18 15:17:41 +08:00
|
|
|
|
using osu.Game.Rulesets.UI;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-05-05 12:00:05 +08:00
|
|
|
|
namespace osu.Game.Screens.Play.HUD
|
2017-01-10 16:01:53 +08:00
|
|
|
|
{
|
2020-03-18 15:17:41 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A container for components displaying the current player health.
|
2021-05-07 15:56:24 +08:00
|
|
|
|
/// Gets bound automatically to the <see cref="Rulesets.Scoring.HealthProcessor"/> when inserted to <see cref="DrawableRuleset.Overlays"/> hierarchy.
|
2020-03-18 15:17:41 +08:00
|
|
|
|
/// </summary>
|
2021-05-11 11:21:31 +08:00
|
|
|
|
public abstract partial class HealthDisplay : CompositeDrawable
|
2017-01-10 16:01:53 +08:00
|
|
|
|
{
|
2021-12-10 13:15:00 +08:00
|
|
|
|
private readonly Bindable<bool> showHealthBar = new Bindable<bool>(true);
|
2021-05-10 14:40:33 +08:00
|
|
|
|
|
2021-05-07 15:56:24 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
protected HealthProcessor HealthProcessor { get; private set; } = null!;
|
|
|
|
|
|
2023-10-07 00:03:41 +08:00
|
|
|
|
protected virtual bool PlayInitialIncreaseAnimation => true;
|
|
|
|
|
|
2023-10-02 16:01:56 +08:00
|
|
|
|
public Bindable<double> Current { get; } = new BindableDouble
|
2017-02-16 21:44:21 +08:00
|
|
|
|
{
|
|
|
|
|
MinValue = 0,
|
|
|
|
|
MaxValue = 1
|
|
|
|
|
};
|
2020-03-18 15:17:41 +08:00
|
|
|
|
|
2023-10-02 16:01:56 +08:00
|
|
|
|
private BindableNumber<double> health = null!;
|
|
|
|
|
|
|
|
|
|
private ScheduledDelegate? initialIncrease;
|
|
|
|
|
|
2023-10-01 05:21:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Triggered when a <see cref="Judgement"/> is a successful hit, signaling the health display to perform a flash animation (if designed to do so).
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="result">The judgement result.</param>
|
2021-05-07 15:56:24 +08:00
|
|
|
|
protected virtual void Flash(JudgementResult result)
|
2020-10-16 12:42:50 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-01 05:21:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Triggered when a <see cref="Judgement"/> resulted in the player losing health.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="result">The judgement result.</param>
|
|
|
|
|
protected virtual void Miss(JudgementResult result)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 11:53:30 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private HUDOverlay? hudOverlay { get; set; }
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
2021-05-07 15:56:24 +08:00
|
|
|
|
{
|
2021-05-12 11:53:30 +08:00
|
|
|
|
base.LoadComplete();
|
2021-05-07 15:56:24 +08:00
|
|
|
|
|
|
|
|
|
HealthProcessor.NewJudgement += onNewJudgement;
|
2021-05-10 14:40:33 +08:00
|
|
|
|
|
2023-10-02 16:01:56 +08:00
|
|
|
|
// Don't bind directly so we can animate the startup procedure.
|
|
|
|
|
health = HealthProcessor.Health.GetBoundCopy();
|
|
|
|
|
health.BindValueChanged(h =>
|
|
|
|
|
{
|
|
|
|
|
finishInitialAnimation();
|
2023-10-07 03:30:41 +08:00
|
|
|
|
Current.Value = h.NewValue;
|
2023-10-02 16:01:56 +08:00
|
|
|
|
});
|
|
|
|
|
|
2021-05-12 11:53:30 +08:00
|
|
|
|
if (hudOverlay != null)
|
2021-12-10 13:15:00 +08:00
|
|
|
|
showHealthBar.BindTo(hudOverlay.ShowHealthBar);
|
2021-05-12 11:53:30 +08:00
|
|
|
|
|
|
|
|
|
// this probably shouldn't be operating on `this.`
|
2021-12-10 13:15:00 +08:00
|
|
|
|
showHealthBar.BindValueChanged(healthBar => this.FadeTo(healthBar.NewValue ? 1 : 0, HUDOverlay.FADE_DURATION, HUDOverlay.FADE_EASING), true);
|
2023-10-02 16:01:56 +08:00
|
|
|
|
|
2023-10-07 00:03:41 +08:00
|
|
|
|
if (PlayInitialIncreaseAnimation)
|
|
|
|
|
startInitialAnimation();
|
|
|
|
|
else
|
|
|
|
|
Current.Value = 1;
|
2023-10-02 16:01:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startInitialAnimation()
|
|
|
|
|
{
|
|
|
|
|
// TODO: this should run in gameplay time, including showing a larger increase when skipping.
|
|
|
|
|
// TODO: it should also start increasing relative to the first hitobject.
|
|
|
|
|
const double increase_delay = 150;
|
|
|
|
|
|
|
|
|
|
initialIncrease = Scheduler.AddDelayed(() =>
|
|
|
|
|
{
|
|
|
|
|
double newValue = Current.Value + 0.05f;
|
|
|
|
|
this.TransformBindableTo(Current, newValue, increase_delay);
|
|
|
|
|
Flash(new JudgementResult(new HitObject(), new Judgement()));
|
|
|
|
|
|
|
|
|
|
if (newValue >= 1)
|
|
|
|
|
finishInitialAnimation();
|
|
|
|
|
}, increase_delay, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void finishInitialAnimation()
|
|
|
|
|
{
|
|
|
|
|
initialIncrease?.Cancel();
|
|
|
|
|
initialIncrease = null;
|
2023-10-07 03:30:41 +08:00
|
|
|
|
|
|
|
|
|
// aside from the repeating `initialIncrease` scheduled task,
|
|
|
|
|
// there may also be a `Current` transform in progress from that schedule.
|
|
|
|
|
// ensure it plays out fully, to prevent changes to `Current.Value` being discarded by the ongoing transform.
|
2023-10-07 03:39:29 +08:00
|
|
|
|
// and yes, this funky `targetMember` spec is seemingly the only way to do this
|
|
|
|
|
// (see: https://github.com/ppy/osu-framework/blob/fe2769171c6e26d1b6fdd6eb7ea8353162fe9065/osu.Framework/Graphics/Transforms/TransformBindable.cs#L21)
|
|
|
|
|
FinishTransforms(targetMember: $"{Current.GetHashCode()}.{nameof(Current.Value)}");
|
2021-05-07 15:56:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onNewJudgement(JudgementResult judgement)
|
2020-03-18 05:57:47 +08:00
|
|
|
|
{
|
2021-05-10 12:41:04 +08:00
|
|
|
|
if (judgement.IsHit && judgement.Type != HitResult.IgnoreHit)
|
|
|
|
|
Flash(judgement);
|
2023-10-01 05:21:12 +08:00
|
|
|
|
else if (judgement.Judgement.HealthIncreaseFor(judgement) < 0)
|
|
|
|
|
Miss(judgement);
|
2021-05-07 15:56:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
2023-06-23 23:59:36 +08:00
|
|
|
|
if (HealthProcessor.IsNotNull())
|
2021-05-07 15:56:24 +08:00
|
|
|
|
HealthProcessor.NewJudgement -= onNewJudgement;
|
2020-03-18 05:57:47 +08:00
|
|
|
|
}
|
2017-01-10 16:01:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|