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;
|
2017-05-23 15:11:46 +08:00
|
|
|
using osu.Framework.Audio.Track;
|
2017-05-17 14:14:04 +08:00
|
|
|
using osu.Framework.Configuration;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Game.Beatmaps;
|
2017-05-23 12:55:18 +08:00
|
|
|
using osu.Game.Beatmaps.ControlPoints;
|
2017-05-17 14:14:04 +08:00
|
|
|
|
|
|
|
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-22 16:01:43 +08:00
|
|
|
|
2017-05-19 09:19:00 +08:00
|
|
|
private int lastBeat;
|
2017-05-23 12:55:18 +08:00
|
|
|
private TimingControlPoint lastTimingPoint;
|
2017-05-17 14:14:04 +08:00
|
|
|
|
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;
|
|
|
|
|
2017-05-17 14:14:04 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
2017-05-24 00:44:47 +08:00
|
|
|
if (Beatmap.Value?.Track == null)
|
2017-05-22 15:56:40 +08:00
|
|
|
return;
|
|
|
|
|
2017-05-24 00:44:47 +08:00
|
|
|
double currentTrackTime = Beatmap.Value.Track.CurrentTime + EarlyActivationMilliseconds;
|
2017-05-23 11:29:43 +08:00
|
|
|
|
2017-05-24 00:44:47 +08:00
|
|
|
TimingControlPoint timingPoint = Beatmap.Value.Beatmap.ControlPointInfo.TimingPointAt(currentTrackTime);
|
|
|
|
EffectControlPoint effectPoint = Beatmap.Value.Beatmap.ControlPointInfo.EffectPointAt(currentTrackTime);
|
2017-05-22 15:56:40 +08:00
|
|
|
|
2017-05-23 12:55:18 +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
|
|
|
|
2017-05-22 16:15:23 +08:00
|
|
|
// The beats before the start of the first control point are off by 1, this should do the trick
|
2017-05-23 12:55:18 +08:00
|
|
|
if (currentTrackTime < timingPoint.Time)
|
2017-05-23 15:11:46 +08:00
|
|
|
beatIndex--;
|
2017-05-22 15:56:40 +08:00
|
|
|
|
2017-05-23 15:11:46 +08:00
|
|
|
if (timingPoint == lastTimingPoint && beatIndex == lastBeat)
|
2017-05-22 16:15:23 +08:00
|
|
|
return;
|
|
|
|
|
2017-05-23 12:55:18 +08:00
|
|
|
double offsetFromBeat = (timingPoint.Time - currentTrackTime) % timingPoint.BeatLength;
|
2017-05-22 16:15:23 +08:00
|
|
|
|
2017-05-22 19:01:28 +08:00
|
|
|
using (BeginDelayedSequence(offsetFromBeat, true))
|
2017-05-24 00:44:47 +08:00
|
|
|
OnNewBeat(beatIndex, timingPoint, effectPoint, Beatmap.Value.Track.CurrentAmplitudes);
|
2017-05-22 16:15:23 +08:00
|
|
|
|
2017-05-23 15:11:46 +08:00
|
|
|
lastBeat = beatIndex;
|
2017-05-23 12:55:18 +08:00
|
|
|
lastTimingPoint = timingPoint;
|
2017-05-17 14:14:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuGameBase game)
|
|
|
|
{
|
2017-05-24 00:44:47 +08:00
|
|
|
Beatmap.BindTo(game.Beatmap);
|
2017-05-17 14:14:04 +08:00
|
|
|
}
|
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
|
|
|
{
|
|
|
|
}
|
2017-05-17 14:14:04 +08:00
|
|
|
}
|
2017-05-17 22:23:04 +08:00
|
|
|
}
|