// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Extensions.ObjectExtensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Threading; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Scoring; using osu.Game.Rulesets.UI; namespace osu.Game.Screens.Play.HUD { /// /// A container for components displaying the current player health. /// Gets bound automatically to the when inserted to hierarchy. /// public abstract partial class HealthDisplay : CompositeDrawable { private readonly Bindable showHealthBar = new Bindable(true); [Resolved] protected HealthProcessor HealthProcessor { get; private set; } = null!; public Bindable Current { get; } = new BindableDouble { MinValue = 0, MaxValue = 1 }; private BindableNumber health = null!; private ScheduledDelegate? initialIncrease; /// /// Triggered when a is a successful hit, signaling the health display to perform a flash animation (if designed to do so). /// /// The judgement result. protected virtual void Flash(JudgementResult result) { } /// /// Triggered when a resulted in the player losing health. /// /// The judgement result. protected virtual void Miss(JudgementResult result) { } [Resolved] private HUDOverlay? hudOverlay { get; set; } protected override void LoadComplete() { base.LoadComplete(); HealthProcessor.NewJudgement += onNewJudgement; // Don't bind directly so we can animate the startup procedure. health = HealthProcessor.Health.GetBoundCopy(); health.BindValueChanged(h => { Current.Value = h.NewValue; finishInitialAnimation(); }); 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); startInitialAnimation(); } 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; } private void onNewJudgement(JudgementResult judgement) { if (judgement.IsHit && judgement.Type != HitResult.IgnoreHit) Flash(judgement); else if (judgement.Judgement.HealthIncreaseFor(judgement) < 0) Miss(judgement); } protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); if (HealthProcessor.IsNotNull()) HealthProcessor.NewJudgement -= onNewJudgement; } } }