1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-17 01:32:55 +08:00
osu-lazer/osu.Game/Screens/Menu/IntroScreen.cs

198 lines
6.6 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.Audio.Sample;
2019-09-23 20:52:44 +08:00
using osu.Framework.Audio.Track;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
using osu.Framework.Screens;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
2019-09-23 20:52:44 +08:00
using osu.Game.IO.Archives;
using osu.Game.Screens.Backgrounds;
2019-09-23 20:52:44 +08:00
using osu.Game.Skinning;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Menu
{
public abstract class IntroScreen : StartupScreen
{
2019-10-08 11:05:52 +08:00
/// <summary>
/// Whether we have loaded the menu previously.
/// </summary>
public bool DidLoadMenu { get; private set; }
2019-10-08 10:52:16 +08:00
/// <summary>
/// A hash used to find the associated beatmap if already imported.
/// </summary>
2019-09-23 20:52:44 +08:00
protected abstract string BeatmapHash { get; }
2019-10-08 10:52:16 +08:00
/// <summary>
/// A source file to use as an import source if the intro beatmap is not yet present.
/// Should be within the "Tracks" namespace of game resources.
/// </summary>
2019-09-23 20:52:44 +08:00
protected abstract string BeatmapFile { get; }
2019-10-08 11:08:47 +08:00
protected IBindable<bool> MenuVoice { get; private set; }
2019-09-23 20:52:44 +08:00
2019-10-08 11:08:47 +08:00
protected IBindable<bool> MenuMusic { get; private set; }
2019-09-23 20:52:44 +08:00
private WorkingBeatmap initialBeatmap;
2019-10-08 11:05:52 +08:00
protected Track Track => initialBeatmap?.Track;
2019-09-23 20:52:44 +08:00
2019-10-08 11:05:52 +08:00
private readonly BindableDouble exitingVolumeFade = new BindableDouble(1);
private const int exit_delay = 3000;
private SampleChannel seeya;
private LeasedBindable<WorkingBeatmap> beatmap;
2019-10-08 11:05:52 +08:00
private MainMenu mainMenu;
2019-10-08 11:05:52 +08:00
[Resolved]
private AudioManager audio { get; set; }
2019-10-08 11:03:42 +08:00
/// <summary>
/// Whether the <see cref="Track"/> is provided by osu! resources, rather than a user beatmap.
/// </summary>
protected bool UsingThemedIntro { get; private set; }
[BackgroundDependencyLoader]
2019-09-24 18:17:27 +08:00
private void load(OsuConfigManager config, SkinManager skinManager, BeatmapManager beatmaps, Framework.Game game)
{
2019-08-21 13:01:07 +08:00
// prevent user from changing beatmap while the intro is still runnning.
2019-10-08 11:04:13 +08:00
beatmap = Beatmap.BeginLease(false);
2019-09-23 20:52:44 +08:00
MenuVoice = config.GetBindable<bool>(OsuSetting.MenuVoice);
MenuMusic = config.GetBindable<bool>(OsuSetting.MenuMusic);
2019-09-24 18:17:27 +08:00
seeya = audio.Samples.Get(@"seeya");
2019-09-23 20:52:44 +08:00
BeatmapSetInfo setInfo = null;
// if the user has requested not to play theme music, we should attempt to find a random beatmap from their collection.
2019-09-23 20:52:44 +08:00
if (!MenuMusic.Value)
{
var sets = beatmaps.GetAllUsableBeatmapSets(IncludedDetails.Minimal);
2019-09-23 20:52:44 +08:00
if (sets.Count > 0)
{
2019-09-23 20:52:44 +08:00
setInfo = beatmaps.QueryBeatmapSet(s => s.ID == sets[RNG.Next(0, sets.Count - 1)].ID);
initialBeatmap = beatmaps.GetWorkingBeatmap(setInfo.Beatmaps[0]);
}
2019-09-23 20:52:44 +08:00
}
// we generally want a song to be playing on startup, so use the intro music even if a user has specified not to if no other track is available.
2019-09-23 20:52:44 +08:00
if (setInfo == null)
{
if (!loadThemedIntro())
2019-09-23 20:52:44 +08:00
{
// if we detect that the theme track or beatmap is unavailable this is either first startup or things are in a bad state.
// this could happen if a user has nuked their files store. for now, reimport to repair this.
var import = beatmaps.Import(new ZipArchiveReader(game.Resources.GetStream($"Tracks/{BeatmapFile}"), BeatmapFile)).Result;
import.Protected = true;
beatmaps.Update(import);
2019-09-23 20:52:44 +08:00
loadThemedIntro();
2019-09-23 20:52:44 +08:00
}
}
bool loadThemedIntro()
{
setInfo = beatmaps.QueryBeatmapSet(b => b.Hash == BeatmapHash);
if (setInfo != null)
{
initialBeatmap = beatmaps.GetWorkingBeatmap(setInfo.Beatmaps[0]);
UsingThemedIntro = !(Track is TrackVirtual);
}
return UsingThemedIntro;
}
2019-09-23 20:52:44 +08:00
}
public override void OnResuming(IScreen last)
{
this.FadeIn(300);
2019-10-08 10:54:39 +08:00
double fadeOutTime = exit_delay;
2020-05-05 09:31:11 +08:00
// we also handle the exit transition.
2019-09-23 20:52:44 +08:00
if (MenuVoice.Value)
seeya.Play();
else
fadeOutTime = 500;
audio.AddAdjustment(AdjustableProperty.Volume, exitingVolumeFade);
this.TransformBindableTo(exitingVolumeFade, 0, fadeOutTime).OnComplete(_ => this.Exit());
//don't want to fade out completely else we will stop running updates.
Game.FadeTo(0.01f, fadeOutTime);
base.OnResuming(last);
}
2019-10-08 11:05:52 +08:00
public override void OnSuspending(IScreen next)
{
base.OnSuspending(next);
initialBeatmap = null;
2019-10-08 11:05:52 +08:00
}
protected override BackgroundScreen CreateBackground() => new BackgroundScreenBlack();
protected void StartTrack()
{
// Only start the current track if it is the menu music. A beatmap's track is started when entering the Main Menu.
if (UsingThemedIntro)
2019-10-08 11:05:52 +08:00
Track.Restart();
}
protected override void LogoArriving(OsuLogo logo, bool resuming)
{
base.LogoArriving(logo, resuming);
logo.Colour = Color4.White;
logo.Triangles = false;
logo.Ripple = false;
if (!resuming)
{
beatmap.Value = initialBeatmap;
2019-10-08 11:03:42 +08:00
logo.MoveTo(new Vector2(0.5f));
logo.ScaleTo(Vector2.One);
logo.Hide();
}
else
{
const int quick_appear = 350;
2019-10-08 11:05:52 +08:00
var initialMovementTime = logo.Alpha > 0.2f ? quick_appear : 0;
logo.MoveTo(new Vector2(0.5f), initialMovementTime, Easing.OutQuint);
logo
.ScaleTo(1, initialMovementTime, Easing.OutQuint)
.FadeIn(quick_appear, Easing.OutQuint)
.Then()
2019-10-08 10:54:39 +08:00
.RotateTo(20, exit_delay * 1.5f)
.FadeOut(exit_delay);
}
}
2019-10-08 11:05:52 +08:00
protected void PrepareMenuLoad() => LoadComponentAsync(mainMenu = new MainMenu());
protected void LoadMenu()
{
2019-08-20 17:18:41 +08:00
beatmap.Return();
2019-08-21 13:01:07 +08:00
DidLoadMenu = true;
this.Push(mainMenu);
}
}
}