1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-24 14:47:24 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/Multiplayer/Spectate/MultiSpectatorScreen.cs

140 lines
5.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.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Online.Spectator;
using osu.Game.Screens.OnlinePlay.Multiplayer.Spectate.Sync;
using osu.Game.Screens.Play;
using osu.Game.Screens.Spectate;
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
{
2021-04-22 22:52:22 +08:00
/// <summary>
/// A <see cref="SpectatorScreen"/> that spectates multiple users in a match.
/// </summary>
2021-04-22 22:39:02 +08:00
public class MultiSpectatorScreen : SpectatorScreen
{
// Isolates beatmap/ruleset to this screen.
public override bool DisallowExternalBeatmapRulesetChanges => true;
2021-04-22 22:52:22 +08:00
/// <summary>
/// Whether all spectating players have finished loading.
/// </summary>
public bool AllPlayersLoaded => instances.All(p => p?.PlayerLoaded == true);
[Resolved]
private SpectatorStreamingClient spectatorClient { get; set; }
2021-04-22 22:52:22 +08:00
private readonly PlayerArea[] instances;
private MasterGameplayClockContainer masterClockContainer;
private ISyncManager syncManager;
private PlayerGrid grid;
2021-04-22 22:39:02 +08:00
private MultiSpectatorLeaderboard leaderboard;
2021-04-22 22:52:22 +08:00
private PlayerArea currentAudioSource;
2021-04-22 22:52:22 +08:00
/// <summary>
/// Creates a new <see cref="MultiSpectatorScreen"/>.
/// </summary>
/// <param name="userIds">The players to spectate.</param>
2021-04-22 22:39:02 +08:00
public MultiSpectatorScreen(int[] userIds)
2021-04-08 21:14:26 +08:00
: base(userIds.AsSpan().Slice(0, Math.Min(16, userIds.Length)).ToArray())
{
2021-04-22 22:52:22 +08:00
instances = new PlayerArea[UserIds.Length];
}
[BackgroundDependencyLoader]
private void load()
{
Container leaderboardContainer;
masterClockContainer = new MasterGameplayClockContainer(Beatmap.Value, 0);
InternalChildren = new[]
{
(Drawable)(syncManager = new CatchUpSyncManager(masterClockContainer)),
masterClockContainer.WithChild(new GridContainer
{
RelativeSizeAxes = Axes.Both,
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize)
},
Content = new[]
{
new Drawable[]
{
leaderboardContainer = new Container
{
RelativeSizeAxes = Axes.Y,
AutoSizeAxes = Axes.X
},
grid = new PlayerGrid { RelativeSizeAxes = Axes.Both }
}
}
})
};
for (int i = 0; i < UserIds.Length; i++)
2021-04-22 22:52:22 +08:00
grid.Add(instances[i] = new PlayerArea(UserIds[i], new CatchUpSlaveClock(masterClockContainer.GameplayClock)));
// Todo: This is not quite correct - it should be per-user to adjust for other mod combinations.
var playableBeatmap = Beatmap.Value.GetPlayableBeatmap(Ruleset.Value);
var scoreProcessor = Ruleset.Value.CreateInstance().CreateScoreProcessor();
scoreProcessor.ApplyBeatmap(playableBeatmap);
2021-04-22 22:39:02 +08:00
LoadComponentAsync(leaderboard = new MultiSpectatorLeaderboard(scoreProcessor, UserIds) { Expanded = { Value = true } }, leaderboardContainer.Add);
}
protected override void LoadComplete()
{
base.LoadComplete();
masterClockContainer.Stop();
2021-04-21 16:13:01 +08:00
masterClockContainer.Reset();
}
protected override void Update()
{
base.Update();
if (!isCandidateAudioSource(currentAudioSource?.GameplayClock))
{
currentAudioSource = instances.Where(i => isCandidateAudioSource(i.GameplayClock))
.OrderBy(i => Math.Abs(i.GameplayClock.CurrentTime - syncManager.Master.CurrentTime))
.FirstOrDefault();
foreach (var instance in instances)
instance.Volume.Value = instance == currentAudioSource ? 1 : 0;
}
}
private bool isCandidateAudioSource([CanBeNull] ISlaveClock clock)
=> clock?.IsRunning == true && !clock.IsCatchingUp && !clock.WaitingOnFrames.Value;
protected override void OnUserStateChanged(int userId, SpectatorState spectatorState)
{
}
protected override void StartGameplay(int userId, GameplayState gameplayState)
{
2021-04-16 12:28:32 +08:00
var instance = instances[getIndexForUser(userId)];
instance.LoadScore(gameplayState.Score);
syncManager.AddSlave(instance.GameplayClock);
leaderboard.AddClock(instance.UserId, instance.GameplayClock);
}
protected override void EndGameplay(int userId)
{
spectatorClient.StopWatchingUser(userId);
leaderboard.RemoveClock(userId);
}
2021-04-08 21:14:26 +08:00
private int getIndexForUser(int userId) => Array.IndexOf(UserIds, userId);
}
}