2019-03-06 19:30:14 +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.
|
|
|
|
|
2021-04-15 18:07:25 +08:00
|
|
|
using System;
|
2022-08-15 16:36:18 +08:00
|
|
|
using System.Collections.Generic;
|
2022-08-15 18:46:29 +08:00
|
|
|
using System.Linq;
|
2019-03-06 19:30:14 +08:00
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2022-06-28 14:23:29 +08:00
|
|
|
using osu.Framework.Logging;
|
2019-03-06 19:30:14 +08:00
|
|
|
using osu.Framework.Timing;
|
2022-08-15 18:46:29 +08:00
|
|
|
using osu.Framework.Utils;
|
2019-03-06 19:30:14 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
{
|
2021-04-16 19:33:29 +08:00
|
|
|
/// <summary>
|
2022-08-15 18:59:08 +08:00
|
|
|
/// Encapsulates gameplay timing logic and provides a <see cref="IGameplayClock"/> via DI for gameplay components to use.
|
2021-04-16 19:33:29 +08:00
|
|
|
/// </summary>
|
2022-08-15 16:36:18 +08:00
|
|
|
public class GameplayClockContainer : Container, IAdjustableClock, IGameplayClock
|
2019-03-06 19:30:14 +08:00
|
|
|
{
|
2021-04-16 19:33:29 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether gameplay is paused.
|
|
|
|
/// </summary>
|
2022-08-15 16:36:18 +08:00
|
|
|
public IBindable<bool> IsPaused => isPaused;
|
|
|
|
|
2021-04-19 18:55:59 +08:00
|
|
|
/// <summary>
|
2022-08-15 18:59:08 +08:00
|
|
|
/// The source clock. Should generally not be used for any timekeeping purposes.
|
2021-04-19 18:55:59 +08:00
|
|
|
/// </summary>
|
2022-08-15 15:56:16 +08:00
|
|
|
public IClock SourceClock { get; private set; }
|
2021-04-19 18:55:59 +08:00
|
|
|
|
2021-09-20 22:28:58 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Invoked when a seek has been performed via <see cref="Seek"/>
|
|
|
|
/// </summary>
|
2022-08-15 16:06:24 +08:00
|
|
|
public event Action? OnSeek;
|
2021-09-20 22:28:58 +08:00
|
|
|
|
2022-03-17 19:54:42 +08:00
|
|
|
/// <summary>
|
2022-03-17 22:39:45 +08:00
|
|
|
/// The time from which the clock should start. Will be seeked to on calling <see cref="Reset"/>.
|
2022-03-17 19:54:42 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// If not set, a value of zero will be used.
|
|
|
|
/// Importantly, the value will be inferred from the current ruleset in <see cref="MasterGameplayClockContainer"/> unless specified.
|
|
|
|
/// </remarks>
|
2022-08-15 18:46:29 +08:00
|
|
|
public double? StartTime { get; set; }
|
2022-07-29 21:12:52 +08:00
|
|
|
|
2022-08-15 18:46:29 +08:00
|
|
|
public virtual IEnumerable<double> NonGameplayAdjustments => Enumerable.Empty<double>();
|
2022-08-15 16:36:18 +08:00
|
|
|
|
2022-08-15 16:06:24 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The final clock which is exposed to gameplay components.
|
|
|
|
/// </summary>
|
2022-08-15 19:09:07 +08:00
|
|
|
protected IFrameBasedClock FramedClock { get; private set; }
|
2022-08-15 18:59:08 +08:00
|
|
|
|
|
|
|
private readonly BindableBool isPaused = new BindableBool(true);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The adjustable source clock used for gameplay. Should be used for seeks and clock control.
|
|
|
|
/// </summary>
|
|
|
|
private readonly DecoupleableInterpolatingFramedClock decoupledClock;
|
2022-08-15 16:06:24 +08:00
|
|
|
|
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>
|
2022-08-15 15:56:16 +08:00
|
|
|
public GameplayClockContainer(IClock sourceClock)
|
2019-03-06 19:30:14 +08:00
|
|
|
{
|
2021-04-19 18:55:59 +08:00
|
|
|
SourceClock = sourceClock;
|
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
RelativeSizeAxes = Axes.Both;
|
2019-03-06 19:30:14 +08:00
|
|
|
|
2022-08-15 18:59:08 +08:00
|
|
|
decoupledClock = new DecoupleableInterpolatingFramedClock { IsCoupled = false };
|
2021-04-14 18:50:22 +08:00
|
|
|
IsPaused.BindValueChanged(OnIsPausedChanged);
|
2022-08-15 19:09:07 +08:00
|
|
|
|
|
|
|
// this will be replaced during load, but non-null for tests which don't add this component to the hierarchy.
|
|
|
|
FramedClock = new FramedClock();
|
2021-04-14 16:47:11 +08:00
|
|
|
}
|
2019-03-06 19:30:14 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
|
2019-03-06 19:30:14 +08:00
|
|
|
{
|
2021-04-14 16:47:11 +08:00
|
|
|
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
|
2020-08-06 17:31:08 +08:00
|
|
|
|
2022-08-15 18:59:08 +08:00
|
|
|
FramedClock = CreateGameplayClock(decoupledClock);
|
2022-08-15 17:14:57 +08:00
|
|
|
|
|
|
|
dependencies.CacheAs<IGameplayClock>(this);
|
2022-07-29 21:12:52 +08:00
|
|
|
|
2021-04-14 16:47:11 +08:00
|
|
|
return dependencies;
|
|
|
|
}
|
2019-03-06 19:30:14 +08:00
|
|
|
|
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-24 03:56:08 +08:00
|
|
|
ensureSourceClockSet();
|
2021-04-20 12:56:13 +08:00
|
|
|
|
2022-08-15 18:59:08 +08:00
|
|
|
if (!decoupledClock.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
|
2022-08-15 18:59:08 +08:00
|
|
|
Seek(FramedClock.CurrentTime);
|
2019-03-06 19:30:14 +08:00
|
|
|
|
2022-08-15 18:59:08 +08:00
|
|
|
decoupledClock.Start();
|
2021-04-14 16:47:11 +08:00
|
|
|
}
|
2019-03-06 19:30:14 +08:00
|
|
|
|
2022-08-15 16:36:18 +08:00
|
|
|
isPaused.Value = false;
|
2021-04-14 16:47:11 +08:00
|
|
|
}
|
2019-03-25 19:25:47 +08:00
|
|
|
|
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)
|
|
|
|
{
|
2022-06-28 14:23:29 +08:00
|
|
|
Logger.Log($"{nameof(GameplayClockContainer)} seeking to {time}");
|
|
|
|
|
2022-08-15 18:59:08 +08:00
|
|
|
decoupledClock.Seek(time);
|
2021-04-20 16:35:59 +08:00
|
|
|
|
|
|
|
// Manually process to make sure the gameplay clock is correctly updated after a seek.
|
2022-08-15 18:59:08 +08:00
|
|
|
FramedClock.ProcessFrame();
|
2021-09-20 22:28:58 +08:00
|
|
|
|
|
|
|
OnSeek?.Invoke();
|
2021-04-20 16:35:59 +08:00
|
|
|
}
|
2020-12-10 16:42:47 +08:00
|
|
|
|
2021-04-16 19:33:29 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Stops gameplay.
|
|
|
|
/// </summary>
|
2022-08-15 16:36:18 +08:00
|
|
|
public void Stop() => isPaused.Value = true;
|
2020-12-10 16:42:47 +08:00
|
|
|
|
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>
|
2022-03-18 14:08:25 +08:00
|
|
|
/// <param name="startClock">Whether to start the clock immediately, if not already started.</param>
|
2022-04-13 12:49:58 +08:00
|
|
|
public void Reset(bool startClock = false)
|
2020-12-10 16:42:47 +08:00
|
|
|
{
|
2021-04-20 16:36:24 +08:00
|
|
|
// Manually stop the source in order to not affect the IsPaused state.
|
2022-08-15 18:59:08 +08:00
|
|
|
decoupledClock.Stop();
|
2021-04-14 16:47:11 +08:00
|
|
|
|
2022-03-18 14:08:25 +08:00
|
|
|
if (!IsPaused.Value || startClock)
|
2021-04-14 16:47:11 +08:00
|
|
|
Start();
|
2022-04-11 13:10:50 +08:00
|
|
|
|
|
|
|
ensureSourceClockSet();
|
|
|
|
Seek(StartTime ?? 0);
|
2019-03-06 19:30:14 +08:00
|
|
|
}
|
|
|
|
|
2021-04-19 18:55:59 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Changes the source clock.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sourceClock">The new source.</param>
|
2022-08-15 18:59:08 +08:00
|
|
|
protected void ChangeSource(IClock sourceClock) => decoupledClock.ChangeSource(SourceClock = sourceClock);
|
2021-04-19 18:55:59 +08:00
|
|
|
|
2021-04-24 03:56:08 +08:00
|
|
|
/// <summary>
|
2022-08-15 18:59:08 +08:00
|
|
|
/// Ensures that the <see cref="decoupledClock"/> is set to <see cref="SourceClock"/>, if it hasn't been given a source yet.
|
2021-04-24 03:56:08 +08:00
|
|
|
/// This is usually done before a seek to avoid accidentally seeking only the adjustable source in decoupled mode,
|
|
|
|
/// but not the actual source clock.
|
|
|
|
/// That will pretty much only happen on the very first call of this method, as the source clock is passed in the constructor,
|
|
|
|
/// but it is not yet set on the adjustable source there.
|
|
|
|
/// </summary>
|
2021-04-24 20:19:39 +08:00
|
|
|
private void ensureSourceClockSet()
|
|
|
|
{
|
2022-08-15 18:59:08 +08:00
|
|
|
if (decoupledClock.Source == null)
|
2021-04-24 20:19:39 +08:00
|
|
|
ChangeSource(SourceClock);
|
|
|
|
}
|
2021-04-24 03:56:08 +08:00
|
|
|
|
2021-04-14 20:15:14 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
if (!IsPaused.Value)
|
2022-08-15 18:59:08 +08:00
|
|
|
FramedClock.ProcessFrame();
|
2021-04-14 20:15:14 +08:00
|
|
|
|
|
|
|
base.Update();
|
|
|
|
}
|
|
|
|
|
2021-04-16 19:33:29 +08:00
|
|
|
/// <summary>
|
2022-08-15 18:59:08 +08:00
|
|
|
/// Invoked when the value of <see cref="IsPaused"/> is changed to start or stop the <see cref="decoupledClock"/> clock.
|
2021-04-16 19:33:29 +08:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="isPaused">Whether the clock should now be paused.</param>
|
|
|
|
protected virtual void OnIsPausedChanged(ValueChangedEvent<bool> isPaused)
|
|
|
|
{
|
|
|
|
if (isPaused.NewValue)
|
2022-08-15 18:59:08 +08:00
|
|
|
decoupledClock.Stop();
|
2021-04-16 19:33:29 +08:00
|
|
|
else
|
2022-08-15 18:59:08 +08:00
|
|
|
decoupledClock.Start();
|
2021-04-16 19:33:29 +08:00
|
|
|
}
|
2021-04-14 16:47:11 +08:00
|
|
|
|
2021-04-16 19:33:29 +08:00
|
|
|
/// <summary>
|
2022-08-15 18:59:08 +08:00
|
|
|
/// Creates the final <see cref="FramedClock"/> which is exposed via DI to be used by gameplay components.
|
2021-04-16 19:33:29 +08:00
|
|
|
/// </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>
|
2022-08-15 18:59:08 +08:00
|
|
|
/// <returns>The final <see cref="FramedClock"/>.</returns>
|
2022-08-15 18:46:29 +08:00
|
|
|
protected virtual IFrameBasedClock CreateGameplayClock(IFrameBasedClock source) => source;
|
2021-04-15 18:07:25 +08:00
|
|
|
|
|
|
|
#region IAdjustableClock
|
|
|
|
|
|
|
|
bool IAdjustableClock.Seek(double position)
|
|
|
|
{
|
|
|
|
Seek(position);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-21 16:13:01 +08:00
|
|
|
void IAdjustableClock.Reset() => Reset();
|
2021-04-15 18:07:25 +08:00
|
|
|
|
2022-08-15 16:57:26 +08:00
|
|
|
public void ResetSpeedAdjustments() => throw new NotImplementedException();
|
2021-04-15 18:07:25 +08:00
|
|
|
|
|
|
|
double IAdjustableClock.Rate
|
|
|
|
{
|
2022-08-15 18:59:08 +08:00
|
|
|
get => FramedClock.Rate;
|
2021-04-15 18:07:25 +08:00
|
|
|
set => throw new NotSupportedException();
|
|
|
|
}
|
|
|
|
|
2022-08-15 18:59:08 +08:00
|
|
|
public double Rate => FramedClock.Rate;
|
2021-04-15 18:07:25 +08:00
|
|
|
|
2022-08-15 18:59:08 +08:00
|
|
|
public double CurrentTime => FramedClock.CurrentTime;
|
2021-04-15 18:07:25 +08:00
|
|
|
|
2022-08-15 18:59:08 +08:00
|
|
|
public bool IsRunning => FramedClock.IsRunning;
|
2021-04-15 18:07:25 +08:00
|
|
|
|
|
|
|
#endregion
|
2022-08-15 16:06:24 +08:00
|
|
|
|
|
|
|
public void ProcessFrame()
|
|
|
|
{
|
|
|
|
// Handled via update. Don't process here to safeguard from external usages potentially processing frames additional times.
|
|
|
|
}
|
|
|
|
|
2022-08-15 18:59:08 +08:00
|
|
|
public double ElapsedFrameTime => FramedClock.ElapsedFrameTime;
|
2022-08-15 16:06:24 +08:00
|
|
|
|
2022-08-15 18:59:08 +08:00
|
|
|
public double FramesPerSecond => FramedClock.FramesPerSecond;
|
2022-08-15 16:06:24 +08:00
|
|
|
|
2022-08-15 18:59:08 +08:00
|
|
|
public FrameTimeInfo TimeInfo => FramedClock.TimeInfo;
|
2022-08-15 16:36:18 +08:00
|
|
|
|
2022-08-15 18:46:29 +08:00
|
|
|
public double TrueGameplayRate
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
double baseRate = Rate;
|
|
|
|
|
|
|
|
foreach (double adjustment in NonGameplayAdjustments)
|
|
|
|
{
|
|
|
|
if (Precision.AlmostEquals(adjustment, 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
baseRate /= adjustment;
|
|
|
|
}
|
|
|
|
|
|
|
|
return baseRate;
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 16:47:11 +08:00
|
|
|
}
|
2019-03-06 19:30:14 +08:00
|
|
|
}
|