1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 07:23:22 +08:00
osu-lazer/osu.Game/Rulesets/Objects/BarLineGenerator.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

77 lines
2.9 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.
2020-05-18 04:08:49 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
2020-01-09 12:43:44 +08:00
using osu.Framework.Utils;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
namespace osu.Game.Rulesets.Objects
{
public class BarLineGenerator<TBarLine>
where TBarLine : class, IBarLine, new()
{
/// <summary>
/// The generated bar lines.
/// </summary>
public readonly List<TBarLine> BarLines = new List<TBarLine>();
/// <summary>
/// Constructs and generates bar lines for provided beatmap.
/// </summary>
/// <param name="beatmap">The beatmap to generate bar lines for.</param>
public BarLineGenerator(IBeatmap beatmap)
{
if (beatmap.HitObjects.Count == 0)
return;
HitObject lastObject = beatmap.HitObjects.Last();
2019-11-25 21:37:07 +08:00
double lastHitTime = 1 + lastObject.GetEndTime();
var timingPoints = beatmap.ControlPointInfo.TimingPoints;
if (timingPoints.Count == 0)
return;
for (int i = 0; i < timingPoints.Count; i++)
{
TimingControlPoint currentTimingPoint = timingPoints[i];
2022-10-10 21:34:29 +08:00
EffectControlPoint currentEffectPoint = beatmap.ControlPointInfo.EffectPointAt(currentTimingPoint.Time);
int currentBeat = 0;
// Stop on the next timing point, or if there is no next timing point stop slightly past the last object
2022-10-08 02:34:31 +08:00
double endTime = i < timingPoints.Count - 1 ? timingPoints[i + 1].Time : lastHitTime + currentTimingPoint.BeatLength * currentTimingPoint.TimeSignature.Numerator;
2022-10-08 02:38:32 +08:00
double startTime = currentTimingPoint.Time;
double barLength = currentTimingPoint.BeatLength * currentTimingPoint.TimeSignature.Numerator;
2022-10-10 21:34:29 +08:00
if (currentEffectPoint.OmitFirstBarLine)
2022-10-08 02:38:32 +08:00
{
startTime += barLength;
}
for (double t = startTime; Precision.AlmostBigger(endTime, t); t += barLength, currentBeat++)
{
double roundedTime = Math.Round(t, MidpointRounding.AwayFromZero);
2020-05-18 04:08:49 +08:00
// in the case of some bar lengths, rounding errors can cause t to be slightly less than
// the expected whole number value due to floating point inaccuracies.
// if this is the case, apply rounding.
if (Precision.AlmostEquals(t, roundedTime))
{
t = roundedTime;
}
BarLines.Add(new TBarLine
{
StartTime = t,
Major = currentBeat % currentTimingPoint.TimeSignature.Numerator == 0
});
}
}
}
}
}