1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 21:22:54 +08:00
osu-lazer/osu.Game/Tests/Visual/PlayerTestScene.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

105 lines
3.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.
2022-06-17 15:37:17 +08:00
#nullable disable
using System;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
2019-09-02 11:54:59 +08:00
using osu.Framework.Extensions.IEnumerableExtensions;
2019-03-18 18:44:14 +08:00
using osu.Framework.Testing;
2019-04-12 13:53:23 +08:00
using osu.Game.Configuration;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
namespace osu.Game.Tests.Visual
{
public abstract class PlayerTestScene : RateAdjustedBeatmapTestScene
{
/// <summary>
/// Whether custom test steps are provided. Custom tests should invoke <see cref="CreateTest"/> to create the test steps.
/// </summary>
protected virtual bool HasCustomSteps => false;
protected TestPlayer Player;
protected OsuConfigManager LocalConfig;
[BackgroundDependencyLoader]
private void load()
{
Dependencies.Cache(LocalConfig = new OsuConfigManager(LocalStorage));
LocalConfig.GetBindable<double>(OsuSetting.DimLevel).Value = 1.0;
2019-03-18 18:44:14 +08:00
}
2019-03-18 18:44:14 +08:00
[SetUpSteps]
public override void SetUpSteps()
2019-03-18 18:44:14 +08:00
{
base.SetUpSteps();
if (!HasCustomSteps)
CreateTest();
}
protected void CreateTest([CanBeNull] Action action = null)
{
if (action != null && !HasCustomSteps)
throw new InvalidOperationException($"Cannot add custom test steps without {nameof(HasCustomSteps)} being set.");
action?.Invoke();
AddStep($"Load player for {CreatePlayerRuleset().Description}", LoadPlayer);
2019-04-25 16:36:17 +08:00
AddUntilStep("player loaded", () => Player.IsLoaded && Player.Alpha == 1);
}
protected virtual bool AllowFail => false;
2019-09-02 11:54:59 +08:00
protected virtual bool Autoplay => false;
2020-03-01 13:16:28 +08:00
protected void LoadPlayer()
{
var ruleset = CreatePlayerRuleset();
Ruleset.Value = ruleset.RulesetInfo;
var beatmap = CreateBeatmap(ruleset.RulesetInfo);
Beatmap.Value = CreateWorkingBeatmap(beatmap);
SelectedMods.Value = Array.Empty<Mod>();
if (!AllowFail)
{
var noFailMod = ruleset.CreateMod<ModNoFail>();
if (noFailMod != null)
2019-12-13 20:45:38 +08:00
SelectedMods.Value = new[] { noFailMod };
}
2019-09-02 11:54:59 +08:00
if (Autoplay)
{
var mod = ruleset.GetAutoplayMod();
2019-09-02 11:54:59 +08:00
if (mod != null)
2019-12-13 20:45:38 +08:00
SelectedMods.Value = SelectedMods.Value.Concat(mod.Yield()).ToArray();
2019-09-02 11:54:59 +08:00
}
Player = CreatePlayer(ruleset);
LoadScreen(Player);
2019-09-02 11:54:59 +08:00
}
protected override void Dispose(bool isDisposing)
{
LocalConfig?.Dispose();
base.Dispose(isDisposing);
}
/// <summary>
/// Creates the ruleset for setting up the <see cref="Player"/> component.
/// </summary>
[NotNull]
protected abstract Ruleset CreatePlayerRuleset();
protected sealed override Ruleset CreateRuleset() => CreatePlayerRuleset();
protected virtual TestPlayer CreatePlayer(Ruleset ruleset) => new TestPlayer(false, false);
}
}