1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 19:27:31 +08:00
osu-lazer/osu.Game/Graphics/Containers/BeatSyncedContainer.cs

68 lines
2.4 KiB
C#
Raw Normal View History

2017-05-19 09:19:00 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
2017-05-17 14:18:56 +08: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
{
2017-05-22 16:50:05 +08:00
private readonly Bindable<WorkingBeatmap> beatmap = new Bindable<WorkingBeatmap>();
2017-05-19 09:19:00 +08:00
private int lastBeat;
private ControlPoint lastControlPoint;
2017-05-23 11:29:43 +08:00
/// <summary>
/// The amount of time before a beat we should fire <see cref="OnNewBeat(int, double, TimeSignatures, bool)"/>.
/// This allows for adding easing to animations that may be synchronised to the beat.
/// </summary>
protected double EarlyActivationMilliseconds;
protected override void Update()
{
if (beatmap.Value?.Track == null)
2017-05-22 15:56:40 +08:00
return;
2017-05-23 11:29:43 +08:00
double currentTrackTime = beatmap.Value.Track.CurrentTime + EarlyActivationMilliseconds;
2017-05-22 16:00:53 +08:00
ControlPoint overridePoint;
ControlPoint controlPoint = beatmap.Value.Beatmap.TimingInfo.TimingPointAt(currentTrackTime, out overridePoint);
2017-05-22 15:56:40 +08:00
2017-05-22 19:02:02 +08:00
if (controlPoint.BeatLength == 0)
return;
2017-05-22 16:00:53 +08:00
bool kiai = (overridePoint ?? controlPoint).KiaiMode;
2017-05-22 19:02:02 +08:00
int beat = (int)((currentTrackTime - controlPoint.Time) / controlPoint.BeatLength);
2017-05-22 15:59:44 +08:00
// The beats before the start of the first control point are off by 1, this should do the trick
if (currentTrackTime < controlPoint.Time)
2017-05-22 15:56:40 +08:00
beat--;
if (controlPoint == lastControlPoint && beat == lastBeat)
return;
double offsetFromBeat = (controlPoint.Time - currentTrackTime) % controlPoint.BeatLength;
using (BeginDelayedSequence(offsetFromBeat, true))
OnNewBeat(beat, controlPoint.BeatLength, controlPoint.TimeSignature, kiai);
2017-05-22 15:56:40 +08:00
lastBeat = beat;
lastControlPoint = controlPoint;
}
[BackgroundDependencyLoader]
private void load(OsuGameBase game)
{
beatmap.BindTo(game.Beatmap);
}
2017-05-22 19:06:37 +08:00
protected virtual void OnNewBeat(int newBeat, double beatLength, TimeSignatures timeSignature, bool kiai)
{
}
}
}