Code cleanup

This commit is contained in:
Henry Lin 2021-06-14 21:34:34 +08:00
parent 12a17d0983
commit 04c0db6dce
3 changed files with 105 additions and 115 deletions

View File

@ -61,7 +61,7 @@ namespace osu.Game.Rulesets.Osu.Mods
} }
// Maximum distance to jump // Maximum distance to jump
public const float MAX_DISTANCE = 250f; private const float max_distance = 250f;
public override void ApplyToBeatmap(IBeatmap beatmap) public override void ApplyToBeatmap(IBeatmap beatmap)
{ {
@ -69,30 +69,20 @@ namespace osu.Game.Rulesets.Osu.Mods
var rng = new Random(Seed.Value.GetValueOrDefault()); var rng = new Random(Seed.Value.GetValueOrDefault());
float nextSingle(float max = 1f) float nextSingle(float max = 1f) => (float)(rng.NextDouble() * max);
{
return (float)(rng.NextDouble() * max);
}
var osuBeatmap = (OsuBeatmap)beatmap; var osuBeatmap = (OsuBeatmap)beatmap;
var origHitObjects = osuBeatmap.HitObjects.OrderBy(x => x.StartTime).ToList(); var origHitObjects = osuBeatmap.HitObjects.OrderBy(x => x.StartTime).ToList();
// Only place circles between startTime and endTime // Only place circles between startTime and endTime
var startTime = origHitObjects.First().StartTime; var startTime = origHitObjects.First().StartTime;
double endTime;
var endObj = origHitObjects.Last(); var endObj = origHitObjects.Last();
switch (endObj) var endTime = endObj switch
{ {
case Slider slider: Slider slider => slider.EndTime,
endTime = slider.EndTime; Spinner spinner => spinner.EndTime,
break; _ => endObj.StartTime
case Spinner spinner: };
endTime = spinner.EndTime;
break;
default:
endTime = endObj.StartTime;
break;
}
// Generate the beats // Generate the beats
var beats = osuBeatmap.ControlPointInfo.TimingPoints var beats = osuBeatmap.ControlPointInfo.TimingPoints
@ -101,11 +91,13 @@ namespace osu.Game.Rulesets.Osu.Mods
{ {
var tpBeats = new List<double>(); var tpBeats = new List<double>();
var currentTime = tp.Time; var currentTime = tp.Time;
while (Precision.AlmostBigger(endTime, currentTime) && osuBeatmap.ControlPointInfo.TimingPointAt(currentTime) == tp) while (Precision.AlmostBigger(endTime, currentTime) && osuBeatmap.ControlPointInfo.TimingPointAt(currentTime) == tp)
{ {
tpBeats.Add(currentTime); tpBeats.Add(currentTime);
currentTime += tp.BeatLength; currentTime += tp.BeatLength;
} }
return tpBeats; return tpBeats;
}) })
// Remove beats that are before startTime // Remove beats that are before startTime
@ -122,9 +114,8 @@ namespace osu.Game.Rulesets.Osu.Mods
.Where((x, idx) => .Where((x, idx) =>
{ {
if (idx == beats.Count - 1) return true; if (idx == beats.Count - 1) return true;
if (Precision.AlmostBigger(osuBeatmap.ControlPointInfo.TimingPointAt(x).BeatLength / 2, beats[idx + 1] - x))
return false; return !Precision.AlmostBigger(osuBeatmap.ControlPointInfo.TimingPointAt(x).BeatLength / 2, beats[idx + 1] - x);
return true;
}) })
.Select(x => .Select(x =>
{ {
@ -139,6 +130,7 @@ namespace osu.Game.Rulesets.Osu.Mods
{ {
var x = hitObjects[i]; var x = hitObjects[i];
var samples = getSamplesAtTime(origHitObjects, x.StartTime); var samples = getSamplesAtTime(origHitObjects, x.StartTime);
if (samples == null) if (samples == null)
{ {
if (i > 0) if (i > 0)
@ -155,11 +147,11 @@ namespace osu.Game.Rulesets.Osu.Mods
hitObjects.ForEach(x => hitObjects.ForEach(x =>
{ {
var origObj = origHitObjects.FindLast(y => Precision.AlmostBigger(x.StartTime, y.StartTime)); var origObj = origHitObjects.FindLast(y => Precision.AlmostBigger(x.StartTime, y.StartTime));
if (origObj == null) x.ComboIndex = 0; x.ComboIndex = origObj?.ComboIndex ?? 0;
else x.ComboIndex = origObj.ComboIndex;
}); });
// Then reprocess them to ensure continuity in the combo indices and add indices in current combo // Then reprocess them to ensure continuity in the combo indices and add indices in current combo
var combos = hitObjects.GroupBy(x => x.ComboIndex).ToList(); var combos = hitObjects.GroupBy(x => x.ComboIndex).ToList();
for (int i = 0; i < combos.Count; i++) for (int i = 0; i < combos.Count; i++)
{ {
var group = combos[i].ToList(); var group = combos[i].ToList();
@ -176,16 +168,18 @@ namespace osu.Game.Rulesets.Osu.Mods
// Position all hit circles // Position all hit circles
var direction = MathHelper.TwoPi * nextSingle(); var direction = MathHelper.TwoPi * nextSingle();
for (int i = 0; i < hitObjects.Count; i++) for (int i = 0; i < hitObjects.Count; i++)
{ {
var x = hitObjects[i]; var x = hitObjects[i];
if (i == 0) if (i == 0)
{ {
x.Position = new Vector2(nextSingle(OsuPlayfield.BASE_SIZE.X), nextSingle(OsuPlayfield.BASE_SIZE.Y)); x.Position = new Vector2(nextSingle(OsuPlayfield.BASE_SIZE.X), nextSingle(OsuPlayfield.BASE_SIZE.Y));
} }
else else
{ {
var distance = Math.Min(MAX_DISTANCE, 40f * (float)Math.Pow(1.05, x.ComboIndex)); var distance = Math.Min(max_distance, 40f * (float)Math.Pow(1.05, x.ComboIndex));
var relativePos = new Vector2( var relativePos = new Vector2(
distance * (float)Math.Cos(direction), distance * (float)Math.Cos(direction),
distance * (float)Math.Sin(direction) distance * (float)Math.Sin(direction)
@ -209,7 +203,7 @@ namespace osu.Game.Rulesets.Osu.Mods
if (x.LastInCombo) if (x.LastInCombo)
direction = MathHelper.TwoPi * nextSingle(); direction = MathHelper.TwoPi * nextSingle();
else else
direction += distance / MAX_DISTANCE * (nextSingle() * MathHelper.TwoPi - MathHelper.Pi); direction += distance / max_distance * (nextSingle() * MathHelper.TwoPi - MathHelper.Pi);
} }
} }
@ -227,25 +221,24 @@ namespace osu.Game.Rulesets.Osu.Mods
/// <param name="hitObjects">The list of hit objects in a beatmap, ordered by StartTime</param> /// <param name="hitObjects">The list of hit objects in a beatmap, ordered by StartTime</param>
/// <param name="time">The point in time to get samples for</param> /// <param name="time">The point in time to get samples for</param>
/// <returns>Hit samples</returns> /// <returns>Hit samples</returns>
private IList<HitSampleInfo> getSamplesAtTime(List<OsuHitObject> hitObjects, double time) private IList<HitSampleInfo> getSamplesAtTime(IEnumerable<OsuHitObject> hitObjects, double time)
{ {
var sampleObj = hitObjects.FirstOrDefault(x => var sampleObj = hitObjects.FirstOrDefault(x =>
{ {
if (Precision.AlmostEquals(time, x.StartTime)) return true; if (Precision.AlmostEquals(time, x.StartTime)) return true;
if (x is Slider slider
&& Precision.AlmostBigger(time, slider.StartTime) if (!(x is Slider s))
&& Precision.AlmostBigger(slider.EndTime, time))
{
if (Precision.AlmostEquals((time - slider.StartTime) % slider.SpanDuration, 0))
{
return true;
}
}
return false; return false;
if (!Precision.AlmostBigger(time, s.StartTime)
|| !Precision.AlmostBigger(s.EndTime, time))
return false;
return Precision.AlmostEquals((time - s.StartTime) % s.SpanDuration, 0);
}); });
if (sampleObj == null) return null; if (sampleObj == null) return null;
IList<HitSampleInfo> samples = null; IList<HitSampleInfo> samples;
if (sampleObj is Slider slider) if (sampleObj is Slider slider)
{ {
samples = slider.NodeSamples[(int)Math.Round((time - slider.StartTime) % slider.SpanDuration)]; samples = slider.NodeSamples[(int)Math.Round((time - slider.StartTime) % slider.SpanDuration)];
@ -254,6 +247,7 @@ namespace osu.Game.Rulesets.Osu.Mods
{ {
samples = sampleObj.Samples; samples = sampleObj.Samples;
} }
return samples; return samples;
} }
@ -269,8 +263,8 @@ namespace osu.Game.Rulesets.Osu.Mods
var h = (OsuHitObject)drawable.HitObject; var h = (OsuHitObject)drawable.HitObject;
// apply grow and fade effect // apply grow and fade effect
if (drawable is DrawableHitCircle circle) if (!(drawable is DrawableHitCircle circle)) return;
{
using (drawable.BeginAbsoluteSequence(h.StartTime - h.TimePreempt)) using (drawable.BeginAbsoluteSequence(h.StartTime - h.TimePreempt))
{ {
// todo: this doesn't feel quite right yet // todo: this doesn't feel quite right yet
@ -283,7 +277,6 @@ namespace osu.Game.Rulesets.Osu.Mods
circle.ApproachCircle.Hide(); circle.ApproachCircle.Hide();
} }
} }
}
public void ReadFromDifficulty(BeatmapDifficulty difficulty) public void ReadFromDifficulty(BeatmapDifficulty difficulty)
{ {
@ -347,7 +340,7 @@ namespace osu.Game.Rulesets.Osu.Mods
} }
/// <summary> /// <summary>
/// Rotates vector "initial" towards vector "destinantion" /// Rotates vector "initial" towards vector "destination"
/// </summary> /// </summary>
/// <param name="initial">Vector to rotate to "destination"</param> /// <param name="initial">Vector to rotate to "destination"</param>
/// <param name="destination">Vector "initial" should be rotated to</param> /// <param name="destination">Vector "initial" should be rotated to</param>
@ -399,7 +392,7 @@ namespace osu.Game.Rulesets.Osu.Mods
{ {
InternalChildren = new Drawable[] InternalChildren = new Drawable[]
{ {
sample = new PausableSkinnableSound(new SampleInfo("spinnerbonus")), // todo: use another sample? sample = new PausableSkinnableSound(new SampleInfo("spinnerbonus")) // todo: use another sample?
}; };
} }

View File

@ -5,7 +5,6 @@ using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using osu.Game.Configuration;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.Settings; using osu.Game.Overlays.Settings;
@ -18,11 +17,14 @@ namespace osu.Game.Rulesets.Mods
public class SeedSettingsControl : SettingsItem<int?> public class SeedSettingsControl : SettingsItem<int?>
{ {
protected override Drawable CreateControl() => new SeedControl protected override Drawable CreateControl()
{
return new SeedControl
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Margin = new MarginPadding { Top = 5 } Margin = new MarginPadding { Top = 5 }
}; };
}
private sealed class SeedControl : CompositeDrawable, IHasCurrentValue<int?> private sealed class SeedControl : CompositeDrawable, IHasCurrentValue<int?>
{ {

View File

@ -2,14 +2,9 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.Settings;
namespace osu.Game.Rulesets.Mods namespace osu.Game.Rulesets.Mods
{ {