Split out judgement pooling concepts from `OsuPlayfield` for reuse

This commit is contained in:
Dean Herbert 2024-01-15 20:29:08 +09:00
parent 4206eccebb
commit 6b844ed8b6
No known key found for this signature in database
2 changed files with 91 additions and 50 deletions

View File

@ -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 @@ public partial class OsuPlayfield : Playfield
private readonly ProxyContainer spinnerProxies;
private readonly JudgementContainer<DrawableOsuJudgement> judgementLayer;
private readonly JudgementPooler<DrawableOsuJudgement> judgementPooler;
public SmokeContainer Smoke { get; }
public FollowPointRenderer FollowPoints { get; }
@ -42,8 +42,6 @@ public partial class OsuPlayfield : Playfield
protected override GameplayCursorContainer CreateCursor() => new OsuCursorContainer();
private readonly IDictionary<HitResult, DrawablePool<DrawableOsuJudgement>> poolDictionary = new Dictionary<HitResult, DrawablePool<DrawableOsuJudgement>>();
private readonly Container judgementAboveHitObjectLayer;
public OsuPlayfield()
@ -65,24 +63,15 @@ public OsuPlayfield()
HitPolicy = new StartTimeOrderedHitPolicy();
foreach (var result in Enum.GetValues<HitResult>().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<DrawableOsuJudgement>(new[]
{
HitResult.Great,
HitResult.Ok,
HitResult.Meh,
HitResult.Miss,
HitResult.LargeTickMiss,
HitResult.IgnoreMiss,
}, onJudgementLoaded));
NewResult += onNewResult;
}
@ -182,10 +171,10 @@ private void onNewResult(DrawableHitObject judgedObject, JudgementResult result)
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 @@ private partial class ProxyContainer : LifetimeManagementContainer
public void Add(Drawable proxy) => AddInternal(proxy);
}
private partial class DrawableJudgementPool : DrawablePool<DrawableOsuJudgement>
{
private readonly HitResult result;
private readonly Action<DrawableOsuJudgement> onLoaded;
public DrawableJudgementPool(HitResult result, Action<DrawableOsuJudgement> 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)

View File

@ -0,0 +1,77 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. 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
{
/// <summary>
/// Handles the task of preparing poolable drawable judgements for gameplay usage.
/// </summary>
/// <typeparam name="T">The drawable judgement type.</typeparam>
public partial class JudgementPooler<T> : CompositeComponent
where T : DrawableJudgement, new()
{
private readonly IDictionary<HitResult, DrawablePool<T>> poolDictionary = new Dictionary<HitResult, DrawablePool<T>>();
private readonly IEnumerable<HitResult> usableHitResults;
private readonly Action<T>? onJudgementInitialLoad;
public JudgementPooler(IEnumerable<HitResult> usableHitResults, Action<T>? onJudgementInitialLoad = null)
{
this.usableHitResults = usableHitResults;
this.onJudgementInitialLoad = onJudgementInitialLoad;
}
public T? Get(HitResult result, Action<T>? 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<T>
{
private readonly HitResult result;
private readonly Action<T>? onLoaded;
public DrawableJudgementPool(HitResult result, Action<T>? 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;
}
}
}
}