1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 09:22:54 +08:00
osu-lazer/osu.Game/Screens/Play/Player.cs

437 lines
15 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Linq;
using System.Threading.Tasks;
using osu.Framework;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
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.Framework.Timing;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Graphics.Cursor;
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-04-13 17:19:50 +08:00
using osu.Game.Screens.Ranking;
using osu.Game.Skinning;
using osu.Game.Storyboards.Drawables;
namespace osu.Game.Screens.Play
{
public class Player : ScreenWithBeatmapBackground, IProvideCursor
{
protected override bool AllowBackButton => false; // handled by HoldForMenuButton
2018-04-13 17:19:50 +08:00
protected override float BackgroundParallaxAmount => 0.1f;
protected override bool HideOverlaysOnEnter => true;
2018-04-13 17:19:50 +08:00
2018-06-06 15:17:51 +08:00
protected 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;
private Bindable<double> userAudioOffset;
public int RestartCount;
public CursorContainer Cursor => RulesetContainer.Cursor;
public bool ProvidingUserCursor => RulesetContainer?.Cursor != null && !RulesetContainer.HasReplayLoaded.Value;
private IAdjustableClock sourceClock;
/// <summary>
/// The decoupled clock used for gameplay. Should be used for seeks and clock control.
/// </summary>
private DecoupleableInterpolatingFramedClock adjustableClock;
2018-11-29 13:56:29 +08:00
[Resolved]
private ScoreManager scoreManager { get; set; }
2018-04-13 17:19:50 +08:00
private PauseContainer pauseContainer;
private RulesetInfo ruleset;
private APIAccess api;
private SampleChannel sampleRestart;
protected ScoreProcessor ScoreProcessor;
2018-04-13 17:19:50 +08:00
protected RulesetContainer RulesetContainer;
private HUDOverlay hudOverlay;
private FailOverlay failOverlay;
private DrawableStoryboard storyboard;
private Container storyboardContainer;
public bool LoadedBeatmapSuccessfully => RulesetContainer?.Objects.Any() == true;
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
2018-08-31 06:04:40 +08:00
private void load(AudioManager audio, APIAccess api, OsuConfigManager config)
2018-04-13 17:19:50 +08:00
{
this.api = api;
WorkingBeatmap working = Beatmap.Value;
if (working is DummyWorkingBeatmap)
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);
userAudioOffset = config.GetBindable<double>(OsuSetting.AudioOffset);
2018-04-19 19:44:38 +08:00
IBeatmap beatmap;
2018-04-13 17:19:50 +08:00
try
{
beatmap = working.Beatmap;
if (beatmap == null)
throw new InvalidOperationException("Beatmap was not loaded");
ruleset = Ruleset.Value ?? beatmap.BeatmapInfo.Ruleset;
var rulesetInstance = ruleset.CreateInstance();
try
{
RulesetContainer = rulesetInstance.CreateRulesetContainerWith(working);
2018-04-13 17:19:50 +08:00
}
catch (BeatmapInvalidForRulesetException)
{
// we may fail to create a RulesetContainer 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();
RulesetContainer = rulesetInstance.CreateRulesetContainerWith(Beatmap.Value);
2018-04-13 17:19:50 +08:00
}
if (!RulesetContainer.Objects.Any())
{
Logger.Log("Beatmap contains no hit objects!", level: LogLevel.Error);
return;
}
2018-04-13 17:19:50 +08:00
}
catch (Exception e)
{
Logger.Error(e, "Could not load beatmap sucessfully!");
//couldn't load, hard abort!
return;
}
sourceClock = (IAdjustableClock)working.Track ?? new StopwatchClock();
adjustableClock = new DecoupleableInterpolatingFramedClock { IsCoupled = false };
adjustableClock.Seek(AllowLeadIn
2018-07-23 14:33:47 +08:00
? Math.Min(0, RulesetContainer.GameplayStartTime - beatmap.BeatmapInfo.AudioLeadIn)
: RulesetContainer.GameplayStartTime);
2018-04-13 17:19:50 +08:00
adjustableClock.ProcessFrame();
// Lazer's audio timings in general doesn't match stable. This is the result of user testing, albeit limited.
// This only seems to be required on windows. We need to eventually figure out why, with a bit of luck.
var platformOffsetClock = new FramedOffsetClock(adjustableClock) { Offset = RuntimeInfo.OS == RuntimeInfo.Platform.Windows ? 22 : 0 };
2018-04-13 17:19:50 +08:00
// the final usable gameplay clock with user-set offsets applied.
var offsetClock = new FramedOffsetClock(platformOffsetClock);
2018-04-13 17:19:50 +08:00
userAudioOffset.ValueChanged += v => offsetClock.Offset = v;
userAudioOffset.TriggerChange();
ScoreProcessor = RulesetContainer.CreateScoreProcessor();
if (!ScoreProcessor.Mode.Disabled)
config.BindWith(OsuSetting.ScoreDisplayMode, ScoreProcessor.Mode);
2018-04-13 17:19:50 +08:00
Children = new Drawable[]
{
pauseContainer = new PauseContainer(offsetClock, adjustableClock)
{
2018-07-18 21:05:24 +08:00
Retries = RestartCount,
2018-04-13 17:19:50 +08:00
OnRetry = Restart,
OnQuit = performUserRequestedExit,
2018-04-13 17:19:50 +08:00
CheckCanPause = () => AllowPause && ValidForResume && !HasFailed && !RulesetContainer.HasReplayLoaded,
Children = new[]
2018-04-13 17:19:50 +08:00
{
storyboardContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
},
new LocalSkinOverrideContainer(working.Skin)
{
RelativeSizeAxes = Axes.Both,
Child = RulesetContainer
},
new BreakOverlay(beatmap.BeatmapInfo.LetterboxInBreaks, ScoreProcessor)
2018-04-13 17:19:50 +08:00
{
2018-05-14 14:36:42 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2018-04-13 17:19:50 +08:00
ProcessCustomClock = false,
2018-05-14 14:36:42 +08:00
Breaks = beatmap.Breaks
2018-04-13 17:19:50 +08:00
},
RulesetContainer.Cursor?.CreateProxy() ?? new Container(),
hudOverlay = new HUDOverlay(ScoreProcessor, RulesetContainer, working, offsetClock, adjustableClock)
2018-04-13 17:19:50 +08:00
{
Clock = Clock, // hud overlay doesn't want to use the audio clock directly
ProcessCustomClock = false,
Anchor = Anchor.Centre,
Origin = Anchor.Centre
},
new SkipOverlay(RulesetContainer.GameplayStartTime)
2018-04-13 17:19:50 +08:00
{
Clock = Clock, // skip button doesn't want to use the audio clock directly
ProcessCustomClock = false,
AdjustableClock = adjustableClock,
FramedClock = offsetClock,
},
}
},
failOverlay = new FailOverlay
{
OnRetry = Restart,
OnQuit = performUserRequestedExit,
2018-04-13 17:19:50 +08:00
},
new HotkeyRetryOverlay
{
Action = () =>
{
if (!IsCurrentScreen) return;
fadeOut(true);
2018-04-13 17:19:50 +08:00
Restart();
},
}
};
hudOverlay.HoldToQuit.Action = performUserRequestedExit;
2018-06-13 10:30:22 +08:00
hudOverlay.KeyCounter.Visible.BindTo(RulesetContainer.HasReplayLoaded);
2018-07-11 16:01:27 +08:00
RulesetContainer.IsPaused.BindTo(pauseContainer.IsPaused);
2018-07-19 00:18:07 +08:00
2018-04-13 17:19:50 +08:00
if (ShowStoryboard)
initializeStoryboard(false);
// 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 void applyRateFromMods()
{
if (sourceClock == null) return;
sourceClock.Rate = 1;
foreach (var mod in Beatmap.Value.Mods.Value.OfType<IApplicableToClock>())
mod.ApplyToClock(sourceClock);
}
private void performUserRequestedExit()
{
if (!IsCurrentScreen) return;
Exit();
}
2018-04-13 17:19:50 +08:00
public void Restart()
{
if (!IsCurrentScreen) return;
2018-04-13 17:19:50 +08:00
sampleRestart?.Play();
ValidForResume = false;
RestartRequested?.Invoke();
Exit();
}
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
{
if (!IsCurrentScreen) return;
2018-11-30 17:32:08 +08:00
var score = CreateScore();
2018-11-29 13:56:29 +08:00
if (RulesetContainer.Replay == null)
2018-11-29 17:07:51 +08:00
scoreManager.Import(score, true);
2018-11-29 13:56:29 +08:00
Push(new Results(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 = new ScoreInfo
{
2018-11-30 17:31:54 +08:00
Beatmap = Beatmap.Value.BeatmapInfo,
2018-11-29 12:22:45 +08:00
Ruleset = ruleset,
User = api.LocalUser.Value
};
ScoreProcessor.PopulateScore(score);
return score;
}
2018-04-13 17:19:50 +08:00
private bool onFail()
{
if (Beatmap.Value.Mods.Value.OfType<IApplicableFailOverride>().Any(m => !m.AllowFail))
return false;
adjustableClock.Stop();
HasFailed = true;
failOverlay.Retries = RestartCount;
failOverlay.Show();
return true;
}
protected override void OnEntering(Screen last)
{
base.OnEntering(last);
if (!LoadedBeatmapSuccessfully)
2018-04-13 17:19:50 +08:00
return;
Content.Alpha = 0;
Content
.ScaleTo(0.7f)
.ScaleTo(1, 750, Easing.OutQuint)
.Delay(250)
.FadeIn(250);
Task.Run(() =>
{
sourceClock.Reset();
Schedule(() =>
{
adjustableClock.ChangeSource(sourceClock);
applyRateFromMods();
this.Delay(750).Schedule(() =>
{
if (!pauseContainer.IsPaused)
{
adjustableClock.Start();
}
});
});
});
pauseContainer.Alpha = 0;
pauseContainer.FadeIn(750, Easing.OutQuint);
}
protected override void OnSuspending(Screen next)
{
fadeOut();
base.OnSuspending(next);
}
protected override bool OnExiting(Screen next)
{
if (onCompletionEvent != null)
{
// Proceed to result screen if beatmap already finished playing
onCompletionEvent.RunTask();
return true;
}
2018-04-13 17:19:50 +08:00
if ((!AllowPause || HasFailed || !ValidForResume || pauseContainer?.IsPaused != false || RulesetContainer?.HasReplayLoaded != false) && (!pauseContainer?.IsResuming ?? true))
{
// In the case of replays, we may have changed the playback rate.
applyRateFromMods();
fadeOut();
return base.OnExiting(next);
}
if (LoadedBeatmapSuccessfully)
2018-04-13 17:19:50 +08:00
pauseContainer?.Pause();
return true;
}
private void fadeOut(bool instant = false)
2018-04-13 17:19:50 +08:00
{
float fadeOutDuration = instant ? 0 : 250;
Content.FadeOut(fadeOutDuration);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnScroll(ScrollEvent e) => mouseWheelDisabled.Value && !pauseContainer.IsPaused;
2018-04-13 17:19:50 +08:00
private void initializeStoryboard(bool asyncLoad)
{
if (storyboardContainer == null)
return;
var beatmap = Beatmap.Value;
storyboard = beatmap.Storyboard.CreateDrawable();
storyboard.Masking = true;
if (asyncLoad)
LoadComponentAsync(storyboard, storyboardContainer.Add);
else
storyboardContainer.Add(storyboard);
}
protected override void UpdateBackgroundElements()
{
if (!IsCurrentScreen) return;
base.UpdateBackgroundElements();
if (ShowStoryboard && storyboard == null)
initializeStoryboard(true);
var beatmap = Beatmap.Value;
var storyboardVisible = ShowStoryboard && beatmap.Storyboard.HasDrawable;
storyboardContainer?
.FadeColour(OsuColour.Gray(BackgroundOpacity), BACKGROUND_FADE_DURATION, Easing.OutQuint)
.FadeTo(storyboardVisible && BackgroundOpacity > 0 ? 1 : 0, BACKGROUND_FADE_DURATION, Easing.OutQuint);
if (storyboardVisible && beatmap.Storyboard.ReplacesBackground)
Background?.FadeTo(0, BACKGROUND_FADE_DURATION, Easing.OutQuint);
}
}
}