1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 02:22:59 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Objects/Slider.cs

296 lines
11 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Objects.Types;
using System.Collections.Generic;
using osu.Game.Rulesets.Objects;
using System.Linq;
using System.Threading;
using Newtonsoft.Json;
2023-04-26 00:12:53 +08:00
using osu.Framework.Bindables;
2019-01-03 16:43:10 +08:00
using osu.Framework.Caching;
2018-04-13 17:19:50 +08:00
using osu.Game.Audio;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Objects.Legacy;
using osu.Game.Rulesets.Osu.Judgements;
2019-09-06 14:24:00 +08:00
using osu.Game.Rulesets.Scoring;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Osu.Objects
{
public class Slider : OsuHitObject, IHasPathWithRepeats, IHasSliderVelocity, IHasGenerateTicks
2018-04-13 17:19:50 +08:00
{
2020-05-27 11:37:44 +08:00
public double EndTime => StartTime + this.SpanCount() * Path.Distance / Velocity;
[JsonIgnore]
2020-05-27 11:37:44 +08:00
public double Duration
2020-02-05 16:12:26 +08:00
{
2020-05-27 11:37:44 +08:00
get => EndTime - StartTime;
2020-02-06 12:16:32 +08:00
set => throw new System.NotSupportedException($"Adjust via {nameof(RepeatCount)} instead"); // can be implemented if/when needed.
2020-02-05 16:12:26 +08:00
}
public override IList<HitSampleInfo> AuxiliarySamples => CreateSlidingSamples().Concat(TailSamples).ToArray();
private readonly Cached<Vector2> endPositionCache = new Cached<Vector2>();
2019-01-03 16:43:10 +08:00
public override Vector2 EndPosition => endPositionCache.IsValid ? endPositionCache.Value : endPositionCache.Value = Position + this.CurvePositionAt(1);
2018-04-13 17:19:50 +08:00
public Vector2 StackedPositionAt(double t) => StackedPosition + this.CurvePositionAt(t);
private readonly SliderPath path = new SliderPath { OptimiseCatmull = true };
2019-12-06 19:53:40 +08:00
public SliderPath Path
{
get => path;
set
{
path.ControlPoints.Clear();
2023-10-30 14:10:10 +08:00
path.ControlPoints.AddRange(value.ControlPoints.Select(c => new PathControlPoint(c.Position, c.Type)));
2019-12-06 19:53:40 +08:00
2023-10-30 14:10:10 +08:00
path.ExpectedDistance.Value = value.ExpectedDistance.Value;
2019-12-06 19:53:40 +08:00
}
}
2018-04-13 17:19:50 +08:00
2018-11-12 13:07:48 +08:00
public double Distance => Path.Distance;
2018-04-13 17:19:50 +08:00
public override Vector2 Position
{
get => base.Position;
set
{
base.Position = value;
updateNestedPositions();
}
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// The position of the cursor at the point of completion of this <see cref="Slider"/> if it was hit
/// with as few movements as possible. This is set and used by difficulty calculation.
/// </summary>
internal Vector2? LazyEndPosition;
/// <summary>
/// The distance travelled by the cursor upon completion of this <see cref="Slider"/> if it was hit
/// with as few movements as possible. This is set and used by difficulty calculation.
/// </summary>
internal float LazyTravelDistance;
2021-10-13 23:41:24 +08:00
/// <summary>
/// The time taken by the cursor upon completion of this <see cref="Slider"/> if it was hit
/// with as few movements as possible. This is set and used by difficulty calculation.
/// </summary>
internal double LazyTravelTime;
public IList<IList<HitSampleInfo>> NodeSamples { get; set; } = new List<IList<HitSampleInfo>>();
2021-04-08 19:57:50 +08:00
[JsonIgnore]
public IList<HitSampleInfo> TailSamples { get; private set; }
2021-04-08 19:19:41 +08:00
2019-01-03 16:43:10 +08:00
private int repeatCount;
public int RepeatCount
{
get => repeatCount;
set
{
repeatCount = value;
updateNestedPositions();
2019-01-03 16:43:10 +08:00
}
}
2018-04-13 17:19:50 +08:00
/// <summary>
/// The length of one span of this <see cref="Slider"/>.
/// </summary>
public double SpanDuration => Duration / this.SpanCount();
2018-10-15 11:32:59 +08:00
/// <summary>
/// The computed velocity of this <see cref="Slider"/>. This is the amount of path distance travelled in 1 ms.
2018-10-15 11:32:59 +08:00
/// </summary>
public double Velocity { get; private set; }
2018-10-15 11:32:59 +08:00
/// <summary>
/// Spacing between <see cref="SliderTick"/>s of this <see cref="Slider"/>.
/// </summary>
public double TickDistance { get; private set; }
2018-04-13 17:19:50 +08:00
/// <summary>
2018-10-15 11:32:59 +08:00
/// An extra multiplier that affects the number of <see cref="SliderTick"/>s generated by this <see cref="Slider"/>.
/// An increase in this value increases <see cref="TickDistance"/>, which reduces the number of ticks generated.
/// </summary>
public double TickDistanceMultiplier = 1;
2018-04-13 17:19:50 +08:00
/// <summary>
/// If <see langword="false"/>, <see cref="Slider"/>'s judgement is fully handled by its nested <see cref="HitObject"/>s.
/// If <see langword="true"/>, this <see cref="Slider"/> will be judged proportionally to the number of nested <see cref="HitObject"/>s hit.
/// </summary>
public bool ClassicSliderBehaviour
{
get => classicSliderBehaviour;
set
{
classicSliderBehaviour = value;
if (HeadCircle != null)
HeadCircle.ClassicSliderBehaviour = value;
if (TailCircle != null)
TailCircle.ClassicSliderBehaviour = value;
}
}
private bool classicSliderBehaviour;
public BindableNumber<double> SliderVelocityMultiplierBindable { get; } = new BindableDouble(1)
{
MinValue = 0.1,
MaxValue = 10
};
2023-04-26 00:12:53 +08:00
public double SliderVelocityMultiplier
2023-04-26 00:12:53 +08:00
{
get => SliderVelocityMultiplierBindable.Value;
set => SliderVelocityMultiplierBindable.Value = value;
2023-04-26 00:12:53 +08:00
}
public bool GenerateTicks { get; set; } = true;
[JsonIgnore]
public SliderHeadCircle HeadCircle { get; protected set; }
[JsonIgnore]
public SliderTailCircle TailCircle { get; protected set; }
2018-04-13 17:19:50 +08:00
public Slider()
{
2022-06-24 20:25:23 +08:00
SamplesBindable.CollectionChanged += (_, _) => UpdateNestedSamples();
Path.Version.ValueChanged += _ => updateNestedPositions();
}
protected override void ApplyDefaultsToSelf(ControlPointInfo controlPointInfo, IBeatmapDifficultyInfo difficulty)
2018-04-13 17:19:50 +08:00
{
base.ApplyDefaultsToSelf(controlPointInfo, difficulty);
TimingControlPoint timingPoint = controlPointInfo.TimingPointAt(StartTime);
Velocity = BASE_SCORING_DISTANCE * difficulty.SliderMultiplier / LegacyRulesetExtensions.GetPrecisionAdjustedBeatLength(this, timingPoint, OsuRuleset.SHORT_NAME);
// WARNING: this is intentionally not computed as `BASE_SCORING_DISTANCE * difficulty.SliderMultiplier`
// for backwards compatibility reasons (intentionally introducing floating point errors to match stable).
double scoringDistance = Velocity * timingPoint.BeatLength;
2018-04-13 17:19:50 +08:00
TickDistance = GenerateTicks ? (scoringDistance / difficulty.SliderTickRate * TickDistanceMultiplier) : double.PositiveInfinity;
}
protected override void CreateNestedHitObjects(CancellationToken cancellationToken)
2018-04-13 17:19:50 +08:00
{
base.CreateNestedHitObjects(cancellationToken);
2018-04-13 17:19:50 +08:00
var sliderEvents = SliderEventGenerator.Generate(StartTime, SpanDuration, Velocity, TickDistance, Path.Distance, this.SpanCount(), cancellationToken);
foreach (var e in sliderEvents)
2018-04-13 17:19:50 +08:00
{
switch (e.Type)
2018-04-13 17:19:50 +08:00
{
case SliderEventType.Tick:
AddNested(new SliderTick
{
SpanIndex = e.SpanIndex,
SpanStartTime = e.SpanStartTime,
StartTime = e.Time,
Position = Position + Path.PositionAt(e.PathProgress),
StackHeight = StackHeight,
});
2018-04-13 17:19:50 +08:00
break;
2019-04-01 11:44:46 +08:00
case SliderEventType.Head:
AddNested(HeadCircle = new SliderHeadCircle
2018-04-13 17:19:50 +08:00
{
StartTime = e.Time,
Position = Position,
StackHeight = StackHeight,
ClassicSliderBehaviour = ClassicSliderBehaviour,
2018-04-13 17:19:50 +08:00
});
break;
2019-04-01 11:44:46 +08:00
case SliderEventType.Tail:
AddNested(TailCircle = new SliderTailCircle(this)
{
2020-10-02 13:20:55 +08:00
RepeatIndex = e.SpanIndex,
StartTime = e.Time,
Position = EndPosition,
StackHeight = StackHeight,
ClassicSliderBehaviour = ClassicSliderBehaviour,
});
break;
2019-04-01 11:44:46 +08:00
case SliderEventType.Repeat:
AddNested(new SliderRepeat(this)
{
RepeatIndex = e.SpanIndex,
StartTime = StartTime + (e.SpanIndex + 1) * SpanDuration,
2019-03-08 14:14:57 +08:00
Position = Position + Path.PositionAt(e.PathProgress),
StackHeight = StackHeight,
});
break;
2018-04-13 17:19:50 +08:00
}
}
UpdateNestedSamples();
2018-04-13 17:19:50 +08:00
}
private void updateNestedPositions()
{
2019-12-06 19:53:40 +08:00
endPositionCache.Invalidate();
if (HeadCircle != null)
HeadCircle.Position = Position;
if (TailCircle != null)
TailCircle.Position = EndPosition;
}
protected void UpdateNestedSamples()
{
this.PopulateNodeSamples();
// TODO: remove this when guaranteed sort is present for samples (https://github.com/ppy/osu/issues/1933)
HitSampleInfo tickSample = (Samples.FirstOrDefault(s => s.Name == HitSampleInfo.HIT_NORMAL) ?? Samples.FirstOrDefault())?.With("slidertick");
foreach (var nested in NestedHitObjects)
{
switch (nested)
{
case SliderTick tick:
tick.SamplesBindable.Clear();
if (tickSample != null)
tick.SamplesBindable.Add(tickSample);
break;
case SliderRepeat repeat:
repeat.Samples = this.GetNodeSamples(repeat.RepeatIndex + 1);
break;
}
}
if (HeadCircle != null)
HeadCircle.Samples = this.GetNodeSamples(0);
// The samples should be attached to the slider tail, however this can only be done if LastTick is removed otherwise they would play earlier than they're intended to.
// (see mapping logic in `CreateNestedHitObjects` above)
//
// For now, the samples are played by the slider itself at the correct end time.
TailSamples = this.GetNodeSamples(repeatCount + 1);
}
public override Judgement CreateJudgement() => ClassicSliderBehaviour
2023-11-10 13:00:34 +08:00
// Final combo is provided by the slider itself - see logic in `DrawableSlider.CheckForResult()`
? new OsuJudgement()
2023-11-10 13:00:34 +08:00
// Final combo is provided by the tail circle - see `SliderTailCircle`
: new OsuIgnoreJudgement();
2019-10-09 18:08:31 +08:00
protected override HitWindows CreateHitWindows() => HitWindows.Empty;
2018-04-13 17:19:50 +08:00
}
}