1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-07 01:07:24 +08:00
osu-lazer/osu.Game/Rulesets/Mods/ModTimeRamp.cs

92 lines
2.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.Timing;
using osu.Game.Beatmaps;
using osu.Game.Rulesets.UI;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Types;
2019-03-06 13:14:41 +08:00
using osuTK;
namespace osu.Game.Rulesets.Mods
{
public abstract class ModTimeRamp : Mod
{
public override Type[] IncompatibleMods => new[] { typeof(ModTimeAdjust) };
2019-03-06 13:14:41 +08:00
protected abstract double FinalRateAdjustment { get; }
}
public abstract class ModTimeRamp<T> : ModTimeRamp, IUpdatableByPlayfield, IApplicableToClock, IApplicableToBeatmap<T>
where T : HitObject
{
2019-03-06 13:14:41 +08:00
private double finalRateTime;
private double beginRampTime;
private IAdjustableClock clock;
2019-03-06 13:14:41 +08:00
/// <summary>
/// The point in the beatmap at which the final ramping rate should be reached.
/// </summary>
private const double final_rate_progress = 0.75f;
public virtual void ApplyToClock(IAdjustableClock clock)
{
2019-03-06 13:14:41 +08:00
this.clock = clock;
lastAdjust = 1;
// for preview purposes. during gameplay, Update will overwrite this setting.
applyAdjustment(1);
}
public virtual void ApplyToBeatmap(Beatmap<T> beatmap)
{
2019-03-06 13:14:41 +08:00
HitObject lastObject = beatmap.HitObjects.LastOrDefault();
beginRampTime = beatmap.HitObjects.FirstOrDefault()?.StartTime ?? 0;
finalRateTime = final_rate_progress * ((lastObject as IHasEndTime)?.EndTime ?? lastObject?.StartTime ?? 0);
}
public virtual void Update(Playfield playfield)
{
2019-03-13 20:37:33 +08:00
applyAdjustment((clock.CurrentTime - beginRampTime) / finalRateTime);
}
private double lastAdjust = 1;
2019-03-13 20:37:33 +08:00
/// <summary>
/// Adjust the rate along the specified ramp
/// </summary>
/// <param name="amount">The amount of adjustment to apply (from 0..1).</param>
private void applyAdjustment(double amount)
{
double adjust = 1 + (Math.Sign(FinalRateAdjustment) * MathHelper.Clamp(amount, 0, 1) * Math.Abs(FinalRateAdjustment));
switch (clock)
{
case IHasPitchAdjust pitch:
pitch.PitchAdjust /= lastAdjust;
pitch.PitchAdjust *= adjust;
break;
2019-04-01 11:44:46 +08:00
case IHasTempoAdjust tempo:
tempo.TempoAdjust /= lastAdjust;
tempo.TempoAdjust *= adjust;
break;
2019-04-01 11:44:46 +08:00
default:
clock.Rate /= lastAdjust;
clock.Rate *= adjust;
break;
}
lastAdjust = adjust;
}
}
2019-03-06 13:14:41 +08:00
}