osu/osu.Game/Graphics/Containers/BeatSyncedContainer.cs

60 lines
2.2 KiB
C#
Raw Normal View History

2017-05-19 01:19:00 +00:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
2017-05-17 06:18:56 +00:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics.Containers;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Timing;
namespace osu.Game.Graphics.Containers
{
public class BeatSyncedContainer : Container
{
private Bindable<WorkingBeatmap> beatmap;
2017-05-19 01:19:00 +00:00
private int lastBeat;
private double lastTimingPointStart;
//This is to avoid sending new beats when not at the very start of the beat
private const int seek_tolerance = 20;
private const double min_beat_length = 1E-100;
protected override void Update()
{
2017-05-22 07:56:40 +00:00
if (beatmap.Value == null)
return;
double trackCurrentTime = beatmap.Value.Track.CurrentTime;
ControlPoint kiaiControlPoint;
ControlPoint controlPoint = beatmap.Value.Beatmap.TimingInfo.TimingPointAt(trackCurrentTime, out kiaiControlPoint);
if (controlPoint == null)
return;
2017-05-22 07:59:44 +00:00
bool kiai = (controlPoint ?? controlPoint).KiaiMode;
2017-05-22 07:56:40 +00:00
double beatLength = controlPoint.BeatLength;
double timingPointStart = controlPoint.Time;
int beat = beatLength > min_beat_length ? (int)((trackCurrentTime - timingPointStart) / beatLength) : 0;
//The beats before the start of the first control point are off by 1, this should do the trick
if (trackCurrentTime < timingPointStart)
beat--;
if ((timingPointStart != lastTimingPointStart || beat != lastBeat) && (int)((trackCurrentTime - timingPointStart) % beatLength) <= seek_tolerance)
OnNewBeat(beat, beatLength, controlPoint.TimeSignature, kiai);
lastBeat = beat;
lastTimingPointStart = timingPointStart;
}
protected virtual void OnNewBeat(int newBeat, double beatLength, TimeSignatures timeSignature, bool kiai)
{
}
[BackgroundDependencyLoader]
private void load(OsuGameBase game)
{
beatmap = game.Beatmap;
}
}
}