1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-07 07:27:25 +08:00
osu-lazer/osu.Game.Modes.Osu/Objects/Slider.cs

144 lines
5.1 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK;
using osu.Game.Beatmaps;
2017-02-13 03:38:05 +08:00
using osu.Game.Beatmaps.Samples;
using osu.Game.Beatmaps.Timing;
using osu.Game.Modes.Objects.Types;
using System;
using System.Collections.Generic;
2016-11-14 17:54:24 +08:00
namespace osu.Game.Modes.Osu.Objects
{
2017-03-14 16:01:46 +08:00
public class Slider : OsuHitObject, IHasEndTime, IHasCurve, IHasDistance, IHasRepeats
{
public double EndTime => StartTime + RepeatCount * Curve.Length / Velocity;
public double Duration => EndTime - StartTime;
public override Vector2 EndPosition => PositionAt(1);
/// <summary>
/// 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.
/// </summary>
/// <param name="progress">Ranges from 0 (beginning of the slider) to 1 (end of the slider).</param>
/// <returns></returns>
public Vector2 PositionAt(double progress) => Curve.PositionAt(CurveProgressAt(progress));
/// <summary>
/// Find the current progress along the curve, accounting for repeat logic.
/// </summary>
public double CurveProgressAt(double progress)
{
var p = progress * RepeatCount % 1;
if (RepeatAt(progress) % 2 == 1)
p = 1 - p;
return p;
}
/// <summary>
/// Determine which repeat of the slider we are on at a given progress.
/// Range is 0..RepeatCount where 0 is the first run.
/// </summary>
public int RepeatAt(double progress) => (int)(progress * RepeatCount);
private int stackHeight;
public override int StackHeight
{
get { return stackHeight; }
set
{
stackHeight = value;
Curve.Offset = StackOffset;
}
}
public List<Vector2> ControlPoints
{
get { return Curve.ControlPoints; }
set { Curve.ControlPoints = value; }
}
2017-03-14 16:01:46 +08:00
public double Distance
{
get { return Curve.Length; }
set { Curve.Length = value; }
}
public CurveType CurveType
{
get { return Curve.CurveType; }
set { Curve.CurveType = value; }
}
public double Velocity;
2017-02-13 03:38:05 +08:00
public double TickDistance;
public override void SetDefaultsFromBeatmap(Beatmap beatmap)
{
base.SetDefaultsFromBeatmap(beatmap);
2017-02-13 03:38:05 +08:00
var baseDifficulty = beatmap.BeatmapInfo.BaseDifficulty;
ControlPoint overridePoint;
ControlPoint timingPoint = beatmap.TimingPointAt(StartTime, out overridePoint);
var velocityAdjustment = overridePoint?.VelocityAdjustment ?? 1;
var baseVelocity = 100 * baseDifficulty.SliderMultiplier / velocityAdjustment;
2017-02-13 03:38:05 +08:00
Velocity = baseVelocity / timingPoint.BeatLength;
TickDistance = baseVelocity / baseDifficulty.SliderTickRate;
}
2017-03-14 16:01:46 +08:00
public int RepeatCount { get; set; } = 1;
2016-11-17 20:29:35 +08:00
internal readonly SliderCurve Curve = new SliderCurve();
2017-02-13 03:38:05 +08:00
public IEnumerable<SliderTick> Ticks
{
get
{
if (TickDistance == 0) yield break;
2017-02-13 03:38:05 +08:00
var length = Curve.Length;
var tickDistance = Math.Min(TickDistance, length);
var repeatDuration = length / Velocity;
var minDistanceFromEnd = Velocity * 0.01;
for (var repeat = 0; repeat < RepeatCount; repeat++)
{
var repeatStartTime = StartTime + repeat * repeatDuration;
var reversed = repeat % 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;
yield return new SliderTick
{
RepeatIndex = repeat,
StartTime = repeatStartTime + timeProgress * repeatDuration,
Position = Curve.PositionAt(distanceProgress),
2017-02-13 03:38:05 +08:00
StackHeight = StackHeight,
Scale = Scale,
ComboColour = ComboColour,
2017-02-13 03:38:05 +08:00
Sample = new HitSampleInfo
{
Type = SampleType.None,
Set = SampleSet.Soft,
},
};
}
}
}
}
public override HitObjectType Type => HitObjectType.Slider;
}
}