1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 02:12:54 +08:00
osu-lazer/osu.Game/Rulesets/Mods/ModTimeRamp.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
3.8 KiB
C#
Raw Normal View History

// 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 13:14:41 +08:00
using System.Linq;
using osu.Framework.Audio;
using osu.Framework.Bindables;
using osu.Game.Beatmaps;
2019-12-09 18:58:51 +08:00
using osu.Game.Configuration;
using osu.Game.Overlays.Settings;
using osu.Game.Rulesets.UI;
namespace osu.Game.Rulesets.Mods
{
public abstract class ModTimeRamp : Mod, IUpdatableByPlayfield, IApplicableToBeatmap, IApplicableToRate
{
/// <summary>
/// The point in the beatmap at which the final ramping rate should be reached.
/// </summary>
2021-02-01 03:18:12 +08:00
public const double FINAL_RATE_PROGRESS = 0.75f;
public override double ScoreMultiplier => 0.5;
[SettingSource("Initial rate", "The starting speed of the track", SettingControlType = typeof(MultiplierSettingsSlider))]
public abstract BindableNumber<double> InitialRate { get; }
[SettingSource("Final rate", "The final speed to ramp to", SettingControlType = typeof(MultiplierSettingsSlider))]
2019-12-09 18:58:51 +08:00
public abstract BindableNumber<double> FinalRate { get; }
[SettingSource("Adjust pitch", "Should pitch be adjusted with speed")]
public abstract BindableBool AdjustPitch { get; }
public sealed override bool ValidForMultiplayerAsFreeMod => false;
2022-03-01 21:12:06 +08:00
public override Type[] IncompatibleMods => new[] { typeof(ModRateAdjust), typeof(ModAdaptiveSpeed) };
2020-03-24 00:54:08 +08:00
public override string SettingDescription => $"{InitialRate.Value:N2}x to {FinalRate.Value:N2}x";
2020-03-19 11:43:26 +08:00
2019-03-06 13:14:41 +08:00
private double finalRateTime;
private double beginRampTime;
2022-09-26 03:49:22 +08:00
public BindableNumber<double> SpeedChange { get; } = new BindableDouble(1)
{
Precision = 0.01,
};
2019-03-06 13:14:41 +08:00
private readonly RateAdjustModHelper rateAdjustHelper;
2019-12-12 22:33:18 +08:00
protected ModTimeRamp()
{
rateAdjustHelper = new RateAdjustModHelper(SpeedChange);
rateAdjustHelper.HandleAudioAdjustments(AdjustPitch);
// for preview purpose at song select. eventually we'll want to be able to update every frame.
2022-06-24 20:25:23 +08:00
FinalRate.BindValueChanged(_ => applyRateAdjustment(double.PositiveInfinity), true);
}
public void ApplyToTrack(IAdjustableAudioComponent track)
{
rateAdjustHelper.ApplyToTrack(track);
2019-12-12 16:45:11 +08:00
FinalRate.TriggerChange();
}
public void ApplyToSample(IAdjustableAudioComponent sample)
{
sample.AddAdjustment(AdjustableProperty.Frequency, SpeedChange);
}
public virtual void ApplyToBeatmap(IBeatmap beatmap)
{
2019-12-09 18:58:51 +08:00
SpeedChange.SetDefault();
double firstObjectStart = beatmap.HitObjects.FirstOrDefault()?.StartTime ?? 0;
2022-12-02 15:09:26 +08:00
double lastObjectEnd = beatmap.HitObjects.Any() ? beatmap.GetLastObjectTime() : 0;
beginRampTime = firstObjectStart;
finalRateTime = firstObjectStart + FINAL_RATE_PROGRESS * (lastObjectEnd - firstObjectStart);
}
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);
// round the end result to match the bindable SpeedChange's precision, in case this is called externally.
return rate * Math.Round(ramp, 2);
}
public virtual void Update(Playfield playfield)
{
applyRateAdjustment(playfield.Clock.CurrentTime);
}
/// <summary>
/// Adjust the rate along the specified ramp.
/// </summary>
private void applyRateAdjustment(double time) => SpeedChange.Value = ApplyToRate(time);
}
2019-03-06 13:14:41 +08:00
}