1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:57:39 +08:00
osu-lazer/osu.Game/Screens/Play/GameplayClockContainer.cs

150 lines
5.3 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.
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Timing;
namespace osu.Game.Screens.Play
{
2021-04-16 19:33:29 +08:00
/// <summary>
/// Encapsulates gameplay timing logic and provides a <see cref="GameplayClock"/> via DI for gameplay components to use.
/// </summary>
2021-04-14 16:47:11 +08:00
public abstract class GameplayClockContainer : Container
{
2021-04-14 16:47:11 +08:00
/// <summary>
2021-04-16 19:33:29 +08:00
/// The final clock which is exposed to gameplay components.
2021-04-14 16:47:11 +08:00
/// </summary>
public GameplayClock GameplayClock { get; private set; }
2021-04-16 19:33:29 +08:00
/// <summary>
/// Whether gameplay is paused.
/// </summary>
public readonly BindableBool IsPaused = new BindableBool();
/// <summary>
2021-04-16 19:33:29 +08:00
/// The adjustable source clock used for gameplay. Should be used for seeks and clock control.
/// </summary>
2021-04-16 19:33:29 +08:00
protected readonly DecoupleableInterpolatingFramedClock AdjustableSource;
2021-04-19 18:55:59 +08:00
/// <summary>
/// The source clock.
/// </summary>
protected IClock SourceClock { get; private set; }
2021-04-16 19:33:29 +08:00
/// <summary>
/// Creates a new <see cref="GameplayClockContainer"/>.
/// </summary>
/// <param name="sourceClock">The source <see cref="IClock"/> used for timing.</param>
2021-04-14 16:47:11 +08:00
protected GameplayClockContainer(IClock sourceClock)
{
2021-04-19 18:55:59 +08:00
SourceClock = sourceClock;
2021-04-14 16:47:11 +08:00
RelativeSizeAxes = Axes.Both;
2021-04-16 19:33:29 +08:00
AdjustableSource = new DecoupleableInterpolatingFramedClock { IsCoupled = false };
2021-04-14 18:50:22 +08:00
IsPaused.BindValueChanged(OnIsPausedChanged);
2021-04-14 16:47:11 +08:00
}
2021-04-14 16:47:11 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
2021-04-14 16:47:11 +08:00
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
2020-08-06 17:31:08 +08:00
2021-04-16 19:33:29 +08:00
dependencies.CacheAs(GameplayClock = CreateGameplayClock(AdjustableSource));
2021-04-14 17:29:34 +08:00
GameplayClock.IsPaused.BindTo(IsPaused);
2021-04-14 16:47:11 +08:00
return dependencies;
}
2021-04-16 19:33:29 +08:00
/// <summary>
/// Starts gameplay.
/// </summary>
2021-04-14 16:47:11 +08:00
public virtual void Start()
{
2021-04-20 12:56:13 +08:00
// Ensure that the source clock is set.
ChangeSource(SourceClock);
2021-04-16 19:33:29 +08:00
if (!AdjustableSource.IsRunning)
2021-04-14 16:47:11 +08:00
{
// Seeking the decoupled clock to its current time ensures that its source clock will be seeked to the same time
// This accounts for the clock source potentially taking time to enter a completely stopped state
Seek(GameplayClock.CurrentTime);
2021-04-16 19:33:29 +08:00
AdjustableSource.Start();
2021-04-14 16:47:11 +08:00
}
2021-04-14 16:47:11 +08:00
IsPaused.Value = false;
}
2021-04-14 16:47:11 +08:00
/// <summary>
/// Seek to a specific time in gameplay.
/// </summary>
/// <param name="time">The destination time to seek to.</param>
2021-04-20 16:35:59 +08:00
public virtual void Seek(double time)
{
AdjustableSource.Seek(time);
// Manually process to make sure the gameplay clock is correctly updated after a seek.
GameplayClock.UnderlyingClock.ProcessFrame();
}
2021-04-16 19:33:29 +08:00
/// <summary>
/// Stops gameplay.
/// </summary>
2021-04-14 16:47:11 +08:00
public virtual void Stop() => IsPaused.Value = true;
2021-04-16 19:33:29 +08:00
/// <summary>
2021-04-19 18:53:55 +08:00
/// Resets this <see cref="GameplayClockContainer"/> and the source to an initial state ready for gameplay.
2021-04-16 19:33:29 +08:00
/// </summary>
2021-04-19 18:53:55 +08:00
public virtual void Reset()
{
2021-04-23 17:46:59 +08:00
ChangeSource(SourceClock);
Seek(0);
2021-04-14 16:47:11 +08:00
2021-04-20 16:36:24 +08:00
// Manually stop the source in order to not affect the IsPaused state.
AdjustableSource.Stop();
2021-04-14 16:47:11 +08:00
if (!IsPaused.Value)
Start();
}
2021-04-19 18:55:59 +08:00
/// <summary>
/// Changes the source clock.
/// </summary>
/// <param name="sourceClock">The new source.</param>
protected void ChangeSource(IClock sourceClock) => AdjustableSource.ChangeSource(SourceClock = sourceClock);
2021-04-14 20:15:14 +08:00
protected override void Update()
{
if (!IsPaused.Value)
GameplayClock.UnderlyingClock.ProcessFrame();
2021-04-14 20:15:14 +08:00
base.Update();
}
2021-04-16 19:33:29 +08:00
/// <summary>
/// Invoked when the value of <see cref="IsPaused"/> is changed to start or stop the <see cref="AdjustableSource"/> clock.
/// </summary>
/// <param name="isPaused">Whether the clock should now be paused.</param>
protected virtual void OnIsPausedChanged(ValueChangedEvent<bool> isPaused)
{
if (isPaused.NewValue)
AdjustableSource.Stop();
else
AdjustableSource.Start();
}
2021-04-14 16:47:11 +08:00
2021-04-16 19:33:29 +08:00
/// <summary>
/// Creates the final <see cref="GameplayClock"/> which is exposed via DI to be used by gameplay components.
/// </summary>
/// <remarks>
/// Any intermediate clocks such as platform offsets should be applied here.
/// </remarks>
/// <param name="source">The <see cref="IFrameBasedClock"/> providing the source time.</param>
/// <returns>The final <see cref="GameplayClock"/>.</returns>
2021-04-14 20:15:14 +08:00
protected abstract GameplayClock CreateGameplayClock(IFrameBasedClock source);
2021-04-14 16:47:11 +08:00
}
}