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

145 lines
5.2 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.
2021-04-16 12:28:32 +08:00
using System;
using System.Collections.Generic;
2021-04-22 22:52:22 +08:00
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Timing;
using osu.Game.Beatmaps;
2021-04-16 12:28:32 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Scoring;
using osu.Game.Screens.Play;
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
{
2021-04-22 22:52:22 +08:00
/// <summary>
/// Provides an area for and manages the hierarchy of a spectated player within a <see cref="MultiSpectatorScreen"/>.
/// </summary>
2021-04-26 18:01:30 +08:00
public class PlayerArea : CompositeDrawable
{
2021-04-22 22:52:22 +08:00
/// <summary>
/// Whether a <see cref="Player"/> is loaded in the area.
/// </summary>
2021-06-11 17:14:37 +08:00
public bool PlayerLoaded => (stack?.CurrentScreen as Player)?.IsLoaded == true;
2021-04-22 22:52:22 +08:00
/// <summary>
/// The user id this <see cref="PlayerArea"/> corresponds to.
/// </summary>
public readonly int UserId;
2021-04-22 22:52:22 +08:00
/// <summary>
/// The <see cref="ISpectatorPlayerClock"/> used to control the gameplay running state of a loaded <see cref="Player"/>.
2021-04-22 22:52:22 +08:00
/// </summary>
[NotNull]
public readonly ISpectatorPlayerClock GameplayClock = new CatchUpSpectatorPlayerClock();
2021-04-22 22:52:22 +08:00
/// <summary>
/// The currently-loaded score.
/// </summary>
[CanBeNull]
public Score Score { get; private set; }
[Resolved]
private BeatmapManager beatmapManager { get; set; }
2021-04-13 21:10:35 +08:00
2021-04-26 18:01:30 +08:00
private readonly BindableDouble volumeAdjustment = new BindableDouble();
private readonly Container gameplayContent;
2021-04-16 12:28:32 +08:00
private readonly LoadingLayer loadingLayer;
private OsuScreenStack stack;
public PlayerArea(int userId, IFrameBasedClock masterClock)
{
UserId = userId;
2021-04-08 21:13:54 +08:00
RelativeSizeAxes = Axes.Both;
Masking = true;
2021-04-16 12:28:32 +08:00
2021-04-26 18:01:30 +08:00
AudioContainer audioContainer;
2021-04-16 12:28:32 +08:00
InternalChildren = new Drawable[]
{
audioContainer = new AudioContainer
{
RelativeSizeAxes = Axes.Both,
Child = gameplayContent = new DrawSizePreservingFillContainer { RelativeSizeAxes = Axes.Both },
},
2021-04-16 12:28:32 +08:00
loadingLayer = new LoadingLayer(true) { State = { Value = Visibility.Visible } }
};
2021-04-26 18:01:30 +08:00
audioContainer.AddAdjustment(AdjustableProperty.Volume, volumeAdjustment);
GameplayClock.Source = masterClock;
}
2021-04-22 22:52:22 +08:00
public void LoadScore([NotNull] Score score)
{
2021-04-16 12:28:32 +08:00
if (Score != null)
2021-04-22 22:52:22 +08:00
throw new InvalidOperationException($"Cannot load a new score on a {nameof(PlayerArea)} that has an existing score.");
2021-04-16 12:28:32 +08:00
Score = score;
gameplayContent.Child = new PlayerIsolationContainer(beatmapManager.GetWorkingBeatmap(Score.ScoreInfo.BeatmapInfo), Score.ScoreInfo.Ruleset, Score.ScoreInfo.Mods)
{
RelativeSizeAxes = Axes.Both,
2021-04-16 12:28:32 +08:00
Child = stack = new OsuScreenStack()
};
stack.Push(new MultiSpectatorPlayerLoader(Score, () => new MultiSpectatorPlayer(Score, GameplayClock)));
2021-04-16 12:28:32 +08:00
loadingLayer.Hide();
}
2021-04-13 19:52:20 +08:00
2021-04-26 18:01:30 +08:00
private bool mute = true;
2021-04-26 18:01:30 +08:00
public bool Mute
{
2021-04-26 18:01:30 +08:00
get => mute;
set
{
mute = value;
volumeAdjustment.Value = value ? 0 : 1;
}
}
2021-04-26 18:01:30 +08:00
// Player interferes with global input, so disable input for now.
public override bool PropagatePositionalInputSubTree => false;
public override bool PropagateNonPositionalInputSubTree => false;
/// <summary>
/// Isolates each player instance from the game-wide ruleset/beatmap/mods (to allow for different players having different settings).
/// </summary>
private class PlayerIsolationContainer : Container
{
[Cached]
private readonly Bindable<RulesetInfo> ruleset = new Bindable<RulesetInfo>();
[Cached]
private readonly Bindable<WorkingBeatmap> beatmap = new Bindable<WorkingBeatmap>();
[Cached]
private readonly Bindable<IReadOnlyList<Mod>> mods = new Bindable<IReadOnlyList<Mod>>();
public PlayerIsolationContainer(WorkingBeatmap beatmap, RulesetInfo ruleset, IReadOnlyList<Mod> mods)
{
this.beatmap.Value = beatmap;
this.ruleset.Value = ruleset;
this.mods.Value = mods;
}
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
dependencies.CacheAs(ruleset.BeginLease(false));
dependencies.CacheAs(beatmap.BeginLease(false));
dependencies.CacheAs(mods.BeginLease(false));
return dependencies;
}
}
}
}