2020-11-05 10:47:23 +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.
2023-01-19 10:43:23 +00:00
using System ;
2020-11-10 11:16:52 +00:00
using osu.Framework.Bindables ;
2020-11-05 10:47:23 +00:00
using osu.Framework.Graphics.Performance ;
2020-11-10 11:16:52 +00:00
using osu.Game.Rulesets.Judgements ;
2020-11-05 10:47:23 +00:00
using osu.Game.Rulesets.Objects.Drawables ;
namespace osu.Game.Rulesets.Objects
{
/// <summary>
/// A <see cref="LifetimeEntry"/> that stores the lifetime for a <see cref="HitObject"/>.
/// </summary>
public class HitObjectLifetimeEntry : LifetimeEntry
{
/// <summary>
/// The <see cref="HitObject"/>.
/// </summary>
public readonly HitObject HitObject ;
2020-11-10 11:16:52 +00:00
/// <summary>
/// The result that <see cref="HitObject"/> was judged with.
/// This is set by the accompanying <see cref="DrawableHitObject"/>, and reused when required for rewinding.
/// </summary>
2022-09-14 06:24:20 +00:00
internal JudgementResult ? Result ;
2020-11-10 11:16:52 +00:00
private readonly IBindable < double > startTimeBindable = new BindableDouble ( ) ;
2023-01-19 10:43:23 +00:00
internal event Action ? RevertResult ;
2020-11-05 10:47:23 +00:00
/// <summary>
/// Creates a new <see cref="HitObjectLifetimeEntry"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to store the lifetime of.</param>
public HitObjectLifetimeEntry ( HitObject hitObject )
{
HitObject = hitObject ;
2020-11-10 11:16:52 +00:00
startTimeBindable . BindTo ( HitObject . StartTimeBindable ) ;
2021-05-31 06:33:28 +00:00
startTimeBindable . BindValueChanged ( _ = > SetInitialLifetime ( ) , true ) ;
2021-05-28 12:26:21 +00:00
2021-05-31 04:41:49 +00:00
// Subscribe to this event before the DrawableHitObject so that the local callback is invoked before the entry is re-applied as a result of DefaultsApplied.
// This way, the DrawableHitObject can use OnApply() to overwrite the LifetimeStart that was set inside setInitialLifetime().
2021-05-31 06:33:28 +00:00
HitObject . DefaultsApplied + = _ = > SetInitialLifetime ( ) ;
2020-11-05 10:47:23 +00:00
}
2021-04-27 06:23:33 +00:00
// The lifetime, as set by the hitobject.
2020-11-05 10:47:23 +00:00
private double realLifetimeStart = double . MinValue ;
private double realLifetimeEnd = double . MaxValue ;
2021-04-27 08:54:18 +00:00
// This method is called even if `start == LifetimeStart` when `KeepAlive` is true (necessary to update `realLifetimeStart`).
protected override void SetLifetimeStart ( double start )
2020-11-05 10:47:23 +00:00
{
2021-04-27 06:23:33 +00:00
realLifetimeStart = start ;
2021-04-27 10:37:01 +00:00
if ( ! keepAlive )
base . SetLifetimeStart ( start ) ;
2021-04-27 08:54:18 +00:00
}
protected override void SetLifetimeEnd ( double end )
{
2021-04-27 06:23:33 +00:00
realLifetimeEnd = end ;
2021-04-27 10:37:01 +00:00
if ( ! keepAlive )
base . SetLifetimeEnd ( end ) ;
2020-11-05 10:47:23 +00:00
}
private bool keepAlive ;
/// <summary>
/// Whether the <see cref="HitObject"/> should be kept always alive.
/// </summary>
internal bool KeepAlive
{
set
{
if ( keepAlive = = value )
return ;
keepAlive = value ;
2021-04-27 10:37:01 +00:00
if ( keepAlive )
SetLifetime ( double . MinValue , double . MaxValue ) ;
else
SetLifetime ( realLifetimeStart , realLifetimeEnd ) ;
2020-11-05 10:47:23 +00:00
}
}
/// <summary>
/// A safe offset prior to the start time of <see cref="HitObject"/> at which it may begin displaying contents.
/// By default, <see cref="HitObject"/>s are assumed to display their contents within 10 seconds prior to their start time.
/// </summary>
/// <remarks>
2021-05-28 08:19:36 +00:00
/// This is only used as an optimisation to delay the initial application of the <see cref="HitObject"/> to a <see cref="DrawableHitObject"/>.
/// A more accurate <see cref="LifetimeEntry.LifetimeStart"/> should be set on the hit object application, for further optimisation.
2020-11-05 10:47:23 +00:00
/// </remarks>
protected virtual double InitialLifetimeOffset = > 10000 ;
/// <summary>
2021-05-28 12:26:21 +00:00
/// Set <see cref="LifetimeEntry.LifetimeStart"/> using <see cref="InitialLifetimeOffset"/>.
2020-11-05 10:47:23 +00:00
/// </summary>
2021-06-02 02:11:41 +00:00
internal void SetInitialLifetime ( ) = > LifetimeStart = HitObject . StartTime - InitialLifetimeOffset ;
2023-01-19 10:43:23 +00:00
internal void OnRevertResult ( ) = > RevertResult ? . Invoke ( ) ;
2020-11-05 10:47:23 +00:00
}
}