From 38a4bdf068c7b727e046a5b6d6119d6b3339f51b Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 30 Jul 2020 19:34:59 +0900 Subject: [PATCH] Add spinner spin sample support --- .../Objects/Drawables/DrawableSpinner.cs | 62 ++++++++++++++++++- .../Pieces/SpinnerRotationTracker.cs | 9 ++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSpinner.cs b/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSpinner.cs index f65570a3d5..68516bedf8 100644 --- a/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSpinner.cs +++ b/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableSpinner.cs @@ -70,6 +70,58 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables }; } + private Bindable isSpinning; + + protected override void LoadComplete() + { + base.LoadComplete(); + + isSpinning = RotationTracker.IsSpinning.GetBoundCopy(); + isSpinning.BindValueChanged(updateSpinningSample); + } + + private SkinnableSound spinningSample; + + private const float minimum_volume = 0.0001f; + + protected override void LoadSamples() + { + base.LoadSamples(); + + spinningSample?.Expire(); + spinningSample = null; + + var firstSample = HitObject.Samples.FirstOrDefault(); + + if (firstSample != null) + { + var clone = HitObject.SampleControlPoint.ApplyTo(firstSample); + clone.Name = "spinnerspin"; + + AddInternal(spinningSample = new SkinnableSound(clone) + { + Volume = { Value = minimum_volume }, + Looping = true, + }); + } + } + + private void updateSpinningSample(ValueChangedEvent tracking) + { + // note that samples will not start playing if exiting a seek operation in the middle of a spinner. + // may be something we want to address at a later point, but not so easy to make happen right now + // (SkinnableSound would need to expose whether the sample is already playing and this logic would need to run in Update). + if (tracking.NewValue && ShouldPlaySamples) + { + spinningSample?.Play(); + spinningSample?.VolumeTo(1, 200); + } + else + { + spinningSample?.VolumeTo(minimum_volume, 200).Finally(_ => spinningSample.Stop()); + } + } + protected override void AddNestedHitObject(DrawableHitObject hitObject) { base.AddNestedHitObject(hitObject); @@ -88,6 +140,9 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables using (BeginDelayedSequence(Spinner.Duration, true)) this.FadeOut(160); + + // skin change does a rewind of transforms, which will stop the spinning sound from playing if it's currently in playback. + isSpinning?.TriggerChange(); } protected override void ClearNestedHitObjects() @@ -151,8 +206,13 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables protected override void Update() { base.Update(); + if (HandleUserInput) - RotationTracker.Tracking = OsuActionInputManager?.PressedActions.Any(x => x == OsuAction.LeftButton || x == OsuAction.RightButton) ?? false; + RotationTracker.Tracking = !Result.HasResult && (OsuActionInputManager?.PressedActions.Any(x => x == OsuAction.LeftButton || x == OsuAction.RightButton) ?? false); + + if (spinningSample != null) + // todo: implement SpinnerFrequencyModulate + spinningSample.Frequency.Value = 0.5f + Progress; } protected override void UpdateAfterChildren() diff --git a/osu.Game.Rulesets.Osu/Objects/Drawables/Pieces/SpinnerRotationTracker.cs b/osu.Game.Rulesets.Osu/Objects/Drawables/Pieces/SpinnerRotationTracker.cs index 968c2a6df5..0cc6c842f4 100644 --- a/osu.Game.Rulesets.Osu/Objects/Drawables/Pieces/SpinnerRotationTracker.cs +++ b/osu.Game.Rulesets.Osu/Objects/Drawables/Pieces/SpinnerRotationTracker.cs @@ -43,6 +43,11 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces /// public float CumulativeRotation { get; private set; } + /// + /// Whether the spinning is spinning at a reasonable speed to be considered visually spinning. + /// + public readonly BindableBool IsSpinning = new BindableBool(); + /// /// Whether currently in the correct time range to allow spinning. /// @@ -73,7 +78,9 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces lastAngle = thisAngle; - Rotation = (float)Interpolation.Lerp(Rotation, currentRotation / 2, Math.Clamp(Math.Abs(Time.Elapsed) / 40, 0, 1)); + IsSpinning.Value = isSpinnableTime && Math.Abs(currentRotation / 2 - Rotation) > 5f; + + Rotation = (float)Interpolation.Damp(Rotation, currentRotation / 2, 0.99, Math.Abs(Time.Elapsed)); } ///