1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 05:52:55 +08:00
osu-lazer/osu.Game/Screens/Play/Player.cs

484 lines
16 KiB
C#
Raw Normal View History

2019-03-18 10:48:11 +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.
2018-04-13 17:19:50 +08:00
using System;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Framework.Logging;
using osu.Framework.Screens;
using osu.Framework.Threading;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
2019-01-04 12:29:37 +08:00
using osu.Game.Graphics.Containers;
2018-04-13 17:19:50 +08:00
using osu.Game.Online.API;
2018-06-06 14:10:09 +08:00
using osu.Game.Overlays;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Scoring;
using osu.Game.Rulesets.UI;
2018-11-28 15:12:57 +08:00
using osu.Game.Scoring;
2018-12-22 15:26:27 +08:00
using osu.Game.Screens.Ranking;
2018-04-13 17:19:50 +08:00
using osu.Game.Skinning;
using osu.Game.Storyboards.Drawables;
namespace osu.Game.Screens.Play
{
public class Player : ScreenWithBeatmapBackground
2018-04-13 17:19:50 +08:00
{
protected override bool AllowBackButton => false; // handled by HoldForMenuButton
2019-01-23 19:52:00 +08:00
public override float BackgroundParallaxAmount => 0.1f;
2018-04-13 17:19:50 +08:00
public override bool HideOverlaysOnEnter => true;
2018-04-13 17:19:50 +08:00
public override OverlayActivation InitialOverlayActivationMode => OverlayActivation.UserTriggered;
2018-06-06 14:10:09 +08:00
2018-04-13 17:19:50 +08:00
public Action RestartRequested;
public bool HasFailed { get; private set; }
public bool AllowPause { get; set; } = true;
public bool AllowLeadIn { get; set; } = true;
public bool AllowResults { get; set; } = true;
private Bindable<bool> mouseWheelDisabled;
2019-02-25 12:27:44 +08:00
private readonly Bindable<bool> storyboardReplacesBackground = new Bindable<bool>();
2018-04-13 17:19:50 +08:00
public int RestartCount;
2018-11-29 13:56:29 +08:00
[Resolved]
private ScoreManager scoreManager { get; set; }
2018-04-13 17:19:50 +08:00
private RulesetInfo ruleset;
private IAPIProvider api;
2018-04-13 17:19:50 +08:00
private SampleChannel sampleRestart;
protected ScoreProcessor ScoreProcessor { get; private set; }
protected DrawableRuleset DrawableRuleset { get; private set; }
2018-04-13 17:19:50 +08:00
protected HUDOverlay HUDOverlay { get; private set; }
2018-04-13 17:19:50 +08:00
public bool LoadedBeatmapSuccessfully => DrawableRuleset?.Objects.Any() == true;
2018-04-13 17:19:50 +08:00
2019-03-17 23:46:15 +08:00
protected GameplayClockContainer GameplayClockContainer { get; private set; }
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load(AudioManager audio, IAPIProvider api, OsuConfigManager config)
2018-04-13 17:19:50 +08:00
{
this.api = api;
WorkingBeatmap working = loadBeatmap();
if (working == null)
2018-04-13 17:19:50 +08:00
return;
2018-08-31 06:04:40 +08:00
sampleRestart = audio.Sample.Get(@"Gameplay/restart");
2018-04-13 17:19:50 +08:00
mouseWheelDisabled = config.GetBindable<bool>(OsuSetting.MouseDisableWheel);
2019-03-19 12:13:19 +08:00
showStoryboard = config.GetBindable<bool>(OsuSetting.ShowStoryboard);
2018-04-13 17:19:50 +08:00
ScoreProcessor = DrawableRuleset.CreateScoreProcessor();
if (!ScoreProcessor.Mode.Disabled)
config.BindWith(OsuSetting.ScoreDisplayMode, ScoreProcessor.Mode);
2018-04-13 17:19:50 +08:00
InternalChild = GameplayClockContainer = new GameplayClockContainer(working, AllowLeadIn, DrawableRuleset.GameplayStartTime);
2019-03-18 10:48:11 +08:00
GameplayClockContainer.Children = new[]
2018-04-13 17:19:50 +08:00
{
2019-03-18 10:48:11 +08:00
StoryboardContainer = CreateStoryboardContainer(),
new ScalingContainer(ScalingMode.Gameplay)
2018-04-13 17:19:50 +08:00
{
2019-03-18 10:48:11 +08:00
Child = new LocalSkinOverrideContainer(working.Skin)
{
2019-03-18 10:48:11 +08:00
RelativeSizeAxes = Axes.Both,
Child = DrawableRuleset
2018-04-13 17:19:50 +08:00
}
},
2019-03-18 10:48:11 +08:00
new BreakOverlay(working.Beatmap.BeatmapInfo.LetterboxInBreaks, ScoreProcessor)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Breaks = working.Beatmap.Breaks
},
// display the cursor above some HUD elements.
DrawableRuleset.Cursor?.CreateProxy() ?? new Container(),
HUDOverlay = new HUDOverlay(ScoreProcessor, DrawableRuleset, working)
2019-03-18 10:48:11 +08:00
{
HoldToQuit = { Action = performUserRequestedExit },
PlayerSettingsOverlay = { PlaybackSettings = { UserPlaybackRate = { BindTarget = GameplayClockContainer.UserPlaybackRate } } },
KeyCounter = { Visible = { BindTarget = DrawableRuleset.HasReplayLoaded } },
2019-03-18 10:48:11 +08:00
RequestSeek = GameplayClockContainer.Seek,
Anchor = Anchor.Centre,
Origin = Anchor.Centre
},
new SkipOverlay(DrawableRuleset.GameplayStartTime)
2019-03-18 10:48:11 +08:00
{
RequestSeek = GameplayClockContainer.Seek
},
FailOverlay = new FailOverlay
{
2019-03-18 13:57:06 +08:00
OnRetry = Restart,
2019-03-18 10:48:11 +08:00
OnQuit = performUserRequestedExit,
},
PauseOverlay = new PauseOverlay
2018-04-13 17:19:50 +08:00
{
2019-03-18 10:48:11 +08:00
OnResume = Resume,
Retries = RestartCount,
2019-03-18 13:57:06 +08:00
OnRetry = Restart,
OnQuit = performUserRequestedExit,
2018-04-13 17:19:50 +08:00
},
new HotkeyRetryOverlay
{
Action = () =>
{
2019-01-23 19:52:00 +08:00
if (!this.IsCurrentScreen()) return;
2018-04-13 17:19:50 +08:00
fadeOut(true);
2019-03-18 13:57:06 +08:00
Restart();
2018-04-13 17:19:50 +08:00
},
}
};
// bind clock into components that require it
DrawableRuleset.IsPaused.BindTo(GameplayClockContainer.IsPaused);
// load storyboard as part of player's load if we can
2019-03-18 10:48:11 +08:00
initializeStoryboard(false);
2018-04-13 17:19:50 +08:00
// Bind ScoreProcessor to ourselves
ScoreProcessor.AllJudged += onCompletion;
ScoreProcessor.Failed += onFail;
2018-04-13 17:19:50 +08:00
foreach (var mod in Beatmap.Value.Mods.Value.OfType<IApplicableToScoreProcessor>())
mod.ApplyToScoreProcessor(ScoreProcessor);
2018-04-13 17:19:50 +08:00
}
private WorkingBeatmap loadBeatmap()
2018-04-13 17:19:50 +08:00
{
WorkingBeatmap working = Beatmap.Value;
if (working is DummyWorkingBeatmap)
return null;
try
{
var beatmap = working.Beatmap;
if (beatmap == null)
throw new InvalidOperationException("Beatmap was not loaded");
2018-04-13 17:19:50 +08:00
ruleset = Ruleset.Value ?? beatmap.BeatmapInfo.Ruleset;
var rulesetInstance = ruleset.CreateInstance();
try
{
DrawableRuleset = rulesetInstance.CreateDrawableRulesetWith(working);
}
catch (BeatmapInvalidForRulesetException)
{
// we may fail to create a DrawableRuleset if the beatmap cannot be loaded with the user's preferred ruleset
// let's try again forcing the beatmap's ruleset.
ruleset = beatmap.BeatmapInfo.Ruleset;
rulesetInstance = ruleset.CreateInstance();
DrawableRuleset = rulesetInstance.CreateDrawableRulesetWith(Beatmap.Value);
}
if (!DrawableRuleset.Objects.Any())
{
Logger.Log("Beatmap contains no hit objects!", level: LogLevel.Error);
return null;
}
}
catch (Exception e)
{
Logger.Error(e, "Could not load beatmap sucessfully!");
//couldn't load, hard abort!
return null;
}
return working;
2018-04-13 17:19:50 +08:00
}
private void performUserRequestedExit()
{
2019-01-23 19:52:00 +08:00
if (!this.IsCurrentScreen()) return;
2019-02-28 12:31:40 +08:00
2019-01-23 19:52:00 +08:00
this.Exit();
}
2019-03-18 13:57:06 +08:00
public void Restart()
2018-04-13 17:19:50 +08:00
{
2019-01-23 19:52:00 +08:00
if (!this.IsCurrentScreen()) return;
2018-04-13 17:19:50 +08:00
sampleRestart?.Play();
ValidForResume = false;
RestartRequested?.Invoke();
2019-01-23 19:52:00 +08:00
this.Exit();
2018-04-13 17:19:50 +08:00
}
private ScheduledDelegate onCompletionEvent;
private void onCompletion()
{
// Only show the completion screen if the player hasn't failed
if (ScoreProcessor.HasFailed || onCompletionEvent != null)
2018-04-13 17:19:50 +08:00
return;
ValidForResume = false;
if (!AllowResults) return;
using (BeginDelayedSequence(1000))
{
onCompletionEvent = Schedule(delegate
{
2019-01-23 19:52:00 +08:00
if (!this.IsCurrentScreen()) return;
2018-04-13 17:19:50 +08:00
2018-11-30 17:32:08 +08:00
var score = CreateScore();
if (DrawableRuleset.ReplayScore == null)
scoreManager.Import(score);
2018-11-29 13:56:29 +08:00
this.Push(CreateResults(score));
onCompletionEvent = null;
2018-04-13 17:19:50 +08:00
});
}
}
2018-11-30 17:32:08 +08:00
protected virtual ScoreInfo CreateScore()
2018-11-29 12:22:45 +08:00
{
var score = DrawableRuleset.ReplayScore?.ScoreInfo ?? new ScoreInfo
2018-11-29 12:22:45 +08:00
{
2018-11-30 17:31:54 +08:00
Beatmap = Beatmap.Value.BeatmapInfo,
2018-11-29 12:22:45 +08:00
Ruleset = ruleset,
2018-12-27 21:16:58 +08:00
Mods = Beatmap.Value.Mods.Value.ToArray(),
User = api.LocalUser.Value,
2018-11-29 12:22:45 +08:00
};
ScoreProcessor.PopulateScore(score);
return score;
}
2019-03-18 10:48:11 +08:00
protected override bool OnScroll(ScrollEvent e) => mouseWheelDisabled.Value && !GameplayClockContainer.IsPaused.Value;
protected virtual Results CreateResults(ScoreInfo score) => new SoloResults(score);
2019-03-22 13:39:20 +08:00
#region Storyboard
private DrawableStoryboard storyboard;
protected UserDimContainer StoryboardContainer { get; private set; }
protected virtual UserDimContainer CreateStoryboardContainer() => new UserDimContainer(true)
{
RelativeSizeAxes = Axes.Both,
Alpha = 1,
EnableUserDim = { Value = true }
};
private Bindable<bool> showStoryboard;
private void initializeStoryboard(bool asyncLoad)
{
if (StoryboardContainer == null || storyboard != null)
return;
if (!showStoryboard.Value)
return;
var beatmap = Beatmap.Value;
storyboard = beatmap.Storyboard.CreateDrawable();
storyboard.Masking = true;
if (asyncLoad)
LoadComponentAsync(storyboard, StoryboardContainer.Add);
else
StoryboardContainer.Add(storyboard);
}
#endregion
2019-03-18 10:48:11 +08:00
#region Fail Logic
protected FailOverlay FailOverlay { get; private set; }
2018-04-13 17:19:50 +08:00
private bool onFail()
{
if (Beatmap.Value.Mods.Value.OfType<IApplicableFailOverride>().Any(m => !m.AllowFail))
return false;
2019-03-17 23:46:15 +08:00
GameplayClockContainer.Stop();
2018-04-13 17:19:50 +08:00
HasFailed = true;
2019-03-18 10:48:11 +08:00
// There is a chance that we could be in a paused state as the ruleset's internal clock (see FrameStabilityContainer)
// could process an extra frame after the GameplayClock is stopped.
// In such cases we want the fail state to precede a user triggered pause.
if (PauseOverlay.State == Visibility.Visible)
PauseOverlay.Hide();
FailOverlay.Retries = RestartCount;
FailOverlay.Show();
2018-04-13 17:19:50 +08:00
return true;
}
2019-03-18 10:48:11 +08:00
#endregion
#region Pause Logic
public bool IsResuming { get; private set; }
/// <summary>
/// The amount of gameplay time after which a second pause is allowed.
/// </summary>
private const double pause_cooldown = 1000;
protected PauseOverlay PauseOverlay { get; private set; }
private double? lastPauseActionTime;
private bool canPause =>
// must pass basic screen conditions (beatmap loaded, instance allows pause)
LoadedBeatmapSuccessfully && AllowPause && ValidForResume
// replays cannot be paused and exit immediately
&& !DrawableRuleset.HasReplayLoaded.Value
2019-03-18 10:48:11 +08:00
// cannot pause if we are already in a fail state
&& !HasFailed
// cannot pause if already paused (or in a cooldown state) unless we are in a resuming state.
&& (IsResuming || (GameplayClockContainer.IsPaused.Value == false && !pauseCooldownActive));
private bool pauseCooldownActive =>
lastPauseActionTime.HasValue && GameplayClockContainer.GameplayClock.CurrentTime < lastPauseActionTime + pause_cooldown;
2019-03-18 10:48:11 +08:00
private bool canResume =>
// cannot resume from a non-paused state
GameplayClockContainer.IsPaused.Value
// cannot resume if we are already in a fail state
&& !HasFailed
// already resuming
&& !IsResuming;
protected override void Update()
{
base.Update();
// eagerly pause when we lose window focus (if we are locally playing).
if (!Game.IsActive.Value)
Pause();
}
public void Pause()
{
if (!canPause) return;
IsResuming = false;
2019-03-18 10:48:11 +08:00
GameplayClockContainer.Stop();
PauseOverlay.Show();
lastPauseActionTime = GameplayClockContainer.GameplayClock.CurrentTime;
}
public void Resume()
{
if (!canResume) return;
IsResuming = true;
PauseOverlay.Hide();
// time-based conditions may allow instant resume.
if (GameplayClockContainer.GameplayClock.CurrentTime < Beatmap.Value.Beatmap.HitObjects.First().StartTime)
completeResume();
else
DrawableRuleset.RequestResume(completeResume);
void completeResume()
{
GameplayClockContainer.Start();
IsResuming = false;
}
2019-03-18 10:48:11 +08:00
}
#endregion
#region Screen Logic
2019-01-23 19:52:00 +08:00
public override void OnEntering(IScreen last)
2018-04-13 17:19:50 +08:00
{
base.OnEntering(last);
if (!LoadedBeatmapSuccessfully)
2018-04-13 17:19:50 +08:00
return;
2019-01-23 19:52:00 +08:00
Alpha = 0;
this
2018-04-13 17:19:50 +08:00
.ScaleTo(0.7f)
.ScaleTo(1, 750, Easing.OutQuint)
.Delay(250)
.FadeIn(250);
showStoryboard.ValueChanged += _ => initializeStoryboard(true);
Background.EnableUserDim.Value = true;
Background.BlurAmount.Value = 0;
Background.StoryboardReplacesBackground.BindTo(storyboardReplacesBackground);
StoryboardContainer.StoryboardReplacesBackground.BindTo(storyboardReplacesBackground);
2019-02-28 19:30:23 +08:00
storyboardReplacesBackground.Value = Beatmap.Value.Storyboard.ReplacesBackground && Beatmap.Value.Storyboard.HasDrawable;
2019-03-17 23:46:15 +08:00
GameplayClockContainer.Restart();
2019-03-18 10:48:11 +08:00
GameplayClockContainer.FadeInFromZero(750, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
}
2019-01-23 19:52:00 +08:00
public override void OnSuspending(IScreen next)
2018-04-13 17:19:50 +08:00
{
fadeOut();
base.OnSuspending(next);
}
2019-01-23 19:52:00 +08:00
public override bool OnExiting(IScreen next)
2018-04-13 17:19:50 +08:00
{
if (onCompletionEvent != null)
{
// Proceed to result screen if beatmap already finished playing
onCompletionEvent.RunTask();
return true;
}
if (canPause)
2018-04-13 17:19:50 +08:00
{
2019-03-18 10:48:11 +08:00
Pause();
return true;
2018-04-13 17:19:50 +08:00
}
if (pauseCooldownActive && !GameplayClockContainer.IsPaused.Value)
// still want to block if we are within the cooldown period and not already paused.
return true;
2019-03-17 23:46:15 +08:00
GameplayClockContainer.ResetLocalAdjustments();
2019-02-15 15:17:01 +08:00
fadeOut();
return base.OnExiting(next);
2018-04-13 17:19:50 +08:00
}
private void fadeOut(bool instant = false)
2018-04-13 17:19:50 +08:00
{
float fadeOutDuration = instant ? 0 : 250;
2019-01-23 19:52:00 +08:00
this.FadeOut(fadeOutDuration);
2019-02-28 19:30:23 +08:00
Background.EnableUserDim.Value = false;
storyboardReplacesBackground.Value = false;
2018-04-13 17:19:50 +08:00
}
2019-03-18 10:48:11 +08:00
#endregion
2018-04-13 17:19:50 +08:00
}
}