1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 06:07:28 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/PlayerInstance.cs

132 lines
4.0 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 osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Bindables;
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
{
private const double catchup_rate = 2;
private const double max_sync_offset = 50;
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;
}
[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)));
}
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;
}
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;
}
}