1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-26 06:02:55 +08:00
osu-lazer/osu.Game/Screens/Play/FailAnimation.cs

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

232 lines
7.7 KiB
C#
Raw Normal View History

2019-06-04 15:13:16 +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.
using osu.Framework.Audio;
using osu.Framework.Bindables;
using osu.Game.Rulesets.UI;
using System;
using System.Collections.Generic;
using ManagedBass.Fx;
2019-06-04 15:13:16 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio.Sample;
using osu.Framework.Audio.Track;
2021-11-01 12:06:26 +08:00
using osu.Framework.Extensions.Color4Extensions;
2019-06-04 15:13:16 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
using osu.Game.Audio;
using osu.Game.Audio.Effects;
2019-06-04 15:13:16 +08:00
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Graphics;
2019-06-04 15:13:16 +08:00
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Skinning;
2019-06-04 15:13:16 +08:00
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Screens.Play
{
/// <summary>
/// Manage the animation to be applied when a player fails.
/// Single use and automatically disposed after use.
2019-06-04 15:13:16 +08:00
/// </summary>
public partial class FailAnimation : Container
2019-06-04 15:13:16 +08:00
{
2022-08-25 13:35:42 +08:00
public Action? OnComplete;
2019-06-04 15:13:16 +08:00
private readonly DrawableRuleset drawableRuleset;
private readonly BindableDouble trackFreq = new BindableDouble(1);
private readonly BindableDouble volumeAdjustment = new BindableDouble(0.5);
2019-06-04 15:13:16 +08:00
2022-08-25 13:35:42 +08:00
private Container filters = null!;
2021-10-15 20:22:38 +08:00
2022-08-25 13:35:42 +08:00
private Box redFlashLayer = null!;
private Track track = null!;
2019-06-04 15:13:16 +08:00
2022-08-25 13:35:42 +08:00
private AudioFilter failLowPassFilter = null!;
private AudioFilter failHighPassFilter = null!;
2019-06-04 15:13:16 +08:00
private const float duration = 2500;
private ISample? failSample;
private SampleChannel? failSampleChannel;
2019-06-04 15:13:16 +08:00
[Resolved]
2022-08-25 13:35:42 +08:00
private OsuConfigManager config { get; set; } = null!;
protected override Container<Drawable> Content { get; } = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
};
/// <summary>
/// The player screen background, used to adjust appearance on failing.
/// </summary>
2022-08-25 13:35:42 +08:00
public BackgroundScreen? Background { private get; set; }
2019-06-04 15:13:16 +08:00
public FailAnimation(DrawableRuleset drawableRuleset)
{
this.drawableRuleset = drawableRuleset;
RelativeSizeAxes = Axes.Both;
2019-06-04 15:13:16 +08:00
}
[BackgroundDependencyLoader]
private void load(AudioManager audio, ISkinSource skin, IBindable<WorkingBeatmap> beatmap)
2019-06-04 15:13:16 +08:00
{
track = beatmap.Value.Track;
failSample = skin.GetSample(new SampleInfo(@"Gameplay/failsound"));
AddRangeInternal(new Drawable[]
{
2021-10-15 20:22:38 +08:00
filters = new Container
{
Children = new Drawable[]
{
failLowPassFilter = new AudioFilter(audio.TrackMixer),
failHighPassFilter = new AudioFilter(audio.TrackMixer, BQFType.HighPass),
},
},
Content,
redFlashLayer = new Box
{
2021-11-01 12:06:26 +08:00
Colour = Color4.Red.Opacity(0.6f),
RelativeSizeAxes = Axes.Both,
Blending = BlendingParameters.Additive,
Depth = float.MinValue,
Alpha = 0
},
});
2019-06-04 15:13:16 +08:00
}
private bool started;
private bool filtersRemoved;
2019-06-04 15:13:16 +08:00
/// <summary>
/// Start the fail animation playing.
/// </summary>
/// <exception cref="InvalidOperationException">Thrown if started more than once.</exception>
public void Start()
{
if (started) throw new InvalidOperationException("Animation cannot be started more than once.");
if (filtersRemoved) throw new InvalidOperationException("Animation cannot be started after filters have been removed.");
2019-06-04 15:13:16 +08:00
started = true;
this.TransformBindableTo(trackFreq, 0, duration).OnComplete(_ =>
{
// Don't reset frequency as the pause screen may appear post transform, causing a second frequency sweep.
removeFilters(false);
2019-06-04 15:13:16 +08:00
OnComplete?.Invoke();
});
failHighPassFilter.CutoffTo(300);
failLowPassFilter.CutoffTo(300, duration, Easing.OutCubic);
failSampleChannel = failSample?.Play();
2019-06-04 15:13:16 +08:00
track.AddAdjustment(AdjustableProperty.Frequency, trackFreq);
track.AddAdjustment(AdjustableProperty.Volume, volumeAdjustment);
2019-06-04 15:13:16 +08:00
applyToPlayfield(drawableRuleset.Playfield);
drawableRuleset.Playfield.HitObjectContainer.FadeOut(duration / 2);
if (config.Get<bool>(OsuSetting.FadePlayfieldWhenHealthLow))
redFlashLayer.FadeOutFromOne(1000);
Content.Masking = true;
Content.Add(new Box
{
Colour = Color4.Black,
RelativeSizeAxes = Axes.Both,
Depth = float.MaxValue
});
2021-10-15 18:37:22 +08:00
Content.ScaleTo(0.85f, duration, Easing.OutQuart);
Content.RotateTo(1, duration, Easing.OutQuart);
Content.FadeColour(Color4.Gray, duration);
// Will be restored by `ApplyToBackground` logic in `SongSelect`.
Background?.FadeColour(OsuColour.Gray(0.3f), 60);
2019-06-04 15:13:16 +08:00
}
/// <summary>
/// Stops any and all persistent effects added by the ongoing fail animation.
/// </summary>
public void Stop()
{
failSampleChannel?.Stop();
removeFilters();
}
private void removeFilters(bool resetTrackFrequency = true)
2021-10-15 20:22:38 +08:00
{
filtersRemoved = true;
if (!started)
return;
if (resetTrackFrequency)
track.RemoveAdjustment(AdjustableProperty.Frequency, trackFreq);
track.RemoveAdjustment(AdjustableProperty.Volume, volumeAdjustment);
if (filters.Parent == null)
return;
2022-08-29 14:49:28 +08:00
RemoveInternal(filters, true);
2021-10-15 20:22:38 +08:00
}
2019-06-04 15:13:16 +08:00
protected override void Update()
{
base.Update();
if (!started)
return;
applyToPlayfield(drawableRuleset.Playfield);
}
private readonly List<DrawableHitObject> appliedObjects = new List<DrawableHitObject>();
private void applyToPlayfield(Playfield playfield)
{
double failTime = playfield.Time.Current;
2019-06-04 15:13:16 +08:00
foreach (var nested in playfield.NestedPlayfields)
applyToPlayfield(nested);
foreach (DrawableHitObject obj in playfield.HitObjectContainer.AliveObjects)
{
if (appliedObjects.Contains(obj))
continue;
float rotation = RNG.NextSingle(-90, 90);
Vector2 originalPosition = obj.Position;
Vector2 originalScale = obj.Scale;
dropOffScreen(obj, failTime, rotation, originalScale, originalPosition);
// need to reapply the fail drop after judgement state changes
2022-06-24 20:25:23 +08:00
obj.ApplyCustomUpdateState += (_, _) => dropOffScreen(obj, failTime, rotation, originalScale, originalPosition);
2019-06-04 15:13:16 +08:00
appliedObjects.Add(obj);
}
}
private void dropOffScreen(DrawableHitObject obj, double failTime, float randomRotation, Vector2 originalScale, Vector2 originalPosition)
{
using (obj.BeginAbsoluteSequence(failTime))
{
obj.RotateTo(randomRotation, duration);
obj.ScaleTo(originalScale * 0.5f, duration);
obj.MoveTo(originalPosition + new Vector2(0, 400), duration);
}
}
2019-06-04 15:13:16 +08:00
}
}