1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 12:27:26 +08:00
osu-lazer/osu.Game/Screens/Play/HUD/HealthDisplay.cs

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

156 lines
5.5 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.
2018-04-13 17:19:50 +08:00
using System;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions.ObjectExtensions;
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;
2024-01-09 16:30:13 +08:00
using osu.Framework.Utils;
using osu.Game.Rulesets.Judgements;
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
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.
/// 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>
public abstract partial class HealthDisplay : CompositeDrawable
2017-01-10 16:01:53 +08:00
{
private readonly Bindable<bool> showHealthBar = new Bindable<bool>(true);
[Resolved]
protected HealthProcessor HealthProcessor { get; private set; } = null!;
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,
2017-02-16 21:44:21 +08:00
};
2020-03-18 15:17:41 +08:00
2023-10-02 16:01:56 +08:00
private BindableNumber<double> health = null!;
protected bool InitialAnimationPlaying => initialIncrease != null;
2023-10-02 16:01:56 +08:00
private ScheduledDelegate? initialIncrease;
/// <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).
/// Calls to this method are debounced.
/// </summary>
protected virtual void Flash()
{
}
[Resolved]
private HUDOverlay? hudOverlay { get; set; }
protected override void LoadComplete()
{
base.LoadComplete();
HealthProcessor.NewJudgement += onNewJudgement;
2023-10-02 16:01:56 +08:00
// Don't bind directly so we can animate the startup procedure.
health = HealthProcessor.Health.GetBoundCopy();
if (hudOverlay != null)
showHealthBar.BindTo(hudOverlay.ShowHealthBar);
// this probably shouldn't be operating on `this.`
showHealthBar.BindValueChanged(healthBar => this.FadeTo(healthBar.NewValue ? 1 : 0, HUDOverlay.FADE_DURATION, HUDOverlay.FADE_EASING), true);
2023-10-02 16:01:56 +08:00
initialHealthValue = health.Value;
if (PlayInitialIncreaseAnimation)
startInitialAnimation();
else
Current.Value = health.Value;
2023-10-02 16:01:56 +08:00
}
private double lastValue;
private double initialHealthValue;
protected override void Update()
{
base.Update();
if (!InitialAnimationPlaying || health.Value != initialHealthValue)
{
Current.Value = health.Value;
if (initialIncrease != null)
FinishInitialAnimation(Current.Value);
}
// Health changes every frame in draining situations.
// Manually handle value changes to avoid bindable event flow overhead.
if (!Precision.AlmostEquals(lastValue, Current.Value, 0.001f))
{
HealthChanged(Current.Value > lastValue);
lastValue = Current.Value;
}
}
2024-01-09 16:30:13 +08:00
protected virtual void HealthChanged(bool increase)
{
}
2023-10-02 16:01:56 +08:00
private void startInitialAnimation()
{
if (Current.Value >= health.Value)
return;
2023-10-02 16:01:56 +08:00
// 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 = Math.Min(Current.Value + 0.05f, health.Value);
2023-10-02 16:01:56 +08:00
this.TransformBindableTo(Current, newValue, increase_delay);
Scheduler.AddOnce(Flash);
2023-10-02 16:01:56 +08:00
if (newValue >= health.Value)
FinishInitialAnimation(health.Value);
2023-10-02 16:01:56 +08:00
}, increase_delay, true);
}
protected virtual void FinishInitialAnimation(double value)
2023-10-02 16:01:56 +08:00
{
if (initialIncrease == null)
return;
initialIncrease.Cancel();
2023-10-02 16:01:56 +08:00
initialIncrease = null;
// 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.
// 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)}");
}
private void onNewJudgement(JudgementResult judgement)
{
2021-05-10 12:41:04 +08:00
if (judgement.IsHit && judgement.Type != HitResult.IgnoreHit)
Scheduler.AddOnce(Flash);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (HealthProcessor.IsNotNull())
HealthProcessor.NewJudgement -= onNewJudgement;
}
2017-01-10 16:01:53 +08:00
}
}