2019-03-05 12:53:47 +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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2020-09-21 18:39:54 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2019-03-25 19:25:47 +08:00
|
|
|
using osu.Framework.Bindables;
|
2019-03-05 12:53:47 +08:00
|
|
|
using osu.Framework.Timing;
|
2020-10-04 20:51:27 +08:00
|
|
|
using osu.Framework.Utils;
|
2019-03-05 12:53:47 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A clock which is used for gameplay elements that need to follow audio time 1:1.
|
2019-04-25 16:36:17 +08:00
|
|
|
/// Exposed via DI by <see cref="GameplayClockContainer"/>.
|
2019-03-05 12:53:47 +08:00
|
|
|
/// <remarks>
|
2019-03-05 17:50:46 +08:00
|
|
|
/// The main purpose of this clock is to stop components using it from accidentally processing the main
|
2019-03-05 12:53:47 +08:00
|
|
|
/// <see cref="IFrameBasedClock"/>, as this should only be done once to ensure accuracy.
|
|
|
|
/// </remarks>
|
|
|
|
/// </summary>
|
2022-08-15 16:11:22 +08:00
|
|
|
public class GameplayClock : IGameplayClock
|
2019-03-05 12:53:47 +08:00
|
|
|
{
|
2021-04-16 19:15:42 +08:00
|
|
|
internal readonly IFrameBasedClock UnderlyingClock;
|
2019-03-05 12:53:47 +08:00
|
|
|
|
2019-03-25 19:25:47 +08:00
|
|
|
public readonly BindableBool IsPaused = new BindableBool();
|
|
|
|
|
2022-08-15 16:11:22 +08:00
|
|
|
IBindable<bool> IGameplayClock.IsPaused => IsPaused;
|
|
|
|
|
2022-08-15 17:53:10 +08:00
|
|
|
public virtual IEnumerable<double> NonGameplayAdjustments => Enumerable.Empty<double>();
|
2020-09-21 18:39:54 +08:00
|
|
|
|
2019-03-05 12:53:47 +08:00
|
|
|
public GameplayClock(IFrameBasedClock underlyingClock)
|
|
|
|
{
|
2021-04-16 19:15:42 +08:00
|
|
|
UnderlyingClock = underlyingClock;
|
2019-03-05 12:53:47 +08:00
|
|
|
}
|
|
|
|
|
2022-07-29 21:12:52 +08:00
|
|
|
public double? StartTime { get; internal set; }
|
|
|
|
|
2021-04-16 19:15:42 +08:00
|
|
|
public double CurrentTime => UnderlyingClock.CurrentTime;
|
2019-03-05 12:53:47 +08:00
|
|
|
|
2021-04-16 19:15:42 +08:00
|
|
|
public double Rate => UnderlyingClock.Rate;
|
2019-03-05 12:53:47 +08:00
|
|
|
|
2020-09-21 18:39:54 +08:00
|
|
|
public double TrueGameplayRate
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
double baseRate = Rate;
|
|
|
|
|
2022-08-15 17:53:10 +08:00
|
|
|
foreach (double adjustment in NonGameplayAdjustments)
|
2020-10-04 20:51:27 +08:00
|
|
|
{
|
2022-08-15 17:53:10 +08:00
|
|
|
if (Precision.AlmostEquals(adjustment, 0))
|
2020-10-04 20:51:27 +08:00
|
|
|
return 0;
|
|
|
|
|
2022-08-15 17:53:10 +08:00
|
|
|
baseRate /= adjustment;
|
2020-10-04 20:51:27 +08:00
|
|
|
}
|
2020-09-21 18:39:54 +08:00
|
|
|
|
|
|
|
return baseRate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-16 19:15:42 +08:00
|
|
|
public bool IsRunning => UnderlyingClock.IsRunning;
|
2019-03-05 12:53:47 +08:00
|
|
|
|
|
|
|
public void ProcessFrame()
|
|
|
|
{
|
2020-10-05 11:46:15 +08:00
|
|
|
// intentionally not updating the underlying clock (handled externally).
|
2019-03-05 12:53:47 +08:00
|
|
|
}
|
|
|
|
|
2021-04-16 19:15:42 +08:00
|
|
|
public double ElapsedFrameTime => UnderlyingClock.ElapsedFrameTime;
|
2019-03-05 12:53:47 +08:00
|
|
|
|
2021-04-16 19:15:42 +08:00
|
|
|
public double FramesPerSecond => UnderlyingClock.FramesPerSecond;
|
2019-03-05 12:53:47 +08:00
|
|
|
|
2021-04-16 19:15:42 +08:00
|
|
|
public FrameTimeInfo TimeInfo => UnderlyingClock.TimeInfo;
|
2019-09-17 21:33:27 +08:00
|
|
|
|
2021-04-16 19:15:42 +08:00
|
|
|
public IClock Source => UnderlyingClock;
|
2019-03-05 12:53:47 +08:00
|
|
|
}
|
2019-03-05 17:50:46 +08:00
|
|
|
}
|