1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-06 17:22:54 +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.IsHit)
ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult);
ApplyMaxResult();
else
MissForcefully();
}

View File

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

View File

@ -87,7 +87,7 @@ namespace osu.Game.Rulesets.Mania.Objects.Drawables
/// <summary>
/// Causes this <see cref="DrawableManiaHitObject"/> to get missed, disregarding all conditions in implementations of <see cref="DrawableHitObject.CheckForResult"/>.
/// </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

View File

@ -91,12 +91,13 @@ namespace osu.Game.Rulesets.Mania.Objects.Drawables
if (!userTriggered)
{
if (!HitObject.HitWindows.CanBeHit(timeOffset))
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult);
ApplyMinResult();
return;
}
hitResult = HitObject.HitWindows.ResultFor(timeOffset);
if (hitResult == HitResult.None)
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)
{
// force success
ApplyResult(static (r, _) => r.Type = HitResult.Great);
ApplyResult(HitResult.Great);
}
else
base.CheckForResult(userTriggered, timeOffset);

View File

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

View File

@ -44,7 +44,6 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
private Container scaleContainer;
private InputManager inputManager;
private HitResult hitResult;
public DrawableHitCircle()
: this(null)
@ -156,12 +155,12 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
if (!userTriggered)
{
if (!HitObject.HitWindows.CanBeHit(timeOffset))
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult);
ApplyMinResult();
return;
}
hitResult = ResultFor(timeOffset);
HitResult hitResult = ResultFor(timeOffset);
var clickAction = CheckHittable?.Invoke(this, Time.Current, hitResult);
if (clickAction == ClickAction.Shake)
@ -170,20 +169,22 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
if (hitResult == HitResult.None || clickAction != ClickAction.Hit)
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;
// 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 (hitCircle.hitResult.IsHit())
{
var localMousePosition = hitCircle.ToLocalSpace(hitCircle.inputManager.CurrentState.Mouse.Position);
circleResult.CursorPositionAtHit = hitCircle.HitObject.StackedPosition + (localMousePosition - hitCircle.DrawSize / 2);
}
circleResult.Type = hitCircle.hitResult;
});
circleResult.Type = state.result;
circleResult.CursorPositionAtHit = state.position;
}, (hitResult, hitPosition));
}
/// <summary>

View File

@ -100,12 +100,12 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
/// <summary>
/// Causes this <see cref="DrawableOsuHitObject"/> to get hit, disregarding all conditions in implementations of <see cref="DrawableHitObject.CheckForResult"/>.
/// </summary>
public void HitForcefully() => ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult);
public void HitForcefully() => ApplyMaxResult();
/// <summary>
/// Causes this <see cref="DrawableOsuHitObject"/> to get missed, disregarding all conditions in implementations of <see cref="DrawableHitObject.CheckForResult"/>.
/// </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;

View File

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

View File

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

View File

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

View File

@ -101,7 +101,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
if (!userTriggered)
{
if (!HitObject.HitWindows.CanBeHit(timeOffset))
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult);
ApplyMinResult();
return;
}
@ -110,7 +110,7 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
return;
if (!validActionPressed)
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult);
ApplyMinResult();
else
{
ApplyResult(static (r, hitObject) =>
@ -217,19 +217,19 @@ namespace osu.Game.Rulesets.Taiko.Objects.Drawables
if (!ParentHitObject.Result.IsHit)
{
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult);
ApplyMinResult();
return;
}
if (!userTriggered)
{
if (timeOffset - ParentHitObject.Result.TimeOffset > SECOND_HIT_WINDOW)
ApplyResult(static (r, _) => r.Type = r.Judgement.MinResult);
ApplyMinResult();
return;
}
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)

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.
// 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())
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);
if (numHits == HitObject.RequiredHits)
ApplyResult(static (r, _) => r.Type = r.Judgement.MaxResult);
ApplyMaxResult();
}
else
{

View File

@ -216,7 +216,7 @@ namespace osu.Game.Tests.Gameplay
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)
{

View File

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

View File

@ -30,7 +30,7 @@ namespace osu.Game.Rulesets.Judgements
/// <summary>
/// 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>
/// <remarks>
/// 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);
}
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>
/// Applies the <see cref="Result"/> of this <see cref="DrawableHitObject"/>, notifying responders such as
/// the <see cref="ScoreProcessor"/> of the <see cref="JudgementResult"/>.
/// </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>
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)
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)
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"/>.
/// </summary>
/// <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>
/// <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.