1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 15:33:05 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Objects/Slider.cs

271 lines
9.4 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2018-06-13 21:20:34 +08:00
using System;
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;
2019-01-03 16:43:10 +08:00
using osu.Framework.Caching;
2018-11-09 12:58:46 +08:00
using osu.Framework.Configuration;
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.Osu.Judgements;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Osu.Objects
{
public class Slider : OsuHitObject, IHasCurve
{
/// <summary>
/// Scoring distance with a speed-adjusted beat length of 1 second.
/// </summary>
private const float base_scoring_distance = 100;
public double EndTime => StartTime + this.SpanCount() * Path.Distance / Velocity;
2018-04-13 17:19:50 +08:00
public double Duration => EndTime - StartTime;
2019-01-03 16:43:10 +08:00
private Cached<Vector2> endPositionCache;
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);
2018-06-29 00:34:47 +08:00
public override int ComboIndex
{
get => base.ComboIndex;
set
{
base.ComboIndex = value;
foreach (var n in NestedHitObjects.OfType<IHasComboInformation>())
n.ComboIndex = value;
}
}
public override int IndexInCurrentCombo
{
get => base.IndexInCurrentCombo;
set
{
base.IndexInCurrentCombo = value;
foreach (var n in NestedHitObjects.OfType<IHasComboInformation>())
n.IndexInCurrentCombo = value;
}
}
2018-11-14 13:29:22 +08:00
public readonly Bindable<SliderPath> PathBindable = new Bindable<SliderPath>();
2018-04-13 17:19:50 +08:00
public SliderPath Path
2018-04-13 17:19:50 +08:00
{
2018-11-14 13:29:22 +08:00
get => PathBindable.Value;
2019-01-03 16:43:10 +08:00
set
{
PathBindable.Value = value;
endPositionCache.Invalidate();
}
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;
if (HeadCircle != null)
HeadCircle.Position = value;
if (TailCircle != null)
TailCircle.Position = EndPosition;
2019-01-03 16:43:10 +08:00
endPositionCache.Invalidate();
}
2018-04-13 17:19:50 +08:00
}
2018-06-13 21:20:34 +08:00
public double? LegacyLastTickOffset { get; set; }
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;
public List<List<SampleInfo>> NodeSamples { get; set; } = new List<List<SampleInfo>>();
2019-01-03 16:43:10 +08:00
private int repeatCount;
public int RepeatCount
{
get => repeatCount;
set
{
repeatCount = value;
endPositionCache.Invalidate();
}
}
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>
/// Velocity of this <see cref="Slider"/>.
/// </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
public HitCircle HeadCircle;
public SliderTailCircle TailCircle;
2018-04-13 17:19:50 +08:00
protected override void ApplyDefaultsToSelf(ControlPointInfo controlPointInfo, BeatmapDifficulty difficulty)
{
base.ApplyDefaultsToSelf(controlPointInfo, difficulty);
TimingControlPoint timingPoint = controlPointInfo.TimingPointAt(StartTime);
DifficultyControlPoint difficultyPoint = controlPointInfo.DifficultyPointAt(StartTime);
double scoringDistance = base_scoring_distance * difficulty.SliderMultiplier * difficultyPoint.SpeedMultiplier;
Velocity = scoringDistance / timingPoint.BeatLength;
TickDistance = scoringDistance / difficulty.SliderTickRate * TickDistanceMultiplier;
2018-04-13 17:19:50 +08:00
}
protected override void CreateNestedHitObjects()
{
base.CreateNestedHitObjects();
createSliderEnds();
createTicks();
createRepeatPoints();
2018-06-13 21:20:34 +08:00
if (LegacyLastTickOffset != null)
TailCircle.StartTime = Math.Max(StartTime + Duration / 2, TailCircle.StartTime - LegacyLastTickOffset.Value);
2018-04-13 17:19:50 +08:00
}
private void createSliderEnds()
{
2018-11-07 15:21:52 +08:00
HeadCircle = new SliderCircle
2018-04-13 17:19:50 +08:00
{
StartTime = StartTime,
Position = Position,
Samples = getNodeSamples(0),
2018-04-13 17:19:50 +08:00
SampleControlPoint = SampleControlPoint,
IndexInCurrentCombo = IndexInCurrentCombo,
ComboIndex = ComboIndex,
};
2018-11-14 13:29:22 +08:00
TailCircle = new SliderTailCircle(this)
2018-04-13 17:19:50 +08:00
{
StartTime = EndTime,
Position = EndPosition,
IndexInCurrentCombo = IndexInCurrentCombo,
ComboIndex = ComboIndex,
};
AddNested(HeadCircle);
AddNested(TailCircle);
}
private void createTicks()
{
2019-01-03 17:58:07 +08:00
// A very lenient maximum length of a slider for ticks to be generated.
// This exists for edge cases such as /b/1573664 where the beatmap has been edited by the user, and should never be reached in normal usage.
const double max_length = 100000;
var length = Math.Min(max_length, Path.Distance);
2018-04-13 17:19:50 +08:00
var tickDistance = MathHelper.Clamp(TickDistance, 0, length);
if (tickDistance == 0) return;
var minDistanceFromEnd = Velocity * 0.01;
var spanCount = this.SpanCount();
for (var span = 0; span < spanCount; span++)
{
var spanStartTime = StartTime + span * SpanDuration;
var reversed = span % 2 == 1;
for (var d = tickDistance; d <= length; d += tickDistance)
{
if (d > length - minDistanceFromEnd)
break;
var distanceProgress = d / length;
var timeProgress = reversed ? 1 - distanceProgress : distanceProgress;
2019-01-06 00:35:33 +08:00
var firstSample = Samples.Find(s => s.Name == SampleInfo.HIT_NORMAL)
2018-06-29 00:34:47 +08:00
?? Samples.FirstOrDefault(); // TODO: remove this when guaranteed sort is present for samples (https://github.com/ppy/osu/issues/1933)
2018-04-13 17:19:50 +08:00
var sampleList = new List<SampleInfo>();
if (firstSample != null)
sampleList.Add(new SampleInfo
{
Bank = firstSample.Bank,
Volume = firstSample.Volume,
Name = @"slidertick",
});
AddNested(new SliderTick
{
SpanIndex = span,
SpanStartTime = spanStartTime,
StartTime = spanStartTime + timeProgress * SpanDuration,
Position = Position + Path.PositionAt(distanceProgress),
2018-04-13 17:19:50 +08:00
StackHeight = StackHeight,
Scale = Scale,
Samples = sampleList
});
}
}
}
private void createRepeatPoints()
{
for (int repeatIndex = 0, repeat = 1; repeatIndex < RepeatCount; repeatIndex++, repeat++)
{
AddNested(new RepeatPoint
{
RepeatIndex = repeatIndex,
SpanDuration = SpanDuration,
StartTime = StartTime + repeat * SpanDuration,
Position = Position + Path.PositionAt(repeat % 2),
2018-04-13 17:19:50 +08:00
StackHeight = StackHeight,
Scale = Scale,
Samples = getNodeSamples(1 + repeatIndex)
2018-04-13 17:19:50 +08:00
});
}
}
private List<SampleInfo> getNodeSamples(int nodeIndex)
{
if (nodeIndex < NodeSamples.Count)
return NodeSamples[nodeIndex];
return Samples;
}
2018-08-06 10:50:18 +08:00
public override Judgement CreateJudgement() => new OsuJudgement();
2018-04-13 17:19:50 +08:00
}
}