1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-21 21:32:55 +08:00

Use pattern matching.

This commit is contained in:
Huo Yaoyuan 2019-11-12 18:16:51 +08:00
parent 7d7b9e36b2
commit e5e8e70704
13 changed files with 348 additions and 348 deletions

View File

@ -145,8 +145,8 @@ dotnet_style_prefer_compound_assignment = true:warning
#Style - null/type checks
dotnet_style_coalesce_expression = true:warning
dotnet_style_null_propagation = true:warning
csharp_style_pattern_matching_over_is_with_cast_check = true:silent
csharp_style_pattern_matching_over_as_with_null_check = true:silent
csharp_style_pattern_matching_over_is_with_cast_check = true:warning
csharp_style_pattern_matching_over_as_with_null_check = true:warning
csharp_style_throw_expression = true:silent
csharp_style_conditional_delegate_call = true:suggestion

View File

@ -8,6 +8,7 @@ using System;
using osu.Game.Rulesets.Catch.UI;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Rulesets.Objects;
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Game.Rulesets.Catch.Beatmaps
{
@ -22,15 +23,13 @@ namespace osu.Game.Rulesets.Catch.Beatmaps
protected override IEnumerable<CatchHitObject> ConvertHitObject(HitObject obj, IBeatmap beatmap)
{
var curveData = obj as IHasCurve;
var positionData = obj as IHasXPosition;
var comboData = obj as IHasCombo;
var endTime = obj as IHasEndTime;
var legacyOffset = obj as IHasLegacyLastTickOffset;
if (curveData != null)
switch (obj)
{
yield return new JuiceStream
case IHasCurve curveData:
return new JuiceStream
{
StartTime = obj.StartTime,
Samples = obj.Samples,
@ -40,30 +39,28 @@ namespace osu.Game.Rulesets.Catch.Beatmaps
X = (positionData?.X ?? 0) / CatchPlayfield.BASE_WIDTH,
NewCombo = comboData?.NewCombo ?? false,
ComboOffset = comboData?.ComboOffset ?? 0,
LegacyLastTickOffset = legacyOffset?.LegacyLastTickOffset ?? 0
};
}
else if (endTime != null)
{
yield return new BananaShower
LegacyLastTickOffset = (obj as IHasLegacyLastTickOffset)?.LegacyLastTickOffset ?? 0
}.Yield();
case IHasEndTime endTime:
return new BananaShower
{
StartTime = obj.StartTime,
Samples = obj.Samples,
Duration = endTime.Duration,
NewCombo = comboData?.NewCombo ?? false,
ComboOffset = comboData?.ComboOffset ?? 0,
};
}
else
{
yield return new Fruit
}.Yield();
default:
return new Fruit
{
StartTime = obj.StartTime,
Samples = obj.Samples,
NewCombo = comboData?.NewCombo ?? false,
ComboOffset = comboData?.ComboOffset ?? 0,
X = (positionData?.X ?? 0) / CatchPlayfield.BASE_WIDTH
};
}.Yield();
}
}

View File

@ -156,37 +156,44 @@ namespace osu.Game.Rulesets.Mania.Beatmaps
/// <returns>The hit objects generated.</returns>
private IEnumerable<ManiaHitObject> generateConverted(HitObject original, IBeatmap originalBeatmap)
{
var endTimeData = original as IHasEndTime;
var distanceData = original as IHasDistance;
var positionData = original as IHasPosition;
Patterns.PatternGenerator conversion = null;
if (distanceData != null)
switch (original)
{
case IHasDistance _:
{
var generator = new DistanceObjectPatternGenerator(Random, original, beatmap, lastPattern, originalBeatmap);
conversion = generator;
var positionData = original as IHasPosition;
for (double time = original.StartTime; !Precision.DefinitelyBigger(time, generator.EndTime); time += generator.SegmentDuration)
{
recordNote(time, positionData?.Position ?? Vector2.Zero);
computeDensity(time);
}
break;
}
else if (endTimeData != null)
case IHasEndTime endTimeData:
{
conversion = new EndTimeObjectPatternGenerator(Random, original, beatmap, originalBeatmap);
recordNote(endTimeData.EndTime, new Vector2(256, 192));
computeDensity(endTimeData.EndTime);
break;
}
else if (positionData != null)
case IHasPosition positionData:
{
computeDensity(original.StartTime);
conversion = new HitObjectPatternGenerator(Random, original, beatmap, lastPattern, lastTime, lastPosition, density, lastStair, originalBeatmap);
recordNote(original.StartTime, positionData.Position);
break;
}
}
if (conversion == null)
@ -219,14 +226,13 @@ namespace osu.Game.Rulesets.Mania.Beatmaps
private Pattern generate()
{
var endTimeData = HitObject as IHasEndTime;
var positionData = HitObject as IHasXPosition;
int column = GetColumn(positionData?.X ?? 0);
var pattern = new Pattern();
if (endTimeData != null)
if (HitObject is IHasEndTime endTimeData)
{
pattern.Add(new HoldNote
{
@ -237,7 +243,7 @@ namespace osu.Game.Rulesets.Mania.Beatmaps
Tail = { Samples = sampleInfoListAt(endTimeData.EndTime) },
});
}
else if (positionData != null)
else if (HitObject is IHasXPosition)
{
pattern.Add(new Note
{
@ -257,16 +263,16 @@ namespace osu.Game.Rulesets.Mania.Beatmaps
/// <returns></returns>
private IList<HitSampleInfo> sampleInfoListAt(double time)
{
var curveData = HitObject as IHasCurve;
if (curveData == null)
return HitObject.Samples;
if (HitObject is IHasCurve curveData)
{
double segmentTime = (curveData.EndTime - HitObject.StartTime) / curveData.SpanCount();
int index = (int)(segmentTime == 0 ? 0 : (time - HitObject.StartTime) / segmentTime);
return curveData.NodeSamples[index];
}
return HitObject.Samples;
}
}
}
}

View File

@ -474,17 +474,17 @@ namespace osu.Game.Rulesets.Mania.Beatmaps.Patterns.Legacy
/// <returns></returns>
private IList<HitSampleInfo> sampleInfoListAt(double time)
{
var curveData = HitObject as IHasCurve;
if (curveData == null)
return HitObject.Samples;
if (HitObject is IHasCurve curveData)
{
double segmentTime = (EndTime - HitObject.StartTime) / spanCount;
int index = (int)(segmentTime == 0 ? 0 : (time - HitObject.StartTime) / segmentTime);
return curveData.NodeSamples[index];
}
return HitObject.Samples;
}
/// <summary>
/// Constructs and adds a note to a pattern.
/// </summary>

View File

@ -379,10 +379,8 @@ namespace osu.Game.Rulesets.Osu.Tests
private void onNewResult(DrawableHitObject judgedObject, JudgementResult result)
{
var osuObject = judgedObject as DrawableOsuHitObject;
if (osuObject == null)
return;
if (judgedObject is DrawableOsuHitObject osuObject)
{
OsuSpriteText text;
Add(text = new OsuSpriteText
{
@ -401,4 +399,5 @@ namespace osu.Game.Rulesets.Osu.Tests
judgementOffsetDirection *= -1;
}
}
}
}

View File

@ -9,6 +9,7 @@ using System.Collections.Generic;
using osu.Game.Rulesets.Objects.Types;
using System;
using osu.Game.Rulesets.Osu.UI;
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Game.Rulesets.Osu.Beatmaps
{
@ -23,15 +24,13 @@ namespace osu.Game.Rulesets.Osu.Beatmaps
protected override IEnumerable<OsuHitObject> ConvertHitObject(HitObject original, IBeatmap beatmap)
{
var curveData = original as IHasCurve;
var endTimeData = original as IHasEndTime;
var positionData = original as IHasPosition;
var comboData = original as IHasCombo;
var legacyOffset = original as IHasLegacyLastTickOffset;
if (curveData != null)
switch (original)
{
yield return new Slider
case IHasCurve curveData:
return new Slider
{
StartTime = original.StartTime,
Samples = original.Samples,
@ -41,15 +40,14 @@ namespace osu.Game.Rulesets.Osu.Beatmaps
Position = positionData?.Position ?? Vector2.Zero,
NewCombo = comboData?.NewCombo ?? false,
ComboOffset = comboData?.ComboOffset ?? 0,
LegacyLastTickOffset = legacyOffset?.LegacyLastTickOffset,
LegacyLastTickOffset = (original as IHasLegacyLastTickOffset)?.LegacyLastTickOffset,
// prior to v8, speed multipliers don't adjust for how many ticks are generated over the same distance.
// this results in more (or less) ticks being generated in <v8 maps for the same time duration.
TickDistanceMultiplier = beatmap.BeatmapInfo.BeatmapVersion < 8 ? 1f / beatmap.ControlPointInfo.DifficultyPointAt(original.StartTime).SpeedMultiplier : 1
};
}
else if (endTimeData != null)
{
yield return new Spinner
}.Yield();
case IHasEndTime endTimeData:
return new Spinner
{
StartTime = original.StartTime,
Samples = original.Samples,
@ -57,18 +55,17 @@ namespace osu.Game.Rulesets.Osu.Beatmaps
Position = positionData?.Position ?? OsuPlayfield.BASE_SIZE / 2,
NewCombo = comboData?.NewCombo ?? false,
ComboOffset = comboData?.ComboOffset ?? 0,
};
}
else
{
yield return new HitCircle
}.Yield();
default:
return new HitCircle
{
StartTime = original.StartTime,
Samples = original.Samples,
Position = positionData?.Position ?? Vector2.Zero,
NewCombo = comboData?.NewCombo ?? false,
ComboOffset = comboData?.ComboOffset ?? 0,
};
}.Yield();
}
}

View File

@ -22,10 +22,8 @@ namespace osu.Game.Rulesets.Osu.Mods
osuObject.Position = new Vector2(osuObject.Position.X, OsuPlayfield.BASE_SIZE.Y - osuObject.Y);
var slider = hitObject as Slider;
if (slider == null)
return;
if (hitObject is Slider slider)
{
slider.NestedHitObjects.OfType<SliderTick>().ForEach(h => h.Position = new Vector2(h.Position.X, OsuPlayfield.BASE_SIZE.Y - h.Position.Y));
slider.NestedHitObjects.OfType<RepeatPoint>().ForEach(h => h.Position = new Vector2(h.Position.X, OsuPlayfield.BASE_SIZE.Y - h.Position.Y));
@ -36,4 +34,5 @@ namespace osu.Game.Rulesets.Osu.Mods
slider.Path = new SliderPath(slider.Path.Type, newControlPoints, slider.Path.ExpectedDistance);
}
}
}
}

View File

@ -73,20 +73,17 @@ namespace osu.Game.Rulesets.Taiko.Beatmaps
protected override IEnumerable<TaikoHitObject> ConvertHitObject(HitObject obj, IBeatmap beatmap)
{
var distanceData = obj as IHasDistance;
var repeatsData = obj as IHasRepeats;
var endTimeData = obj as IHasEndTime;
var curveData = obj as IHasCurve;
// Old osu! used hit sounding to determine various hit type information
IList<HitSampleInfo> samples = obj.Samples;
bool strong = samples.Any(s => s.Name == HitSampleInfo.HIT_FINISH);
if (distanceData != null)
switch (obj)
{
case IHasDistance distanceData:
{
// Number of spans of the object - one for the initial length and for each repeat
int spans = repeatsData?.SpanCount() ?? 1;
int spans = (obj as IHasRepeats)?.SpanCount() ?? 1;
TimingControlPoint timingPoint = beatmap.ControlPointInfo.TimingPointAt(obj.StartTime);
DifficultyControlPoint difficultyPoint = beatmap.ControlPointInfo.DifficultyPointAt(obj.StartTime);
@ -117,7 +114,7 @@ namespace osu.Game.Rulesets.Taiko.Beatmaps
if (!isForCurrentRuleset && tickSpacing > 0 && osuDuration < 2 * speedAdjustedBeatLength)
{
List<IList<HitSampleInfo>> allSamples = curveData != null ? curveData.NodeSamples : new List<IList<HitSampleInfo>>(new[] { samples });
List<IList<HitSampleInfo>> allSamples = obj is IHasCurve curveData ? curveData.NodeSamples : new List<IList<HitSampleInfo>>(new[] { samples });
int i = 0;
@ -160,8 +157,11 @@ namespace osu.Game.Rulesets.Taiko.Beatmaps
TickRate = beatmap.BeatmapInfo.BaseDifficulty.SliderTickRate == 3 ? 3 : 4
};
}
break;
}
else if (endTimeData != null)
case IHasEndTime endTimeData:
{
double hitMultiplier = BeatmapDifficulty.DifficultyRange(beatmap.BeatmapInfo.BaseDifficulty.OverallDifficulty, 3, 5, 7.5) * swell_hit_multiplier;
@ -172,8 +172,11 @@ namespace osu.Game.Rulesets.Taiko.Beatmaps
Duration = endTimeData.Duration,
RequiredHits = (int)Math.Max(1, endTimeData.Duration / 1000 * hitMultiplier)
};
break;
}
else
default:
{
bool isRim = samples.Any(s => s.Name == HitSampleInfo.HIT_CLAP || s.Name == HitSampleInfo.HIT_WHISTLE);
@ -195,6 +198,9 @@ namespace osu.Game.Rulesets.Taiko.Beatmaps
IsStrong = strong
};
}
break;
}
}
}

View File

@ -43,11 +43,9 @@ namespace osu.Game.Rulesets.Taiko.Replays
IHasEndTime endTimeData = h as IHasEndTime;
double endTime = endTimeData?.EndTime ?? h.StartTime;
Swell swell = h as Swell;
DrumRoll drumRoll = h as DrumRoll;
Hit hit = h as Hit;
if (swell != null)
switch (h)
{
case Swell swell:
{
int d = 0;
int count = 0;
@ -83,16 +81,22 @@ namespace osu.Game.Rulesets.Taiko.Replays
if (++count == req)
break;
}
break;
}
else if (drumRoll != null)
case DrumRoll drumRoll:
{
foreach (var tick in drumRoll.NestedHitObjects.OfType<DrumRollTick>())
{
Frames.Add(new TaikoReplayFrame(tick.StartTime, hitButton ? TaikoAction.LeftCentre : TaikoAction.RightCentre));
hitButton = !hitButton;
}
break;
}
else if (hit != null)
case Hit hit:
{
TaikoAction[] actions;
@ -110,9 +114,12 @@ namespace osu.Game.Rulesets.Taiko.Replays
}
Frames.Add(new TaikoReplayFrame(h.StartTime, actions));
break;
}
else
default:
throw new InvalidOperationException("Unknown hit object type.");
}
var nextHitObject = GetNextObject(i); // Get the next object that requires pressing the same button

View File

@ -125,11 +125,8 @@ namespace osu.Game.Tournament.Screens.Drawings.Components
foreach (var c in Children)
{
var stc = c as ScrollingTeam;
if (stc == null)
continue;
if (c is ScrollingTeam stc)
{
if (closest == null)
{
closest = stc;
@ -142,6 +139,7 @@ namespace osu.Game.Tournament.Screens.Drawings.Components
if (o < lastOffset)
closest = stc;
}
}
Trace.Assert(closest != null, "closest != null");
@ -203,11 +201,8 @@ namespace osu.Game.Tournament.Screens.Drawings.Components
foreach (var c in Children)
{
ScrollingTeam st = c as ScrollingTeam;
if (st == null)
continue;
if (c is ScrollingTeam st)
{
if (st.Team == team)
{
st.FadeOut(200);
@ -215,6 +210,7 @@ namespace osu.Game.Tournament.Screens.Drawings.Components
}
}
}
}
public void StartScrolling()
{
@ -295,10 +291,8 @@ namespace osu.Game.Tournament.Screens.Drawings.Components
{
foreach (var c in Children)
{
ScrollingTeam st = c as ScrollingTeam;
if (st == null)
continue;
if (c is ScrollingTeam st)
{
if (st.Selected)
{
st.Selected = false;
@ -306,6 +300,7 @@ namespace osu.Game.Tournament.Screens.Drawings.Components
}
}
}
}
private void speedTo(float value, double duration = 0, Easing easing = Easing.None) =>
this.TransformTo(nameof(speed), value, duration, easing);

View File

@ -297,9 +297,7 @@ namespace osu.Game.Overlays.Settings.Sections.General
{
set
{
var h = Header as UserDropdownHeader;
if (h == null) return;
if (Header is UserDropdownHeader h)
h.StatusColour = value;
}
}

View File

@ -148,13 +148,9 @@ namespace osu.Game.Rulesets.UI.Scrolling
// Generate the timing points, making non-timing changes use the previous timing change and vice-versa
var timingChanges = allPoints.Select(c =>
{
var timingPoint = c as TimingControlPoint;
var difficultyPoint = c as DifficultyControlPoint;
if (timingPoint != null)
if (c is TimingControlPoint timingPoint)
lastTimingPoint = timingPoint;
if (difficultyPoint != null)
else if (c is DifficultyControlPoint difficultyPoint)
lastDifficultyPoint = difficultyPoint;
return new MultiplierControlPoint(c.Time)

View File

@ -17,10 +17,10 @@ namespace osu.Game.Screens.Backgrounds
public override bool Equals(BackgroundScreen other)
{
var backgroundScreenCustom = other as BackgroundScreenCustom;
if (backgroundScreenCustom == null) return false;
if (other is BackgroundScreenCustom backgroundScreenCustom)
return base.Equals(other) && textureName == backgroundScreenCustom.textureName;
return false;
}
}
}