2021-04-15 18:12:52 +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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Timing;
|
2022-08-24 13:50:10 +08:00
|
|
|
using osu.Game.Screens.Play;
|
2021-04-15 18:12:52 +08:00
|
|
|
|
2021-05-03 12:38:53 +08:00
|
|
|
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
|
2021-04-15 18:12:52 +08:00
|
|
|
{
|
2021-04-15 18:35:57 +08:00
|
|
|
/// <summary>
|
2022-08-24 14:07:04 +08:00
|
|
|
/// A clock which catches up using rate adjustment.
|
2021-04-15 18:35:57 +08:00
|
|
|
/// </summary>
|
2022-08-24 14:07:04 +08:00
|
|
|
public class SpectatorPlayerClock : IFrameBasedClock, IAdjustableClock
|
2021-04-15 18:12:52 +08:00
|
|
|
{
|
|
|
|
/// <summary>
|
2021-04-15 18:35:57 +08:00
|
|
|
/// The catch up rate.
|
2021-04-15 18:12:52 +08:00
|
|
|
/// </summary>
|
2022-08-24 14:37:14 +08:00
|
|
|
private const double catchup_rate = 2;
|
2021-04-15 18:12:52 +08:00
|
|
|
|
2022-08-24 13:50:10 +08:00
|
|
|
private readonly GameplayClockContainer masterClock;
|
2021-04-15 18:12:52 +08:00
|
|
|
|
|
|
|
public double CurrentTime { get; private set; }
|
|
|
|
|
2022-08-24 14:17:56 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether this clock is waiting on frames to continue playback.
|
|
|
|
/// </summary>
|
|
|
|
public Bindable<bool> WaitingOnFrames { get; } = new Bindable<bool>(true);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether this clock is behind the master clock and running at a higher rate to catch up to it.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Of note, this will be false if this clock is *ahead* of the master clock.
|
|
|
|
/// </remarks>
|
|
|
|
public bool IsCatchingUp { get; set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether this spectator clock should be running.
|
|
|
|
/// Use instead of <see cref="Start"/> / <see cref="Stop"/> to control time.
|
|
|
|
/// </summary>
|
|
|
|
public bool IsRunning { get; set; }
|
2021-04-15 18:12:52 +08:00
|
|
|
|
2022-08-24 14:07:04 +08:00
|
|
|
public SpectatorPlayerClock(GameplayClockContainer masterClock)
|
2022-08-22 20:52:43 +08:00
|
|
|
{
|
2022-08-24 13:50:10 +08:00
|
|
|
this.masterClock = masterClock;
|
2022-08-22 20:52:43 +08:00
|
|
|
}
|
|
|
|
|
2021-04-15 18:12:52 +08:00
|
|
|
public void Reset() => CurrentTime = 0;
|
|
|
|
|
2022-08-24 14:17:56 +08:00
|
|
|
public void Start()
|
2022-05-13 05:07:11 +08:00
|
|
|
{
|
2022-08-24 14:17:56 +08:00
|
|
|
// Our running state should only be managed by SpectatorSyncManager via IsRunning.
|
2022-05-13 05:07:11 +08:00
|
|
|
}
|
|
|
|
|
2022-08-24 14:17:56 +08:00
|
|
|
public void Stop()
|
2022-05-13 05:07:11 +08:00
|
|
|
{
|
2022-08-24 14:17:56 +08:00
|
|
|
// Our running state should only be managed by an SpectatorSyncManager via IsRunning.
|
2022-05-13 05:07:11 +08:00
|
|
|
}
|
|
|
|
|
2021-06-11 15:23:59 +08:00
|
|
|
public bool Seek(double position)
|
|
|
|
{
|
|
|
|
CurrentTime = position;
|
|
|
|
return true;
|
|
|
|
}
|
2021-04-15 18:12:52 +08:00
|
|
|
|
|
|
|
public void ResetSpeedAdjustments()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-24 14:38:25 +08:00
|
|
|
public double Rate
|
2021-04-15 18:12:52 +08:00
|
|
|
{
|
2022-08-24 14:38:25 +08:00
|
|
|
get => IsCatchingUp ? catchup_rate : 1;
|
|
|
|
set => throw new NotImplementedException();
|
2021-04-15 18:12:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void ProcessFrame()
|
|
|
|
{
|
|
|
|
if (IsRunning)
|
|
|
|
{
|
2022-08-24 13:50:10 +08:00
|
|
|
double elapsedSource = masterClock.ElapsedFrameTime;
|
2021-04-15 18:12:52 +08:00
|
|
|
double elapsed = elapsedSource * Rate;
|
|
|
|
|
|
|
|
CurrentTime += elapsed;
|
|
|
|
ElapsedFrameTime = elapsed;
|
2022-08-24 13:50:10 +08:00
|
|
|
FramesPerSecond = masterClock.FramesPerSecond;
|
2021-04-15 18:12:52 +08:00
|
|
|
}
|
2022-08-24 14:37:14 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ElapsedFrameTime = 0;
|
|
|
|
FramesPerSecond = 0;
|
|
|
|
}
|
2021-04-15 18:12:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public double ElapsedFrameTime { get; private set; }
|
|
|
|
|
|
|
|
public double FramesPerSecond { get; private set; }
|
|
|
|
|
|
|
|
public FrameTimeInfo TimeInfo => new FrameTimeInfo { Elapsed = ElapsedFrameTime, Current = CurrentTime };
|
|
|
|
}
|
|
|
|
}
|