2021-04-08 21:07:00 +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 osu.Framework.Allocation;
|
2021-04-09 18:58:24 +08:00
|
|
|
using osu.Framework.Audio;
|
|
|
|
using osu.Framework.Bindables;
|
2021-04-08 21:07:00 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
using osu.Game.Scoring;
|
|
|
|
using osu.Game.Screens.Play;
|
|
|
|
using osu.Game.Users;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
|
|
|
|
{
|
|
|
|
public class PlayerInstance : CompositeDrawable
|
|
|
|
{
|
2021-04-09 18:58:24 +08:00
|
|
|
private const double catchup_rate = 2;
|
2021-04-09 21:01:21 +08:00
|
|
|
private const double max_sync_offset = 50;
|
2021-04-09 18:58:24 +08:00
|
|
|
|
2021-04-08 21:07:00 +08:00
|
|
|
public bool PlayerLoaded => stack?.CurrentScreen is Player;
|
|
|
|
|
|
|
|
public User User => Score.ScoreInfo.User;
|
|
|
|
|
|
|
|
public WorkingBeatmap Beatmap { get; private set; }
|
|
|
|
|
|
|
|
public readonly Score Score;
|
|
|
|
|
|
|
|
private OsuScreenStack stack;
|
|
|
|
private MultiplayerSpectatorPlayer player;
|
|
|
|
|
|
|
|
public PlayerInstance(Score score)
|
|
|
|
{
|
|
|
|
Score = score;
|
2021-04-08 21:13:54 +08:00
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
Masking = true;
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(BeatmapManager beatmapManager)
|
|
|
|
{
|
|
|
|
Beatmap = beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.Beatmap, bypassCache: true);
|
|
|
|
|
|
|
|
InternalChild = new GameplayIsolationContainer(Beatmap, Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods)
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Child = new DrawSizePreservingFillContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Child = stack = new OsuScreenStack()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
stack.Push(new MultiplayerSpectatorPlayerLoader(Score, () => player = new MultiplayerSpectatorPlayer(Score)));
|
|
|
|
}
|
2021-04-09 18:58:24 +08:00
|
|
|
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
{
|
|
|
|
base.UpdateAfterChildren();
|
|
|
|
updateCatchup();
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly BindableDouble catchupFrequencyAdjustment = new BindableDouble(catchup_rate);
|
|
|
|
private double targetTrackTime;
|
|
|
|
private bool isCatchingUp;
|
|
|
|
|
|
|
|
private void updateCatchup()
|
|
|
|
{
|
|
|
|
if (player?.IsLoaded != true)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Score.Replay.Frames.Count == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (player.GameplayClockContainer.IsPaused.Value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
double currentTime = Beatmap.Track.CurrentTime;
|
|
|
|
bool catchupRequired = targetTrackTime > currentTime + max_sync_offset;
|
|
|
|
|
|
|
|
// Skip catchup if nothing needs to be done.
|
|
|
|
if (catchupRequired == isCatchingUp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (catchupRequired)
|
|
|
|
Beatmap.Track.AddAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment);
|
|
|
|
else
|
|
|
|
Beatmap.Track.RemoveAdjustment(AdjustableProperty.Frequency, catchupFrequencyAdjustment);
|
2021-04-09 19:22:30 +08:00
|
|
|
|
|
|
|
isCatchingUp = catchupRequired;
|
2021-04-09 18:58:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public double GetCurrentGameplayTime()
|
|
|
|
{
|
|
|
|
if (player?.IsLoaded != true)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return player.GameplayClockContainer.GameplayClock.CurrentTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public double GetCurrentTrackTime()
|
|
|
|
{
|
|
|
|
if (player?.IsLoaded != true)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return Beatmap.Track.CurrentTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ContinueGameplay(double targetTrackTime)
|
|
|
|
{
|
|
|
|
if (player?.IsLoaded != true)
|
|
|
|
return;
|
|
|
|
|
|
|
|
player.GameplayClockContainer.Start();
|
|
|
|
this.targetTrackTime = targetTrackTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PauseGameplay()
|
|
|
|
{
|
|
|
|
if (player?.IsLoaded != true)
|
|
|
|
return;
|
|
|
|
|
|
|
|
player.GameplayClockContainer.Stop();
|
|
|
|
}
|
2021-04-13 19:52:20 +08:00
|
|
|
|
|
|
|
// Player interferes with global input, so disable input for now.
|
|
|
|
public override bool PropagatePositionalInputSubTree => false;
|
|
|
|
public override bool PropagateNonPositionalInputSubTree => false;
|
2021-04-08 21:07:00 +08:00
|
|
|
}
|
|
|
|
}
|