From 56922b66be39193d207e1bcf2871ed02ff0e1171 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 6 Mar 2017 11:11:29 +0900 Subject: [PATCH] Refactor sliders to have more central position/progress calculations. --- .../Objects/Drawables/DrawableSlider.cs | 7 ++--- osu.Game.Modes.Osu/Objects/Slider.cs | 27 ++++++++++++++++++- osu.Game.Modes.Osu/Objects/SliderCurve.cs | 6 ++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/osu.Game.Modes.Osu/Objects/Drawables/DrawableSlider.cs b/osu.Game.Modes.Osu/Objects/Drawables/DrawableSlider.cs index 907ce8da63..a3e78ed422 100644 --- a/osu.Game.Modes.Osu/Objects/Drawables/DrawableSlider.cs +++ b/osu.Game.Modes.Osu/Objects/Drawables/DrawableSlider.cs @@ -102,8 +102,8 @@ namespace osu.Game.Modes.Osu.Objects.Drawables double progress = MathHelper.Clamp((Time.Current - slider.StartTime) / slider.Duration, 0, 1); - int repeat = (int)(progress * slider.RepeatCount); - progress = (progress * slider.RepeatCount) % 1; + int repeat = slider.RepeatAt(progress); + progress = slider.CurveProgressAt(progress); if (repeat > currentRepeat) { @@ -112,9 +112,6 @@ namespace osu.Game.Modes.Osu.Objects.Drawables currentRepeat = repeat; } - if (repeat % 2 == 1) - progress = 1 - progress; - bouncer2.Position = slider.Curve.PositionAt(body.SnakedEnd ?? 0); //todo: we probably want to reconsider this before adding scoring, but it looks and feels nice. diff --git a/osu.Game.Modes.Osu/Objects/Slider.cs b/osu.Game.Modes.Osu/Objects/Slider.cs index 5f6c50fd72..88abb68d0a 100644 --- a/osu.Game.Modes.Osu/Objects/Slider.cs +++ b/osu.Game.Modes.Osu/Objects/Slider.cs @@ -14,7 +14,32 @@ namespace osu.Game.Modes.Osu.Objects { public override double EndTime => StartTime + RepeatCount * Curve.Length / Velocity; - public override Vector2 EndPosition => RepeatCount % 2 == 0 ? Position : Curve.PositionAt(1); + public override Vector2 EndPosition => PositionAt(1); + + /// + /// Computes the position on the slider at a given progress that ranges from 0 (beginning of the slider) + /// to 1 (end of the slider). This includes repeat logic. + /// + /// Ranges from 0 (beginning of the slider) to 1 (end of the slider). + /// + public Vector2 PositionAt(double progress) => Curve.PositionAt(CurveProgressAt(progress)); + + /// + /// Find the current progress along the curve, accounting for repeat logic. + /// + public double CurveProgressAt(double progress) + { + var p = progress * RepeatCount % 1; + if (RepeatAt(progress) % 2 == 1) + p = 1 - p; + return p; + } + + /// + /// Determine which repeat of the slider we are on at a given progress. + /// Range is 0..RepeatCount where 0 is the first run. + /// + public int RepeatAt(double progress) => (int)(progress * RepeatCount); private int stackHeight; public override int StackHeight diff --git a/osu.Game.Modes.Osu/Objects/SliderCurve.cs b/osu.Game.Modes.Osu/Objects/SliderCurve.cs index 3b52a41b8c..e37da355c0 100644 --- a/osu.Game.Modes.Osu/Objects/SliderCurve.cs +++ b/osu.Game.Modes.Osu/Objects/SliderCurve.cs @@ -186,10 +186,10 @@ namespace osu.Game.Modes.Osu.Objects } /// - /// Computes the position on the slider at a given progress that ranges from 0 (beginning of the slider) - /// to 1 (end of the slider). + /// Computes the position on the slider at a given progress that ranges from 0 (beginning of the curve) + /// to 1 (end of the curve). /// - /// Ranges from 0 (beginning of the slider) to 1 (end of the slider). + /// Ranges from 0 (beginning of the curve) to 1 (end of the curve). /// public Vector2 PositionAt(double progress) {