osu/osu.Game.Rulesets.Osu/Mods/OsuModBubbles.cs

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

256 lines
9.9 KiB
C#
Raw Normal View History

2022-12-25 20:32:47 +00: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 System;
using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
2022-12-25 20:32:47 +00:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Pooling;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Localisation;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Rulesets.Osu.Skinning.Default;
2022-12-25 20:32:47 +00:00
using osu.Game.Rulesets.Scoring;
using osu.Game.Rulesets.UI;
using osu.Game.Scoring;
using osuTK;
namespace osu.Game.Rulesets.Osu.Mods
{
public partial class OsuModBubbles : ModWithVisibilityAdjustment, IApplicableToDrawableRuleset<OsuHitObject>, IApplicableToScoreProcessor
{
public override string Name => "Bubbles";
2023-02-06 08:32:17 +00:00
public override string Acronym => "BU";
2022-12-25 20:32:47 +00:00
2023-02-06 08:32:17 +00:00
public override LocalisableString Description => "Don't let their popping distract you!";
2022-12-25 20:32:47 +00:00
public override double ScoreMultiplier => 1;
public override ModType Type => ModType.Fun;
2023-02-06 08:32:17 +00:00
// Compatibility with these seems potentially feasible in the future, blocked for now because they don't work as one would expect
2022-12-25 20:32:47 +00:00
public override Type[] IncompatibleMods => new[] { typeof(OsuModBarrelRoll), typeof(OsuModMagnetised), typeof(OsuModRepel) };
private PlayfieldAdjustmentContainer bubbleContainer = null!;
2022-12-25 20:32:47 +00:00
private readonly Bindable<int> currentCombo = new BindableInt();
private float maxSize;
private float bubbleRadius;
private double bubbleFade;
private readonly DrawablePool<BubbleDrawable> bubblePool = new DrawablePool<BubbleDrawable>(100);
private DrawableOsuHitObject lastJudgedHitobject = null!;
2022-12-25 20:32:47 +00:00
public ScoreRank AdjustRank(ScoreRank rank, double accuracy) => rank;
public void ApplyToScoreProcessor(ScoreProcessor scoreProcessor)
{
currentCombo.BindTo(scoreProcessor.Combo);
currentCombo.BindValueChanged(combo =>
maxSize = Math.Min(1.75f, (float)(1.25 + 0.005 * combo.NewValue)), true);
scoreProcessor.NewJudgement += result =>
{
if (result.HitObject is not OsuHitObject osuHitObject) return;
DrawableOsuHitObject drawableOsuHitObject = lastJudgedHitobject;
switch (result.HitObject)
{
case Slider:
case SpinnerTick:
break;
default:
addBubble();
break;
}
void addBubble()
{
BubbleDrawable bubble = bubblePool.Get();
bubble.Info = new BubbleInfo
{
InitialSize = new Vector2(bubbleRadius),
MaxSize = maxSize,
Position = getPosition(),
FadeTime = bubbleFade,
Colour = drawableOsuHitObject.AccentColour.Value,
IsHit = drawableOsuHitObject.IsHit,
};
bubbleContainer.Add(bubble);
}
Vector2 getPosition()
{
switch (drawableOsuHitObject)
{
// SliderHeads are derived from HitCircles,
// so we must handle them before to avoid them using the wrong positioning logic
case DrawableSliderHead:
return osuHitObject.Position;
// Using hitobject position will cause issues with HitCircle placement due to stack leniency.
case DrawableHitCircle:
return drawableOsuHitObject.Position;
default:
return osuHitObject.Position;
}
}
};
scoreProcessor.JudgementReverted += _ =>
{
bubbleContainer.LastOrDefault()?.FinishTransforms();
bubbleContainer.LastOrDefault()?.Expire();
};
2022-12-25 20:32:47 +00:00
}
public void ApplyToDrawableRuleset(DrawableRuleset<OsuHitObject> drawableRuleset)
{
// Multiplying by 2 results in an initial size that is too large, hence 1.90 has been chosen
// Also avoids the HitObject bleeding around the edges of the bubble drawable at minimum size
bubbleRadius = (float)(drawableRuleset.Beatmap.HitObjects.OfType<HitCircle>().First().Radius * 1.90f);
bubbleFade = drawableRuleset.Beatmap.HitObjects.OfType<HitCircle>().First().TimePreempt * 2;
2022-12-25 20:32:47 +00:00
// We want to hide the judgements since they are obscured by the BubbleDrawable (due to layering)
drawableRuleset.Playfield.DisplayJudgements.Value = false;
bubbleContainer = drawableRuleset.CreatePlayfieldAdjustmentContainer();
2022-12-25 20:32:47 +00:00
drawableRuleset.KeyBindingInputManager.Add(bubbleContainer);
2022-12-25 20:32:47 +00:00
}
protected override void ApplyIncreasedVisibilityState(DrawableHitObject hitObject, ArmedState state) => applyBubbleState(hitObject);
protected override void ApplyNormalVisibilityState(DrawableHitObject hitObject, ArmedState state) => applyBubbleState(hitObject);
private void applyBubbleState(DrawableHitObject drawableObject)
{
DrawableOsuHitObject osuHitObject = (DrawableOsuHitObject)drawableObject;
if (drawableObject is DrawableSlider slider)
{
slider.Body.OnSkinChanged += () => applySliderState(slider);
applySliderState(slider);
}
if (osuHitObject == lastJudgedHitobject || !osuHitObject.Judged) return;
2022-12-25 20:32:47 +00:00
switch (osuHitObject)
2022-12-25 20:32:47 +00:00
{
case DrawableSlider:
case DrawableSpinnerTick:
break;
2022-12-25 20:32:47 +00:00
default:
lastJudgedHitobject = osuHitObject;
2022-12-25 20:32:47 +00:00
break;
}
2022-12-25 20:33:10 +00:00
}
2022-12-25 20:32:47 +00:00
// Makes the slider border coloured on all skins (for aesthetics)
private void applySliderState(DrawableSlider slider) =>
((PlaySliderBody)slider.Body.Drawable).BorderColour = slider.AccentColour.Value;
2022-12-25 20:32:47 +00:00
#region Pooled Bubble drawable
private partial class BubbleDrawable : PoolableDrawable
2022-12-25 20:32:47 +00:00
{
private readonly Box colourBox;
private readonly CircularContainer content;
public BubbleInfo Info { get; set; }
2023-01-11 16:51:41 +00:00
public BubbleDrawable()
2022-12-25 20:32:47 +00:00
{
Origin = Anchor.Centre;
InternalChild = content = new CircularContainer
2022-12-25 20:32:47 +00:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
MaskingSmoothness = 2,
BorderThickness = 0,
BorderColour = Colour4.White,
Masking = true,
EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Radius = 3,
Colour = Colour4.Black.Opacity(0.05f),
},
Child = colourBox = new Box { RelativeSizeAxes = Axes.Both, }
};
2022-12-25 20:32:47 +00:00
}
protected override void PrepareForUse()
2022-12-25 20:32:47 +00:00
{
Alpha = 1;
Colour = Colour4.White;
Scale = new Vector2(1);
Position = Info.Position;
Size = Info.InitialSize;
content.BorderThickness = Info.InitialSize.X / 3.5f;
content.BorderColour = Colour4.White;
2023-01-27 23:30:30 +00:00
//We want to fade to a darker colour to avoid colours such as white hiding the "ripple" effect.
ColourInfo colourDarker = Info.Colour.Darken(0.1f);
2023-01-27 23:30:30 +00:00
// Main bubble scaling based on combo
this.ScaleTo(Info.MaxSize, getAnimationDuration() * 0.8f)
2022-12-25 20:32:47 +00:00
.Then()
// Pop at the end of the bubbles life time
.ScaleTo(Info.MaxSize * 1.5f, getAnimationDuration() * 0.2f, Easing.OutQuint)
.FadeOutFromOne(getAnimationDuration() * 0.2f, Easing.OutCirc).Expire();
2023-01-11 16:51:41 +00:00
if (Info.IsHit)
2023-01-27 23:30:30 +00:00
{
colourBox.FadeColour(colourDarker);
content.TransformTo(nameof(BorderColour), colourDarker, getAnimationDuration() * 0.3f, Easing.OutQuint);
// Ripple effect utilises the border to reduce drawable count
content.TransformTo(nameof(BorderThickness), 2f, getAnimationDuration() * 0.3f, Easing.OutQuint)
// Avoids transparency overlap issues during the bubble "pop"
.Then().Schedule(() => content.BorderThickness = 0);
2023-01-11 16:51:41 +00:00
2023-01-27 23:30:30 +00:00
return;
}
2023-01-11 16:51:41 +00:00
Colour = Colour4.Black;
2023-01-11 16:51:41 +00:00
// The absolute length of the bubble's animation, can be used in fractions for animations of partial length
double getAnimationDuration() => 1700 + Math.Pow(Info.FadeTime, 1.07f);
2022-12-25 20:32:47 +00:00
}
}
private struct BubbleInfo
2022-12-25 20:32:47 +00:00
{
public Vector2 InitialSize { get; set; }
public float MaxSize { get; set; }
public Vector2 Position { get; set; }
public Colour4 Colour { get; set; }
// FadeTime is based on the approach rate of the beatmap.
public double FadeTime { get; set; }
// Whether the corresponding HitObject was hit
public bool IsHit { get; set; }
}
#endregion
}
}