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

87 lines
3.3 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 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;
2017-05-23 15:11:46 +08:00
using osu.Framework.Audio.Track;
using osu.Framework.Configuration;
using osu.Framework.Graphics.Containers;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints;
namespace osu.Game.Graphics.Containers
{
public class BeatSyncedContainer : Container
{
2017-05-24 00:44:47 +08:00
protected readonly Bindable<WorkingBeatmap> Beatmap = new Bindable<WorkingBeatmap>();
2017-05-19 09:19:00 +08:00
private int lastBeat;
private TimingControlPoint lastTimingPoint;
2017-05-23 11:29:43 +08:00
/// <summary>
2017-05-24 00:17:09 +08:00
/// The amount of time before a beat we should fire <see cref="OnNewBeat(int, TimingControlPoint, EffectControlPoint, TrackAmplitudes)"/>.
2017-05-23 11:29:43 +08:00
/// This allows for adding easing to animations that may be synchronised to the beat.
/// </summary>
protected double EarlyActivationMilliseconds;
/// <summary>
/// The time in milliseconds until the next beat.
/// </summary>
public double TimeUntilNextBeat { get; private set; }
/// <summary>
/// The time in milliseconds since the last beat
/// </summary>
public double TimeSinceLastBeat { get; private set; }
protected override void Update()
{
if (!Beatmap.Value.TrackLoaded || !Beatmap.Value.BeatmapLoaded) return;
var track = Beatmap.Value.Track;
var beatmap = Beatmap.Value.Beatmap;
if (track == null || beatmap == null)
2017-05-22 15:56:40 +08:00
return;
double currentTrackTime = track.Length > 0 ? track.CurrentTime + EarlyActivationMilliseconds : Clock.CurrentTime;
2017-05-23 11:29:43 +08:00
2017-11-21 11:29:46 +08:00
TimingControlPoint timingPoint = beatmap.ControlPointInfo.TimingPointAt(currentTrackTime);
EffectControlPoint effectPoint = beatmap.ControlPointInfo.EffectPointAt(currentTrackTime);
2017-05-22 15:56:40 +08:00
if (timingPoint.BeatLength == 0)
2017-05-22 19:02:02 +08:00
return;
2017-05-23 15:11:46 +08:00
int beatIndex = (int)((currentTrackTime - timingPoint.Time) / timingPoint.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 < timingPoint.Time)
2017-05-23 15:11:46 +08:00
beatIndex--;
2017-05-22 15:56:40 +08:00
TimeUntilNextBeat = (timingPoint.Time - currentTrackTime) % timingPoint.BeatLength;
if (TimeUntilNextBeat < 0)
TimeUntilNextBeat += timingPoint.BeatLength;
TimeSinceLastBeat = timingPoint.BeatLength - TimeUntilNextBeat;
if (timingPoint.Equals(lastTimingPoint) && beatIndex == lastBeat)
return;
using (BeginDelayedSequence(-TimeSinceLastBeat, true))
2017-11-21 11:29:46 +08:00
OnNewBeat(beatIndex, timingPoint, effectPoint, track.CurrentAmplitudes);
2017-05-23 15:11:46 +08:00
lastBeat = beatIndex;
lastTimingPoint = timingPoint;
}
[BackgroundDependencyLoader]
private void load(OsuGameBase game)
{
2017-05-24 00:44:47 +08:00
Beatmap.BindTo(game.Beatmap);
}
2017-05-22 19:06:37 +08:00
2017-05-23 15:11:46 +08:00
protected virtual void OnNewBeat(int beatIndex, TimingControlPoint timingPoint, EffectControlPoint effectPoint, TrackAmplitudes amplitudes)
2017-05-22 19:06:37 +08:00
{
}
}
}