2019-01-26 20:15:19 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
|
|
|
using System;
|
2019-03-06 14:14:41 +09:00
|
|
|
using System.Linq;
|
2019-12-09 19:41:31 +09:00
|
|
|
using osu.Framework.Audio;
|
2019-12-09 17:34:04 +09:00
|
|
|
using osu.Framework.Audio.Track;
|
2019-12-09 19:41:31 +09:00
|
|
|
using osu.Framework.Bindables;
|
2020-09-29 14:09:51 +09:00
|
|
|
using osu.Framework.Graphics.Audio;
|
2019-01-26 20:15:19 +08:00
|
|
|
using osu.Game.Beatmaps;
|
2019-12-09 19:58:51 +09:00
|
|
|
using osu.Game.Configuration;
|
2019-01-26 20:15:19 +08:00
|
|
|
using osu.Game.Rulesets.Objects;
|
2020-09-29 14:09:51 +09:00
|
|
|
using osu.Game.Rulesets.UI;
|
2019-01-26 20:15:19 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Mods
|
|
|
|
{
|
2021-01-31 17:43:16 +01:00
|
|
|
public abstract class ModTimeRamp : Mod, IUpdatableByPlayfield, IApplicableToBeatmap, IApplicableToRate
|
2019-01-26 20:15:19 +08:00
|
|
|
{
|
2019-08-01 12:35:17 +09:00
|
|
|
/// <summary>
|
|
|
|
/// The point in the beatmap at which the final ramping rate should be reached.
|
|
|
|
/// </summary>
|
2021-01-31 20:18:12 +01:00
|
|
|
public const double FINAL_RATE_PROGRESS = 0.75f;
|
2019-08-01 12:35:17 +09:00
|
|
|
|
2020-01-27 07:56:18 +08:00
|
|
|
[SettingSource("Initial rate", "The starting speed of the track")]
|
|
|
|
public abstract BindableNumber<double> InitialRate { get; }
|
|
|
|
|
2019-12-09 19:58:51 +09:00
|
|
|
[SettingSource("Final rate", "The final speed to ramp to")]
|
|
|
|
public abstract BindableNumber<double> FinalRate { get; }
|
2019-01-26 20:15:19 +08:00
|
|
|
|
2020-06-13 16:32:43 +01:00
|
|
|
[SettingSource("Adjust pitch", "Should pitch be adjusted with speed")]
|
2020-06-13 15:16:27 +01:00
|
|
|
public abstract BindableBool AdjustPitch { get; }
|
|
|
|
|
2021-02-23 18:10:03 +00:00
|
|
|
public override Type[] IncompatibleMods => new[] { typeof(ModRateAdjust) };
|
|
|
|
|
2020-03-23 12:54:08 -04:00
|
|
|
public override string SettingDescription => $"{InitialRate.Value:N2}x to {FinalRate.Value:N2}x";
|
2020-03-18 23:43:26 -04:00
|
|
|
|
2019-03-06 14:14:41 +09:00
|
|
|
private double finalRateTime;
|
|
|
|
private double beginRampTime;
|
|
|
|
|
2019-12-09 19:41:31 +09:00
|
|
|
public BindableNumber<double> SpeedChange { get; } = new BindableDouble
|
2019-01-26 20:15:19 +08:00
|
|
|
{
|
2019-12-09 19:41:31 +09:00
|
|
|
Default = 1,
|
|
|
|
Value = 1,
|
|
|
|
Precision = 0.01,
|
|
|
|
};
|
2019-03-06 14:14:41 +09:00
|
|
|
|
2020-08-05 21:10:38 +09:00
|
|
|
private ITrack track;
|
2019-03-12 18:15:18 +09:00
|
|
|
|
2019-12-12 23:33:18 +09:00
|
|
|
protected ModTimeRamp()
|
2019-12-12 17:28:31 +09:00
|
|
|
{
|
|
|
|
// for preview purpose at song select. eventually we'll want to be able to update every frame.
|
2021-01-31 17:43:16 +01:00
|
|
|
FinalRate.BindValueChanged(val => applyRateAdjustment(double.PositiveInfinity), true);
|
2020-06-14 18:48:49 +02:00
|
|
|
AdjustPitch.BindValueChanged(applyPitchAdjustment);
|
2019-12-12 17:28:31 +09:00
|
|
|
}
|
|
|
|
|
2020-08-06 18:25:34 +09:00
|
|
|
public void ApplyToTrack(ITrack track)
|
2019-12-09 19:41:31 +09:00
|
|
|
{
|
2020-08-05 21:10:38 +09:00
|
|
|
this.track = track;
|
2020-06-13 15:16:27 +01:00
|
|
|
|
2019-12-12 17:45:11 +09:00
|
|
|
FinalRate.TriggerChange();
|
2020-06-13 16:32:43 +01:00
|
|
|
AdjustPitch.TriggerChange();
|
2019-01-26 20:15:19 +08:00
|
|
|
}
|
|
|
|
|
2020-09-29 14:09:51 +09:00
|
|
|
public void ApplyToSample(DrawableSample sample)
|
2020-06-16 14:58:23 +01:00
|
|
|
{
|
|
|
|
sample.AddAdjustment(AdjustableProperty.Frequency, SpeedChange);
|
|
|
|
}
|
|
|
|
|
2019-08-01 12:35:17 +09:00
|
|
|
public virtual void ApplyToBeatmap(IBeatmap beatmap)
|
2019-01-26 20:15:19 +08:00
|
|
|
{
|
2019-12-09 19:58:51 +09:00
|
|
|
SpeedChange.SetDefault();
|
|
|
|
|
2021-01-31 20:34:56 +01:00
|
|
|
double firstObjectStart = beatmap.HitObjects.FirstOrDefault()?.StartTime ?? 0;
|
|
|
|
double lastObjectEnd = beatmap.HitObjects.LastOrDefault()?.GetEndTime() ?? 0;
|
|
|
|
|
|
|
|
beginRampTime = firstObjectStart;
|
|
|
|
finalRateTime = firstObjectStart + FINAL_RATE_PROGRESS * (lastObjectEnd - firstObjectStart);
|
2019-01-26 20:15:19 +08:00
|
|
|
}
|
|
|
|
|
2021-01-31 17:43:16 +01:00
|
|
|
public double ApplyToRate(double time, double rate = 1)
|
|
|
|
{
|
|
|
|
double amount = (time - beginRampTime) / Math.Max(1, finalRateTime - beginRampTime);
|
|
|
|
double ramp = InitialRate.Value + (FinalRate.Value - InitialRate.Value) * Math.Clamp(amount, 0, 1);
|
2021-02-07 19:02:09 +01:00
|
|
|
|
|
|
|
// round the end result to match the bindable SpeedChange's precision, in case this is called externally.
|
|
|
|
return rate * Math.Round(ramp, 2);
|
2021-01-31 17:43:16 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 20:15:19 +08:00
|
|
|
public virtual void Update(Playfield playfield)
|
|
|
|
{
|
2021-01-31 17:43:16 +01:00
|
|
|
applyRateAdjustment(track.CurrentTime);
|
2019-03-12 18:15:18 +09:00
|
|
|
}
|
|
|
|
|
2019-03-14 12:29:16 +09:00
|
|
|
/// <summary>
|
2021-01-31 17:43:16 +01:00
|
|
|
/// Adjust the rate along the specified ramp.
|
2019-03-14 12:29:16 +09:00
|
|
|
/// </summary>
|
2021-01-31 17:43:16 +01:00
|
|
|
private void applyRateAdjustment(double time) => SpeedChange.Value = ApplyToRate(time);
|
2020-06-13 15:16:27 +01:00
|
|
|
|
2020-06-14 18:50:07 +02:00
|
|
|
private void applyPitchAdjustment(ValueChangedEvent<bool> adjustPitchSetting)
|
2020-06-13 15:16:27 +01:00
|
|
|
{
|
|
|
|
// remove existing old adjustment
|
2020-08-12 01:41:21 +09:00
|
|
|
track?.RemoveAdjustment(adjustmentForPitchSetting(adjustPitchSetting.OldValue), SpeedChange);
|
2020-06-13 15:16:27 +01:00
|
|
|
|
2020-08-12 01:41:21 +09:00
|
|
|
track?.AddAdjustment(adjustmentForPitchSetting(adjustPitchSetting.NewValue), SpeedChange);
|
2020-06-13 15:16:27 +01:00
|
|
|
}
|
2020-06-13 16:32:43 +01:00
|
|
|
|
2020-06-14 18:50:07 +02:00
|
|
|
private AdjustableProperty adjustmentForPitchSetting(bool adjustPitchSettingValue)
|
|
|
|
=> adjustPitchSettingValue ? AdjustableProperty.Frequency : AdjustableProperty.Tempo;
|
2019-01-26 20:15:19 +08:00
|
|
|
}
|
2019-03-06 14:14:41 +09:00
|
|
|
}
|