Skip to content

Commit

Permalink
Rewrite mods to not apply adjustments outside of ApplyToTrack()
Browse files Browse the repository at this point in the history
  • Loading branch information
bdach committed May 9, 2022
1 parent 38e463d commit a54295b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
29 changes: 23 additions & 6 deletions osu.Game/Rulesets/Mods/ModAdaptiveSpeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ public class ModAdaptiveSpeed : Mod, IApplicableToRate, IApplicableToDrawableHit
Value = 1
};

private readonly BindableNumber<double> frequencyAdjustment = new BindableDouble
{
MinValue = min_allowable_rate,
MaxValue = max_allowable_rate,
Default = 1,
Value = 1
};

private readonly BindableNumber<double> tempoAdjustment = new BindableDouble
{
MinValue = min_allowable_rate,
MaxValue = max_allowable_rate,
Default = 1,
Value = 1
};

// The two constants below denote the maximum allowable range of rates that `SpeedChange` can take.
// The range is purposefully wider than the range of values that `InitialRate` allows
// in order to give some leeway for change even when extreme initial rates are chosen.
Expand All @@ -79,7 +95,6 @@ public class ModAdaptiveSpeed : Mod, IApplicableToRate, IApplicableToDrawableHit
// Apply a fixed rate change when missing, allowing the player to catch up when the rate is too fast.
private const double rate_change_on_miss = 0.95d;

private ITrack track;
private double targetRate = 1d;

/// <summary>
Expand Down Expand Up @@ -143,7 +158,8 @@ public ModAdaptiveSpeed()

public void ApplyToTrack(ITrack track)
{
this.track = track;
track.AddAdjustment(AdjustableProperty.Frequency, frequencyAdjustment);
track.AddAdjustment(AdjustableProperty.Tempo, tempoAdjustment);

InitialRate.TriggerChange();
AdjustPitch.TriggerChange();
Expand Down Expand Up @@ -209,13 +225,14 @@ public void ApplyToBeatmap(IBeatmap beatmap)

private void adjustPitchChanged(ValueChangedEvent<bool> adjustPitchSetting)
{
track?.RemoveAdjustment(adjustmentForPitchSetting(adjustPitchSetting.OldValue), SpeedChange);
adjustmentForPitchSetting(adjustPitchSetting.OldValue).UnbindFrom(SpeedChange);
adjustmentForPitchSetting(adjustPitchSetting.OldValue).SetDefault();

track?.AddAdjustment(adjustmentForPitchSetting(adjustPitchSetting.NewValue), SpeedChange);
adjustmentForPitchSetting(adjustPitchSetting.NewValue).BindTo(SpeedChange);
}

private AdjustableProperty adjustmentForPitchSetting(bool adjustPitchSettingValue)
=> adjustPitchSettingValue ? AdjustableProperty.Frequency : AdjustableProperty.Tempo;
private BindableNumber<double> adjustmentForPitchSetting(bool adjustPitchSettingValue)
=> adjustPitchSettingValue ? frequencyAdjustment : tempoAdjustment;

private IEnumerable<HitObject> getAllApplicableHitObjects(IEnumerable<HitObject> hitObjects)
{
Expand Down
28 changes: 23 additions & 5 deletions osu.Game/Rulesets/Mods/ModTimeRamp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public abstract class ModTimeRamp : Mod, IUpdatableByPlayfield, IApplicableToBea
Precision = 0.01,
};

private readonly BindableNumber<double> frequencyAdjustment = new BindableDouble
{
Default = 1,
Value = 1,
Precision = 0.01
};

private readonly BindableNumber<double> tempoAdjustment = new BindableDouble
{
Default = 1,
Value = 1,
Precision = 0.01
};

private ITrack track;

protected ModTimeRamp()
Expand All @@ -57,8 +71,12 @@ protected ModTimeRamp()

public void ApplyToTrack(ITrack track)
{
// stored only for the purpose of accessing track.CurrentTime.
this.track = track;

track.AddAdjustment(AdjustableProperty.Frequency, frequencyAdjustment);
track.AddAdjustment(AdjustableProperty.Tempo, tempoAdjustment);

FinalRate.TriggerChange();
AdjustPitch.TriggerChange();
}
Expand Down Expand Up @@ -100,13 +118,13 @@ public virtual void Update(Playfield playfield)

private void applyPitchAdjustment(ValueChangedEvent<bool> adjustPitchSetting)
{
// remove existing old adjustment
track?.RemoveAdjustment(adjustmentForPitchSetting(adjustPitchSetting.OldValue), SpeedChange);
adjustmentForPitchSetting(adjustPitchSetting.OldValue).UnbindFrom(SpeedChange);
adjustmentForPitchSetting(adjustPitchSetting.OldValue).SetDefault();

track?.AddAdjustment(adjustmentForPitchSetting(adjustPitchSetting.NewValue), SpeedChange);
adjustmentForPitchSetting(adjustPitchSetting.NewValue).BindTo(SpeedChange);
}

private AdjustableProperty adjustmentForPitchSetting(bool adjustPitchSettingValue)
=> adjustPitchSettingValue ? AdjustableProperty.Frequency : AdjustableProperty.Tempo;
private BindableNumber<double> adjustmentForPitchSetting(bool adjustPitchSettingValue)
=> adjustPitchSettingValue ? frequencyAdjustment : tempoAdjustment;
}
}

0 comments on commit a54295b

Please sign in to comment.