1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-09 01:32:59 +08:00

Refactor result application around again to remove requirement for fields

Co-authored-by: Dean Herbert <pe@ppy.sh>
This commit is contained in:
Bartłomiej Dach 2024-02-05 13:21:01 +01:00
parent 6cfd2813ed
commit efe6bb25b1
No known key found for this signature in database
18 changed files with 59 additions and 50 deletions

View File

@ -265,7 +265,7 @@ namespace osu.Game.Rulesets.Mania.Objects.Drawables
if (Tail.AllJudged) if (Tail.AllJudged)
{ {
if (Tail.IsHit) if (Tail.IsHit)
ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult); ApplyMaxResult();
else else
MissForcefully(); MissForcefully();
} }

View File

@ -11,8 +11,6 @@ namespace osu.Game.Rulesets.Mania.Objects.Drawables
public override bool DisplayResult => false; public override bool DisplayResult => false;
private bool hit;
public DrawableHoldNoteBody() public DrawableHoldNoteBody()
: this(null) : this(null)
{ {
@ -27,12 +25,10 @@ namespace osu.Game.Rulesets.Mania.Objects.Drawables
{ {
if (AllJudged) return; if (AllJudged) return;
this.hit = hit; if (hit)
ApplyResult(static (r, hitObject) => ApplyMaxResult();
{ else
var holdNoteBody = (DrawableHoldNoteBody)hitObject; ApplyMinResult();
r.Type = holdNoteBody.hit ? r.Judgement.MaxResult : r.Judgement.MinResult;
});
} }
} }
} }

View File

@ -87,7 +87,7 @@ namespace osu.Game.Rulesets.Mania.Objects.Drawables
/// <summary> /// <summary>
/// Causes this <see cref="DrawableManiaHitObject"/> to get missed, disregarding all conditions in implementations of <see cref="DrawableHitObject.CheckForResult"/>. /// Causes this <see cref="DrawableManiaHitObject"/> to get missed, disregarding all conditions in implementations of <see cref="DrawableHitObject.CheckForResult"/>.
/// </summary> /// </summary>
public virtual void MissForcefully() => ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); public virtual void MissForcefully() => ApplyMinResult();
} }
public abstract partial class DrawableManiaHitObject<TObject> : DrawableManiaHitObject public abstract partial class DrawableManiaHitObject<TObject> : DrawableManiaHitObject

View File

@ -91,12 +91,13 @@ namespace osu.Game.Rulesets.Mania.Objects.Drawables
if (!userTriggered) if (!userTriggered)
{ {
if (!HitObject.HitWindows.CanBeHit(timeOffset)) if (!HitObject.HitWindows.CanBeHit(timeOffset))
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); ApplyMinResult();
return; return;
} }
hitResult = HitObject.HitWindows.ResultFor(timeOffset); hitResult = HitObject.HitWindows.ResultFor(timeOffset);
if (hitResult == HitResult.None) if (hitResult == HitResult.None)
return; return;

View File

@ -136,7 +136,7 @@ namespace osu.Game.Rulesets.Osu.Tests
if (auto && !userTriggered && timeOffset > hitOffset && CheckHittable?.Invoke(this, Time.Current, HitResult.Great) == ClickAction.Hit) if (auto && !userTriggered && timeOffset > hitOffset && CheckHittable?.Invoke(this, Time.Current, HitResult.Great) == ClickAction.Hit)
{ {
// force success // force success
ApplyResult(static (r, _) => r.Type = HitResult.Great); ApplyResult(HitResult.Great);
} }
else else
base.CheckForResult(userTriggered, timeOffset); base.CheckForResult(userTriggered, timeOffset);

View File

@ -208,7 +208,7 @@ namespace osu.Game.Rulesets.Osu.Tests
if (shouldHit && !userTriggered && timeOffset >= 0) if (shouldHit && !userTriggered && timeOffset >= 0)
{ {
// force success // force success
ApplyResult(static (r, _) => r.Type = HitResult.Great); ApplyResult(HitResult.Great);
} }
else else
base.CheckForResult(userTriggered, timeOffset); base.CheckForResult(userTriggered, timeOffset);

View File

@ -44,7 +44,6 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
private Container scaleContainer; private Container scaleContainer;
private InputManager inputManager; private InputManager inputManager;
private HitResult hitResult;
public DrawableHitCircle() public DrawableHitCircle()
: this(null) : this(null)
@ -156,12 +155,12 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
if (!userTriggered) if (!userTriggered)
{ {
if (!HitObject.HitWindows.CanBeHit(timeOffset)) if (!HitObject.HitWindows.CanBeHit(timeOffset))
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); ApplyMinResult();
return; return;
} }
hitResult = ResultFor(timeOffset); HitResult hitResult = ResultFor(timeOffset);
var clickAction = CheckHittable?.Invoke(this, Time.Current, hitResult); var clickAction = CheckHittable?.Invoke(this, Time.Current, hitResult);
if (clickAction == ClickAction.Shake) if (clickAction == ClickAction.Shake)
@ -170,20 +169,22 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
if (hitResult == HitResult.None || clickAction != ClickAction.Hit) if (hitResult == HitResult.None || clickAction != ClickAction.Hit)
return; return;
ApplyResult(static (r, hitObject) => Vector2? hitPosition = null;
// Todo: This should also consider misses, but they're a little more interesting to handle, since we don't necessarily know the position at the time of a miss.
if (hitResult.IsHit())
{
var localMousePosition = ToLocalSpace(inputManager.CurrentState.Mouse.Position);
hitPosition = HitObject.StackedPosition + (localMousePosition - DrawSize / 2);
}
ApplyResult<(HitResult result, Vector2? position)>((r, state) =>
{ {
var hitCircle = (DrawableHitCircle)hitObject;
var circleResult = (OsuHitCircleJudgementResult)r; var circleResult = (OsuHitCircleJudgementResult)r;
// Todo: This should also consider misses, but they're a little more interesting to handle, since we don't necessarily know the position at the time of a miss. circleResult.Type = state.result;
if (hitCircle.hitResult.IsHit()) circleResult.CursorPositionAtHit = state.position;
{ }, (hitResult, hitPosition));
var localMousePosition = hitCircle.ToLocalSpace(hitCircle.inputManager.CurrentState.Mouse.Position);
circleResult.CursorPositionAtHit = hitCircle.HitObject.StackedPosition + (localMousePosition - hitCircle.DrawSize / 2);
}
circleResult.Type = hitCircle.hitResult;
});
} }
/// <summary> /// <summary>

View File

@ -100,12 +100,12 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
/// <summary> /// <summary>
/// Causes this <see cref="DrawableOsuHitObject"/> to get hit, disregarding all conditions in implementations of <see cref="DrawableHitObject.CheckForResult"/>. /// Causes this <see cref="DrawableOsuHitObject"/> to get hit, disregarding all conditions in implementations of <see cref="DrawableHitObject.CheckForResult"/>.
/// </summary> /// </summary>
public void HitForcefully() => ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult); public void HitForcefully() => ApplyMaxResult();
/// <summary> /// <summary>
/// Causes this <see cref="DrawableOsuHitObject"/> to get missed, disregarding all conditions in implementations of <see cref="DrawableHitObject.CheckForResult"/>. /// Causes this <see cref="DrawableOsuHitObject"/> to get missed, disregarding all conditions in implementations of <see cref="DrawableHitObject.CheckForResult"/>.
/// </summary> /// </summary>
public void MissForcefully() => ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); public void MissForcefully() => ApplyMinResult();
private RectangleF parentScreenSpaceRectangle => ((DrawableOsuHitObject)ParentHitObject)?.parentScreenSpaceRectangle ?? Parent!.ScreenSpaceDrawQuad.AABBFloat; private RectangleF parentScreenSpaceRectangle => ((DrawableOsuHitObject)ParentHitObject)?.parentScreenSpaceRectangle ?? Parent!.ScreenSpaceDrawQuad.AABBFloat;

View File

@ -143,7 +143,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
if (timeOffset < 0) if (timeOffset < 0)
return; return;
ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult); ApplyMaxResult();
} }
protected override void UpdateHitStateTransforms(ArmedState state) protected override void UpdateHitStateTransforms(ArmedState state)

View File

@ -49,14 +49,14 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
if (!userTriggered) if (!userTriggered)
{ {
if (timeOffset > HitObject.HitWindow) if (timeOffset > HitObject.HitWindow)
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); ApplyMinResult();
return; return;
} }
if (Math.Abs(timeOffset) > HitObject.HitWindow) if (Math.Abs(timeOffset) > HitObject.HitWindow)
return; return;
ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult); ApplyMaxResult();
} }
public override void OnKilled() public override void OnKilled()
@ -64,7 +64,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
base.OnKilled(); base.OnKilled();
if (Time.Current > HitObject.GetEndTime() && !Judged) if (Time.Current > HitObject.GetEndTime() && !Judged)
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); ApplyMinResult();
} }
protected override void UpdateHitStateTransforms(ArmedState state) protected override void UpdateHitStateTransforms(ArmedState state)

View File

@ -25,7 +25,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult); ApplyMaxResult();
} }
protected override void LoadSamples() protected override void LoadSamples()

View File

@ -101,7 +101,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
if (!userTriggered) if (!userTriggered)
{ {
if (!HitObject.HitWindows.CanBeHit(timeOffset)) if (!HitObject.HitWindows.CanBeHit(timeOffset))
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); ApplyMinResult();
return; return;
} }
@ -110,7 +110,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
return; return;
if (!validActionPressed) if (!validActionPressed)
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); ApplyMinResult();
else else
{ {
ApplyResult(static (r, hitObject) => ApplyResult(static (r, hitObject) =>
@ -217,19 +217,19 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
if (!ParentHitObject.Result.IsHit) if (!ParentHitObject.Result.IsHit)
{ {
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); ApplyMinResult();
return; return;
} }
if (!userTriggered) if (!userTriggered)
{ {
if (timeOffset - ParentHitObject.Result.TimeOffset > SECOND_HIT_WINDOW) if (timeOffset - ParentHitObject.Result.TimeOffset > SECOND_HIT_WINDOW)
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); ApplyMinResult();
return; return;
} }
if (Math.Abs(timeOffset - ParentHitObject.Result.TimeOffset) <= SECOND_HIT_WINDOW) if (Math.Abs(timeOffset - ParentHitObject.Result.TimeOffset) <= SECOND_HIT_WINDOW)
ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult); ApplyMaxResult();
} }
public override bool OnPressed(KeyBindingPressEvent<TaikoAction> e) public override bool OnPressed(KeyBindingPressEvent<TaikoAction> e)

View File

@ -27,7 +27,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
// it can happen that the hit window of the nested strong hit extends past the lifetime of the parent object. // it can happen that the hit window of the nested strong hit extends past the lifetime of the parent object.
// this is a safety to prevent such cases from causing the nested hit to never be judged and as such prevent gameplay from completing. // this is a safety to prevent such cases from causing the nested hit to never be judged and as such prevent gameplay from completing.
if (!Judged && Time.Current > ParentHitObject?.HitObject.GetEndTime()) if (!Judged && Time.Current > ParentHitObject?.HitObject.GetEndTime())
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); ApplyMinResult();
} }
} }
} }

View File

@ -208,7 +208,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
expandingRing.ScaleTo(1f + Math.Min(target_ring_scale - 1f, (target_ring_scale - 1f) * completion * 1.3f), 260, 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(static (r, _) => r.Type = r.Judgement.MaxResult); ApplyMaxResult();
} }
else else
{ {

View File

@ -216,7 +216,7 @@ namespace osu.Game.Tests.Gameplay
LifetimeStart = LIFETIME_ON_APPLY; LifetimeStart = LIFETIME_ON_APPLY;
} }
public void MissForcefully() => ApplyResult(static (r, _) => r.Type = HitResult.Miss); public void MissForcefully() => ApplyResult(HitResult.Miss);
protected override void UpdateHitStateTransforms(ArmedState state) protected override void UpdateHitStateTransforms(ArmedState state)
{ {

View File

@ -431,7 +431,7 @@ namespace osu.Game.Tests.Visual.Gameplay
protected override void CheckForResult(bool userTriggered, double timeOffset) protected override void CheckForResult(bool userTriggered, double timeOffset)
{ {
if (timeOffset > HitObject.Duration) if (timeOffset > HitObject.Duration)
ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult); ApplyMaxResult();
} }
protected override void UpdateHitStateTransforms(ArmedState state) protected override void UpdateHitStateTransforms(ArmedState state)
@ -468,7 +468,7 @@ namespace osu.Game.Tests.Visual.Gameplay
public override void OnKilled() public override void OnKilled()
{ {
base.OnKilled(); base.OnKilled();
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult); ApplyMinResult();
} }
} }
@ -547,7 +547,7 @@ namespace osu.Game.Tests.Visual.Gameplay
{ {
base.CheckForResult(userTriggered, timeOffset); base.CheckForResult(userTriggered, timeOffset);
if (timeOffset >= 0) if (timeOffset >= 0)
ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult); ApplyMaxResult();
} }
} }
@ -596,7 +596,7 @@ namespace osu.Game.Tests.Visual.Gameplay
{ {
base.CheckForResult(userTriggered, timeOffset); base.CheckForResult(userTriggered, timeOffset);
if (timeOffset >= 0) if (timeOffset >= 0)
ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult); ApplyMaxResult();
} }
} }

View File

@ -30,7 +30,7 @@ namespace osu.Game.Rulesets.Judgements
/// <summary> /// <summary>
/// The time at which this <see cref="JudgementResult"/> occurred. /// The time at which this <see cref="JudgementResult"/> occurred.
/// Populated when this <see cref="JudgementResult"/> is applied via <see cref="DrawableHitObject.ApplyResult"/>. /// Populated when this <see cref="JudgementResult"/> is applied via <see cref="DrawableHitObject.ApplyResult{T}"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This is used instead of <see cref="TimeAbsolute"/> to check whether this <see cref="JudgementResult"/> should be reverted. /// This is used instead of <see cref="TimeAbsolute"/> to check whether this <see cref="JudgementResult"/> should be reverted.

View File

@ -682,17 +682,28 @@ namespace osu.Game.Rulesets.Objects.Drawables
UpdateResult(false); UpdateResult(false);
} }
protected void ApplyMaxResult() => ApplyResult((r, _) => r.Type = r.Judgement.MaxResult);
protected void ApplyMinResult() => ApplyResult((r, _) => r.Type = r.Judgement.MinResult);
protected void ApplyResult(HitResult type) => ApplyResult(static (result, state) => result.Type = state, type);
[Obsolete("Use overload with state, preferrably with static delegates to avoid allocation overhead.")] // Can be removed 2024-07-26
protected void ApplyResult(Action<JudgementResult> application) => ApplyResult((r, _) => application(r), this);
protected void ApplyResult(Action<JudgementResult, DrawableHitObject> application) => ApplyResult(application, this);
/// <summary> /// <summary>
/// Applies the <see cref="Result"/> of this <see cref="DrawableHitObject"/>, notifying responders such as /// Applies the <see cref="Result"/> of this <see cref="DrawableHitObject"/>, notifying responders such as
/// the <see cref="ScoreProcessor"/> of the <see cref="JudgementResult"/>. /// the <see cref="ScoreProcessor"/> of the <see cref="JudgementResult"/>.
/// </summary> /// </summary>
/// <param name="application">The callback that applies changes to the <see cref="JudgementResult"/>. Using a `static` delegate is recommended to avoid allocation overhead.</param> /// <param name="application">The callback that applies changes to the <see cref="JudgementResult"/>. Using a `static` delegate is recommended to avoid allocation overhead.</param>
protected void ApplyResult(Action<JudgementResult, DrawableHitObject> application) /// <param name="state">The state.</param>
protected void ApplyResult<T>(Action<JudgementResult, T> application, T state)
{ {
if (Result.HasResult) if (Result.HasResult)
throw new InvalidOperationException("Cannot apply result on a hitobject that already has a result."); throw new InvalidOperationException("Cannot apply result on a hitobject that already has a result.");
application?.Invoke(Result, this); application?.Invoke(Result, state);
if (!Result.HasResult) if (!Result.HasResult)
throw new InvalidOperationException($"{GetType().ReadableName()} applied a {nameof(JudgementResult)} but did not update {nameof(JudgementResult.Type)}."); throw new InvalidOperationException($"{GetType().ReadableName()} applied a {nameof(JudgementResult)} but did not update {nameof(JudgementResult.Type)}.");
@ -737,7 +748,7 @@ namespace osu.Game.Rulesets.Objects.Drawables
/// Checks if a scoring result has occurred for this <see cref="DrawableHitObject"/>. /// Checks if a scoring result has occurred for this <see cref="DrawableHitObject"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// If a scoring result has occurred, this method must invoke <see cref="ApplyResult"/> to update the result and notify responders. /// If a scoring result has occurred, this method must invoke <see cref="ApplyResult{T}"/> to update the result and notify responders.
/// </remarks> /// </remarks>
/// <param name="userTriggered">Whether the user triggered this check.</param> /// <param name="userTriggered">Whether the user triggered this check.</param>
/// <param name="timeOffset">The offset from the end time of the <see cref="HitObject"/> at which this check occurred. /// <param name="timeOffset">The offset from the end time of the <see cref="HitObject"/> at which this check occurred.