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

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

235 lines
8.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 System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Logging;
using osu.Framework.Timing;
using osu.Framework.Utils;
using osu.Game.Beatmaps;
namespace osu.Game.Screens.Play
{
2021-04-16 19:33:29 +08:00
/// <summary>
/// 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>
[Cached(typeof(IGameplayClock))]
public class GameplayClockContainer : Container, IAdjustableClock, IGameplayClock
{
2021-04-16 19:33:29 +08:00
/// <summary>
/// Whether gameplay is paused.
/// </summary>
public IBindable<bool> IsPaused => isPaused;
2021-04-19 18:55:59 +08:00
/// <summary>
/// The source clock. Should generally not be used for any timekeeping purposes.
2021-04-19 18:55:59 +08:00
/// </summary>
public IClock SourceClock { get; private set; }
2021-04-19 18:55:59 +08:00
/// <summary>
/// Invoked when a seek has been performed via <see cref="Seek"/>
/// </summary>
public event Action? OnSeek;
/// <summary>
/// The time from which the clock should start. Will be seeked to on calling <see cref="Reset"/>.
/// Settting a start time will <see cref="Reset"/> to the new value.
/// </summary>
/// <remarks>
/// By default, a value of zero will be used.
/// Importantly, the value will be inferred from the current beatmap in <see cref="MasterGameplayClockContainer"/> by default.
/// </remarks>
public double StartTime
{
get => startTime;
set
{
startTime = value;
Reset();
}
}
private double startTime;
2022-07-29 21:12:52 +08:00
public virtual IEnumerable<double> NonGameplayAdjustments => Enumerable.Empty<double>();
private readonly BindableBool isPaused = new BindableBool(true);
/// <summary>
/// The adjustable source clock used for gameplay. Should be used for seeks and clock control.
/// This is the final clock exposed to gameplay components as an <see cref="IGameplayClock"/>.
/// </summary>
protected readonly FramedBeatmapClock GameplayClock;
protected override Container<Drawable> Content { get; } = new Container { RelativeSizeAxes = Axes.Both };
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>
/// <param name="applyOffsets">Whether to apply platform, user and beatmap offsets to the mix.</param>
public GameplayClockContainer(IClock sourceClock, bool applyOffsets = false)
{
2021-04-19 18:55:59 +08:00
SourceClock = sourceClock;
2021-04-14 16:47:11 +08:00
RelativeSizeAxes = Axes.Both;
InternalChildren = new Drawable[]
{
GameplayClock = new FramedBeatmapClock(sourceClock, applyOffsets) { IsCoupled = false },
Content
};
2022-07-29 21:12:52 +08:00
IsPaused.BindValueChanged(OnIsPausedChanged);
2021-04-14 16:47:11 +08:00
}
2021-04-16 19:33:29 +08:00
/// <summary>
/// Starts gameplay and marks un-paused state.
2021-04-16 19:33:29 +08:00
/// </summary>
2021-04-14 16:47:11 +08:00
public virtual void Start()
{
ensureSourceClockSet();
2021-04-20 12:56:13 +08:00
isPaused.Value = false;
// the clock may be stopped via internal means (ie. not via `IsPaused`).
if (!GameplayClock.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);
// Delay the start operation to ensure all children components get the initial seek time.
// Without this, children may get a start time beyond StartTime without seeing the time has elapsed.
// This can manifest as events which should be fired at the precise StartTime not firing.
SchedulerAfterChildren.Add(GameplayClock.Start);
2021-04-14 16:47:11 +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>
public void Seek(double time)
2021-04-20 16:35:59 +08:00
{
Logger.Log($"{nameof(GameplayClockContainer)} seeking to {time}");
GameplayClock.Seek(time);
2021-04-20 16:35:59 +08:00
OnSeek?.Invoke();
2021-04-20 16:35:59 +08:00
}
2021-04-16 19:33:29 +08:00
/// <summary>
/// Stops gameplay and marks paused state.
2021-04-16 19:33:29 +08:00
/// </summary>
public 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>
/// <param name="startClock">Whether to start the clock immediately, if not already started.</param>
public void Reset(bool startClock = false)
{
2021-04-20 16:36:24 +08:00
// Manually stop the source in order to not affect the IsPaused state.
GameplayClock.Stop();
2021-04-14 16:47:11 +08:00
ensureSourceClockSet();
Seek(StartTime);
if (!IsPaused.Value || startClock)
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) => GameplayClock.ChangeSource(SourceClock = sourceClock);
2021-04-19 18:55:59 +08:00
/// <summary>
/// Ensures that the <see cref="GameplayClock"/> is set to <see cref="SourceClock"/>, if it hasn't been given a source yet.
/// 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>
private void ensureSourceClockSet()
{
if (GameplayClock.Source == null)
ChangeSource(SourceClock);
}
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="GameplayClock"/> 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)
GameplayClock.Stop();
2021-04-16 19:33:29 +08:00
else
GameplayClock.Start();
2021-04-16 19:33:29 +08:00
}
2021-04-14 16:47:11 +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();
public void ResetSpeedAdjustments() => throw new NotImplementedException();
double IAdjustableClock.Rate
{
get => GameplayClock.Rate;
set => throw new NotSupportedException();
}
public double Rate => GameplayClock.Rate;
public double CurrentTime => GameplayClock.CurrentTime;
public bool IsRunning => GameplayClock.IsRunning;
#endregion
public void ProcessFrame()
{
// Handled via update. Don't process here to safeguard from external usages potentially processing frames additional times.
}
public double ElapsedFrameTime => GameplayClock.ElapsedFrameTime;
public double FramesPerSecond => GameplayClock.FramesPerSecond;
public FrameTimeInfo TimeInfo => GameplayClock.TimeInfo;
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
}
}