diff --git a/osu.Game.Rulesets.Osu/UI/OsuPlayfield.cs b/osu.Game.Rulesets.Osu/UI/OsuPlayfield.cs index c94057cf6d..411a02c5af 100644 --- a/osu.Game.Rulesets.Osu/UI/OsuPlayfield.cs +++ b/osu.Game.Rulesets.Osu/UI/OsuPlayfield.cs @@ -4,13 +4,11 @@ #nullable disable using System; -using System.Collections.Generic; using System.Diagnostics; using System.Linq; using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; -using osu.Framework.Graphics.Pooling; using osu.Game.Beatmaps; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Objects; @@ -35,6 +33,8 @@ namespace osu.Game.Rulesets.Osu.UI private readonly ProxyContainer spinnerProxies; private readonly JudgementContainer judgementLayer; + private readonly JudgementPooler judgementPooler; + public SmokeContainer Smoke { get; } public FollowPointRenderer FollowPoints { get; } @@ -42,8 +42,6 @@ namespace osu.Game.Rulesets.Osu.UI protected override GameplayCursorContainer CreateCursor() => new OsuCursorContainer(); - private readonly IDictionary> poolDictionary = new Dictionary>(); - private readonly Container judgementAboveHitObjectLayer; public OsuPlayfield() @@ -65,24 +63,15 @@ namespace osu.Game.Rulesets.Osu.UI HitPolicy = new StartTimeOrderedHitPolicy(); - foreach (var result in Enum.GetValues().Where(r => - { - switch (r) - { - case HitResult.Great: - case HitResult.Ok: - case HitResult.Meh: - case HitResult.Miss: - case HitResult.LargeTickMiss: - case HitResult.IgnoreMiss: - return true; - } - - return false; - })) - poolDictionary.Add(result, new DrawableJudgementPool(result, onJudgementLoaded)); - - AddRangeInternal(poolDictionary.Values); + AddInternal(judgementPooler = new JudgementPooler(new[] + { + HitResult.Great, + HitResult.Ok, + HitResult.Meh, + HitResult.Miss, + HitResult.LargeTickMiss, + HitResult.IgnoreMiss, + }, onJudgementLoaded)); NewResult += onNewResult; } @@ -182,10 +171,10 @@ namespace osu.Game.Rulesets.Osu.UI if (!judgedObject.DisplayResult || !DisplayJudgements.Value) return; - if (!poolDictionary.TryGetValue(result.Type, out var pool)) - return; + var explosion = judgementPooler.Get(result.Type, doj => doj.Apply(result, judgedObject)); - DrawableOsuJudgement explosion = pool.Get(doj => doj.Apply(result, judgedObject)); + if (explosion == null) + return; judgementLayer.Add(explosion); @@ -201,31 +190,6 @@ namespace osu.Game.Rulesets.Osu.UI public void Add(Drawable proxy) => AddInternal(proxy); } - private partial class DrawableJudgementPool : DrawablePool - { - private readonly HitResult result; - private readonly Action onLoaded; - - public DrawableJudgementPool(HitResult result, Action onLoaded) - : base(20) - { - this.result = result; - this.onLoaded = onLoaded; - } - - protected override DrawableOsuJudgement CreateNewDrawable() - { - var judgement = base.CreateNewDrawable(); - - // just a placeholder to initialise the correct drawable hierarchy for this pool. - judgement.Apply(new JudgementResult(new HitObject(), new Judgement()) { Type = result }, null); - - onLoaded?.Invoke(judgement); - - return judgement; - } - } - private class OsuHitObjectLifetimeEntry : HitObjectLifetimeEntry { public OsuHitObjectLifetimeEntry(HitObject hitObject) diff --git a/osu.Game/Rulesets/UI/JudgementPooler.cs b/osu.Game/Rulesets/UI/JudgementPooler.cs new file mode 100644 index 0000000000..efec760f15 --- /dev/null +++ b/osu.Game/Rulesets/UI/JudgementPooler.cs @@ -0,0 +1,77 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using System.Collections.Generic; +using osu.Framework.Allocation; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Pooling; +using osu.Game.Rulesets.Judgements; +using osu.Game.Rulesets.Objects; +using osu.Game.Rulesets.Scoring; + +namespace osu.Game.Rulesets.UI +{ + /// + /// Handles the task of preparing poolable drawable judgements for gameplay usage. + /// + /// The drawable judgement type. + public partial class JudgementPooler : CompositeComponent + where T : DrawableJudgement, new() + { + private readonly IDictionary> poolDictionary = new Dictionary>(); + + private readonly IEnumerable usableHitResults; + private readonly Action? onJudgementInitialLoad; + + public JudgementPooler(IEnumerable usableHitResults, Action? onJudgementInitialLoad = null) + { + this.usableHitResults = usableHitResults; + this.onJudgementInitialLoad = onJudgementInitialLoad; + } + + public T? Get(HitResult result, Action? setupAction) + { + if (!poolDictionary.TryGetValue(result, out var pool)) + return null; + + return pool.Get(setupAction); + } + + [BackgroundDependencyLoader] + private void load() + { + foreach (HitResult result in usableHitResults) + { + var pool = new DrawableJudgementPool(result, onJudgementInitialLoad); + poolDictionary.Add(result, pool); + AddInternal(pool); + } + } + + private partial class DrawableJudgementPool : DrawablePool + { + private readonly HitResult result; + private readonly Action? onLoaded; + + public DrawableJudgementPool(HitResult result, Action? onLoaded) + : base(20) + { + this.result = result; + this.onLoaded = onLoaded; + } + + protected override T CreateNewDrawable() + { + var judgement = base.CreateNewDrawable(); + + // just a placeholder to initialise the correct drawable hierarchy for this pool. + judgement.Apply(new JudgementResult(new HitObject(), new Judgement()) { Type = result }, null); + + onLoaded?.Invoke(judgement); + + return judgement; + } + } + } +}