Merge pull request #9431 from peppy/judgement-pooling

Add pooling support for osu! hit judgements
This commit is contained in:
Dan Balasescu
2020-07-10 21:43:00 +09:00
committed by GitHub
3 changed files with 129 additions and 43 deletions

View File

@ -24,10 +24,14 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
{ {
} }
public DrawableOsuJudgement()
{
}
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuConfigManager config) private void load(OsuConfigManager config)
{ {
if (config.Get<bool>(OsuSetting.HitLighting) && Result.Type != HitResult.Miss) if (config.Get<bool>(OsuSetting.HitLighting))
{ {
AddInternal(lighting = new SkinnableSprite("lighting") AddInternal(lighting = new SkinnableSprite("lighting")
{ {
@ -36,16 +40,34 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
Blending = BlendingParameters.Additive, Blending = BlendingParameters.Additive,
Depth = float.MaxValue Depth = float.MaxValue
}); });
}
}
if (JudgedObject != null) public override void Apply(JudgementResult result, DrawableHitObject judgedObject)
{ {
lightingColour = JudgedObject.AccentColour.GetBoundCopy(); base.Apply(result, judgedObject);
lightingColour.BindValueChanged(colour => lighting.Colour = colour.NewValue, true);
} if (judgedObject?.HitObject is OsuHitObject osuObject)
else {
{ Position = osuObject.StackedPosition;
lighting.Colour = Color4.White; Scale = new Vector2(osuObject.Scale);
} }
}
protected override void PrepareForUse()
{
base.PrepareForUse();
lightingColour?.UnbindAll();
if (JudgedObject != null)
{
lightingColour = JudgedObject.AccentColour.GetBoundCopy();
lightingColour.BindValueChanged(colour => lighting.Colour = Result.Type == HitResult.Miss ? Color4.Transparent : colour.NewValue, true);
}
else
{
lighting.Colour = Color4.White;
} }
} }
@ -55,13 +77,13 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
{ {
if (lighting != null) if (lighting != null)
{ {
JudgementBody.Delay(FadeInDuration).FadeOut(400); JudgementBody.FadeIn().Delay(FadeInDuration).FadeOut(400);
lighting.ScaleTo(0.8f).ScaleTo(1.2f, 600, Easing.Out); lighting.ScaleTo(0.8f).ScaleTo(1.2f, 600, Easing.Out);
lighting.FadeIn(200).Then().Delay(200).FadeOut(1000); lighting.FadeIn(200).Then().Delay(200).FadeOut(1000);
} }
JudgementText?.TransformSpacingTo(new Vector2(14, 0), 1800, Easing.OutQuint); JudgementText?.TransformSpacingTo(Vector2.Zero).Then().TransformSpacingTo(new Vector2(14, 0), 1800, Easing.OutQuint);
base.ApplyHitAnimations(); base.ApplyHitAnimations();
} }
} }

View File

@ -1,17 +1,23 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using osuTK; using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Pooling;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables; using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.Objects.Drawables; using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Rulesets.Osu.Objects.Drawables.Connections; using osu.Game.Rulesets.Osu.Objects.Drawables.Connections;
using osu.Game.Rulesets.UI; using osu.Game.Rulesets.Osu.Scoring;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Osu.UI.Cursor; using osu.Game.Rulesets.Osu.UI.Cursor;
using osu.Game.Rulesets.Scoring;
using osu.Game.Rulesets.UI;
using osu.Game.Skinning; using osu.Game.Skinning;
using osuTK;
namespace osu.Game.Rulesets.Osu.UI namespace osu.Game.Rulesets.Osu.UI
{ {
@ -26,6 +32,8 @@ namespace osu.Game.Rulesets.Osu.UI
protected override GameplayCursorContainer CreateCursor() => new OsuCursorContainer(); protected override GameplayCursorContainer CreateCursor() => new OsuCursorContainer();
private readonly IDictionary<HitResult, DrawablePool<DrawableOsuJudgement>> poolDictionary = new Dictionary<HitResult, DrawablePool<DrawableOsuJudgement>>();
public OsuPlayfield() public OsuPlayfield()
{ {
InternalChildren = new Drawable[] InternalChildren = new Drawable[]
@ -54,6 +62,13 @@ namespace osu.Game.Rulesets.Osu.UI
}; };
hitPolicy = new OrderedHitPolicy(HitObjectContainer); hitPolicy = new OrderedHitPolicy(HitObjectContainer);
var hitWindows = new OsuHitWindows();
foreach (var result in Enum.GetValues(typeof(HitResult)).OfType<HitResult>().Where(r => r > HitResult.None && hitWindows.IsHitResultAllowed(r)))
poolDictionary.Add(result, new DrawableJudgementPool(result));
AddRangeInternal(poolDictionary.Values);
} }
public override void Add(DrawableHitObject h) public override void Add(DrawableHitObject h)
@ -91,12 +106,7 @@ namespace osu.Game.Rulesets.Osu.UI
if (!judgedObject.DisplayResult || !DisplayJudgements.Value) if (!judgedObject.DisplayResult || !DisplayJudgements.Value)
return; return;
DrawableOsuJudgement explosion = new DrawableOsuJudgement(result, judgedObject) DrawableOsuJudgement explosion = poolDictionary[result.Type].Get(doj => doj.Apply(result, judgedObject));
{
Origin = Anchor.Centre,
Position = ((OsuHitObject)judgedObject.HitObject).StackedEndPosition,
Scale = new Vector2(((OsuHitObject)judgedObject.HitObject).Scale)
};
judgementLayer.Add(explosion); judgementLayer.Add(explosion);
} }
@ -107,5 +117,26 @@ namespace osu.Game.Rulesets.Osu.UI
{ {
public void Add(Drawable approachCircleProxy) => AddInternal(approachCircleProxy); public void Add(Drawable approachCircleProxy) => AddInternal(approachCircleProxy);
} }
private class DrawableJudgementPool : DrawablePool<DrawableOsuJudgement>
{
private readonly HitResult result;
public DrawableJudgementPool(HitResult result)
: base(10)
{
this.result = result;
}
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);
return judgement;
}
}
} }
} }

View File

@ -1,11 +1,14 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Diagnostics;
using JetBrains.Annotations;
using osuTK; using osuTK;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Extensions; using osu.Framework.Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Pooling;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics; using osu.Game.Graphics;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
@ -18,16 +21,15 @@ namespace osu.Game.Rulesets.Judgements
/// <summary> /// <summary>
/// A drawable object which visualises the hit result of a <see cref="Judgements.Judgement"/>. /// A drawable object which visualises the hit result of a <see cref="Judgements.Judgement"/>.
/// </summary> /// </summary>
public class DrawableJudgement : CompositeDrawable public class DrawableJudgement : PoolableDrawable
{ {
private const float judgement_size = 128; private const float judgement_size = 128;
[Resolved] [Resolved]
private OsuColour colours { get; set; } private OsuColour colours { get; set; }
protected readonly JudgementResult Result; public JudgementResult Result { get; private set; }
public DrawableHitObject JudgedObject { get; private set; }
public readonly DrawableHitObject JudgedObject;
protected Container JudgementBody; protected Container JudgementBody;
protected SpriteText JudgementText; protected SpriteText JudgementText;
@ -48,29 +50,21 @@ namespace osu.Game.Rulesets.Judgements
/// <param name="result">The judgement to visualise.</param> /// <param name="result">The judgement to visualise.</param>
/// <param name="judgedObject">The object which was judged.</param> /// <param name="judgedObject">The object which was judged.</param>
public DrawableJudgement(JudgementResult result, DrawableHitObject judgedObject) public DrawableJudgement(JudgementResult result, DrawableHitObject judgedObject)
: this()
{ {
Result = result; Apply(result, judgedObject);
JudgedObject = judgedObject; }
public DrawableJudgement()
{
Size = new Vector2(judgement_size); Size = new Vector2(judgement_size);
Origin = Anchor.Centre;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
InternalChild = JudgementBody = new Container prepareDrawables();
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Child = new SkinnableDrawable(new GameplaySkinComponent<HitResult>(Result.Type), _ => JudgementText = new OsuSpriteText
{
Text = Result.Type.GetDescription().ToUpperInvariant(),
Font = OsuFont.Numeric.With(size: 20),
Colour = colours.ForHitResult(Result.Type),
Scale = new Vector2(0.85f, 1),
}, confineMode: ConfineMode.NoScaling)
};
} }
protected virtual void ApplyHitAnimations() protected virtual void ApplyHitAnimations()
@ -81,11 +75,24 @@ namespace osu.Game.Rulesets.Judgements
this.Delay(FadeOutDelay).FadeOut(400); this.Delay(FadeOutDelay).FadeOut(400);
} }
protected override void LoadComplete() public virtual void Apply([NotNull] JudgementResult result, [CanBeNull] DrawableHitObject judgedObject)
{ {
base.LoadComplete(); Result = result;
JudgedObject = judgedObject;
}
protected override void PrepareForUse()
{
base.PrepareForUse();
Debug.Assert(Result != null);
prepareDrawables();
this.FadeInFromZero(FadeInDuration, Easing.OutQuint); this.FadeInFromZero(FadeInDuration, Easing.OutQuint);
JudgementBody.ScaleTo(1);
JudgementBody.RotateTo(0);
JudgementBody.MoveTo(Vector2.Zero);
switch (Result.Type) switch (Result.Type)
{ {
@ -109,5 +116,31 @@ namespace osu.Game.Rulesets.Judgements
Expire(true); Expire(true);
} }
private HitResult? currentDrawableType;
private void prepareDrawables()
{
var type = Result?.Type ?? HitResult.Perfect; //TODO: better default type from ruleset
if (type == currentDrawableType)
return;
InternalChild = JudgementBody = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
Child = new SkinnableDrawable(new GameplaySkinComponent<HitResult>(type), _ => JudgementText = new OsuSpriteText
{
Text = type.GetDescription().ToUpperInvariant(),
Font = OsuFont.Numeric.With(size: 20),
Colour = colours.ForHitResult(type),
Scale = new Vector2(0.85f, 1),
}, confineMode: ConfineMode.NoScaling)
};
currentDrawableType = type;
}
} }
} }