1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 03:22:55 +08:00

Add a base container with beat syncing logic

This commit is contained in:
ColdVolcano 2017-05-17 01:14:04 -05:00
parent 12716d2ab4
commit 0801a211da
2 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,64 @@
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;
private int beat;
private double timingPointStart;
//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;
public BeatSyncedContainer()
{
}
protected override void Update()
{
if (beatmap.Value != null)
{
double currentTime = beatmap.Value.Track.CurrentTime;
ControlPoint kiaiControlPoint;
ControlPoint controlPoint = beatmap.Value.Beatmap.TimingInfo.TimingPointAt(currentTime, out kiaiControlPoint);
if (controlPoint != null)
{
double oldTimingPointStart = timingPointStart;
double beatLenght = double.MinValue;
int oldBeat = beat;
bool kiai = false;
beatLenght = controlPoint.BeatLength;
timingPointStart = controlPoint.Time;
kiai = kiaiControlPoint?.KiaiMode ?? false;
beat = beatLenght > min_beat_length ? (int)((currentTime - timingPointStart) / beatLenght) : 0;
//should we handle negative beats? (before the start of the controlPoint)
//The beats before the start of the first control point are off by 1, this should do the trick
if (currentTime <= timingPointStart)
beat--;
if ((timingPointStart != oldTimingPointStart || beat != oldBeat) && (int)((currentTime - timingPointStart) % (beatLenght)) <= seek_tolerance)
OnNewBeat(beat, controlPoint.BeatLength, controlPoint.TimeSignature, kiai);
}
}
}
protected virtual void OnNewBeat(int newBeat, double beatLength, TimeSignatures timeSignature, bool kiai)
{
}
[BackgroundDependencyLoader]
private void load(OsuGameBase game)
{
beatmap = game.Beatmap;
}
}
}

View File

@ -74,6 +74,7 @@
<Compile Include="Audio\SampleInfoList.cs" />
<Compile Include="Beatmaps\Drawables\BeatmapBackgroundSprite.cs" />
<Compile Include="Beatmaps\DifficultyCalculator.cs" />
<Compile Include="Graphics\Containers\BeatSyncedContainer.cs" />
<Compile Include="Online\API\Requests\PostMessageRequest.cs" />
<Compile Include="Online\Chat\ErrorMessage.cs" />
<Compile Include="Overlays\Chat\ChatTabControl.cs" />