Reduce code duplication

This commit is contained in:
sw1tchbl4d3
2022-06-20 17:22:41 +02:00
parent 98527fec26
commit 6c8042642a
3 changed files with 63 additions and 103 deletions

View File

@ -4,6 +4,7 @@
#nullable disable #nullable disable
using System.Linq; using System.Linq;
using System.Collections.Generic;
using System.Threading; using System.Threading;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Judgements;
@ -40,19 +41,17 @@ namespace osu.Game.Rulesets.Taiko.Mods
var hitObjects = taikoBeatmap.HitObjects.Select(ho => var hitObjects = taikoBeatmap.HitObjects.Select(ho =>
{ {
if (ho is DrumRoll drumRoll) switch (ho)
{ {
var newDrumRoll = new ClassicDrumRoll(drumRoll); case DrumRoll drumRoll:
return newDrumRoll; return new ClassicDrumRoll(drumRoll);
}
if (ho is Swell swell) case Swell swell:
{ return new ClassicSwell(swell);
var newSwell = new ClassicSwell(swell);
return newSwell;
}
return ho; default:
return ho;
}
}).ToList(); }).ToList();
taikoBeatmap.HitObjects = hitObjects; taikoBeatmap.HitObjects = hitObjects;
@ -73,33 +72,35 @@ namespace osu.Game.Rulesets.Taiko.Mods
public override Judgement CreateJudgement() => new TaikoClassicDrumRollJudgement(); public override Judgement CreateJudgement() => new TaikoClassicDrumRollJudgement();
protected override void CreateTicks(CancellationToken cancellationToken) protected override List<TaikoHitObject> CreateTicks(CancellationToken cancellationToken)
{ {
if (TickSpacing == 0) List<TaikoHitObject> oldTicks = base.CreateTicks(cancellationToken);
return;
bool first = true; List<TaikoHitObject> newTicks = oldTicks.Select(oldTick =>
for (double t = StartTime; t < EndTime + TickSpacing / 2; t += TickSpacing)
{ {
cancellationToken.ThrowIfCancellationRequested(); if (oldTick is DrumRollTick drumRollTick)
AddNested(new ClassicDrumRollTick
{ {
FirstTick = first, return new ClassicDrumRollTick(drumRollTick);
TickSpacing = TickSpacing, }
StartTime = t,
IsStrong = IsStrong
});
first = false; return oldTick;
} }).ToList();
return newTicks;
} }
} }
private class ClassicDrumRollTick : DrumRollTick private class ClassicDrumRollTick : DrumRollTick
{ {
public override Judgement CreateJudgement() => new TaikoClassicDrumRollTickJudgement(); public override Judgement CreateJudgement() => new TaikoClassicDrumRollTickJudgement();
public ClassicDrumRollTick(DrumRollTick original)
{
StartTime = original.StartTime;
Samples = original.Samples;
FirstTick = original.FirstTick;
TickSpacing = original.TickSpacing;
}
} }
private class ClassicSwell : Swell private class ClassicSwell : Swell
@ -118,12 +119,12 @@ namespace osu.Game.Rulesets.Taiko.Mods
private class TaikoClassicDrumRollJudgement : TaikoDrumRollJudgement private class TaikoClassicDrumRollJudgement : TaikoDrumRollJudgement
{ {
public override HitResult MaxResult => HitResult.LargeBonus; public override HitResult MaxResult => HitResult.IgnoreHit;
} }
private class TaikoClassicDrumRollTickJudgement : TaikoDrumRollTickJudgement private class TaikoClassicDrumRollTickJudgement : TaikoDrumRollTickJudgement
{ {
public override HitResult MaxResult => HitResult.LargeBonus; public override HitResult MaxResult => HitResult.SmallBonus;
} }
private class TaikoClassicSwellJudgement : TaikoSwellJudgement private class TaikoClassicSwellJudgement : TaikoSwellJudgement
@ -143,7 +144,7 @@ namespace osu.Game.Rulesets.Taiko.Mods
if (timeOffset < 0) if (timeOffset < 0)
return; return;
ApplyResult(r => r.Type = HitResult.IgnoreMiss); ApplyResult(r => r.Type = HitResult.IgnoreHit);
} }
} }
@ -151,52 +152,7 @@ namespace osu.Game.Rulesets.Taiko.Mods
{ {
public override bool DisplayResult => false; public override bool DisplayResult => false;
protected override void CheckForResult(bool userTriggered, double timeOffset) protected override HitResult OkResult => HitResult.SmallBonus;
{
if (userTriggered)
{
DrawableSwellTick nextTick = null;
foreach (var t in Ticks)
{
if (!t.Result.HasResult)
{
nextTick = t;
break;
}
}
nextTick?.TriggerResult(true);
int numHits = Ticks.Count(r => r.IsHit);
AnimateCompletion(numHits);
if (numHits == HitObject.RequiredHits)
ApplyResult(r => r.Type = HitResult.LargeBonus);
}
else
{
if (timeOffset < 0)
return;
int numHits = 0;
foreach (var tick in Ticks)
{
if (tick.IsHit)
{
numHits++;
continue;
}
if (!tick.Result.HasResult)
tick.TriggerResult(false);
}
ApplyResult(r => r.Type = numHits > HitObject.RequiredHits / 2 ? HitResult.SmallBonus : HitResult.IgnoreMiss);
}
}
} }
public void Update(Playfield playfield) public void Update(Playfield playfield)

View File

@ -34,7 +34,8 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
/// </summary> /// </summary>
private const double ring_appear_offset = 100; private const double ring_appear_offset = 100;
protected readonly Container<DrawableSwellTick> Ticks; protected virtual HitResult OkResult => HitResult.Ok;
private readonly Container<DrawableSwellTick> ticks;
private readonly Container bodyContainer; private readonly Container bodyContainer;
private readonly CircularContainer targetRing; private readonly CircularContainer targetRing;
private readonly CircularContainer expandingRing; private readonly CircularContainer expandingRing;
@ -114,7 +115,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
} }
}); });
AddInternal(Ticks = new Container<DrawableSwellTick> { RelativeSizeAxes = Axes.Both }); AddInternal(ticks = new Container<DrawableSwellTick> { RelativeSizeAxes = Axes.Both });
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -132,20 +133,6 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
Origin = Anchor.Centre, Origin = Anchor.Centre,
}); });
protected void AnimateCompletion(int numHits)
{
float completion = (float)numHits / HitObject.RequiredHits;
expandingRing
.FadeTo(expandingRing.Alpha + Math.Clamp(completion / 16, 0.1f, 0.6f), 50)
.Then()
.FadeTo(completion / 8, 2000, Easing.OutQuint);
MainPiece.Drawable.RotateTo((float)(completion * HitObject.Duration / 8), 4000, Easing.OutQuint);
expandingRing.ScaleTo(1f + Math.Min(target_ring_scale - 1f, (target_ring_scale - 1f) * completion * 1.3f), 260, Easing.OutQuint);
}
protected override void OnFree() protected override void OnFree()
{ {
base.OnFree(); base.OnFree();
@ -162,7 +149,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
switch (hitObject) switch (hitObject)
{ {
case DrawableSwellTick tick: case DrawableSwellTick tick:
Ticks.Add(tick); ticks.Add(tick);
break; break;
} }
} }
@ -170,7 +157,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
protected override void ClearNestedHitObjects() protected override void ClearNestedHitObjects()
{ {
base.ClearNestedHitObjects(); base.ClearNestedHitObjects();
Ticks.Clear(false); ticks.Clear(false);
} }
protected override DrawableHitObject CreateNestedHitObject(HitObject hitObject) protected override DrawableHitObject CreateNestedHitObject(HitObject hitObject)
@ -190,7 +177,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
{ {
DrawableSwellTick nextTick = null; DrawableSwellTick nextTick = null;
foreach (var t in Ticks) foreach (var t in ticks)
{ {
if (!t.Result.HasResult) if (!t.Result.HasResult)
{ {
@ -201,12 +188,21 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
nextTick?.TriggerResult(true); nextTick?.TriggerResult(true);
int numHits = Ticks.Count(r => r.IsHit); int numHits = ticks.Count(r => r.IsHit);
AnimateCompletion(numHits); float completion = (float)numHits / HitObject.RequiredHits;
expandingRing
.FadeTo(expandingRing.Alpha + Math.Clamp(completion / 16, 0.1f, 0.6f), 50)
.Then()
.FadeTo(completion / 8, 2000, Easing.OutQuint);
MainPiece.Drawable.RotateTo((float)(completion * HitObject.Duration / 8), 4000, Easing.OutQuint);
expandingRing.ScaleTo(1f + Math.Min(target_ring_scale - 1f, (target_ring_scale - 1f) * completion * 1.3f), 260, Easing.OutQuint);
if (numHits == HitObject.RequiredHits) if (numHits == HitObject.RequiredHits)
ApplyResult(r => r.Type = HitResult.Great); ApplyResult(r => r.Type = r.Judgement.MaxResult);
} }
else else
{ {
@ -215,7 +211,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
int numHits = 0; int numHits = 0;
foreach (var tick in Ticks) foreach (var tick in ticks)
{ {
if (tick.IsHit) if (tick.IsHit)
{ {
@ -227,7 +223,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
tick.TriggerResult(false); tick.TriggerResult(false);
} }
ApplyResult(r => r.Type = numHits > HitObject.RequiredHits / 2 ? HitResult.Ok : r.Judgement.MinResult); ApplyResult(r => r.Type = numHits > HitObject.RequiredHits / 2 ? OkResult : r.Judgement.MinResult);
} }
} }

View File

@ -5,6 +5,7 @@
using osu.Game.Rulesets.Objects.Types; using osu.Game.Rulesets.Objects.Types;
using System; using System;
using System.Collections.Generic;
using System.Threading; using System.Threading;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Beatmaps.ControlPoints; using osu.Game.Beatmaps.ControlPoints;
@ -75,7 +76,10 @@ namespace osu.Game.Rulesets.Taiko.Objects
protected override void CreateNestedHitObjects(CancellationToken cancellationToken) protected override void CreateNestedHitObjects(CancellationToken cancellationToken)
{ {
CreateTicks(cancellationToken); foreach (TaikoHitObject tick in CreateTicks(cancellationToken))
{
AddNested(tick);
}
RequiredGoodHits = NestedHitObjects.Count * Math.Min(0.15, 0.05 + 0.10 / 6 * overallDifficulty); RequiredGoodHits = NestedHitObjects.Count * Math.Min(0.15, 0.05 + 0.10 / 6 * overallDifficulty);
RequiredGreatHits = NestedHitObjects.Count * Math.Min(0.30, 0.10 + 0.20 / 6 * overallDifficulty); RequiredGreatHits = NestedHitObjects.Count * Math.Min(0.30, 0.10 + 0.20 / 6 * overallDifficulty);
@ -83,10 +87,12 @@ namespace osu.Game.Rulesets.Taiko.Objects
base.CreateNestedHitObjects(cancellationToken); base.CreateNestedHitObjects(cancellationToken);
} }
protected virtual void CreateTicks(CancellationToken cancellationToken) protected virtual List<TaikoHitObject> CreateTicks(CancellationToken cancellationToken)
{ {
List<TaikoHitObject> ticks = new List<TaikoHitObject>();
if (TickSpacing == 0) if (TickSpacing == 0)
return; return ticks;
bool first = true; bool first = true;
@ -94,7 +100,7 @@ namespace osu.Game.Rulesets.Taiko.Objects
{ {
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
AddNested(new DrumRollTick ticks.Add(new DrumRollTick
{ {
FirstTick = first, FirstTick = first,
TickSpacing = TickSpacing, TickSpacing = TickSpacing,
@ -104,6 +110,8 @@ namespace osu.Game.Rulesets.Taiko.Objects
first = false; first = false;
} }
return ticks;
} }
public override Judgement CreateJudgement() => new TaikoDrumRollJudgement(); public override Judgement CreateJudgement() => new TaikoDrumRollJudgement();