1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 04:13:00 +08:00

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
commit 3c9db923b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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]
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")
{
@ -36,16 +40,34 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables
Blending = BlendingParameters.Additive,
Depth = float.MaxValue
});
}
}
if (JudgedObject != null)
{
lightingColour = JudgedObject.AccentColour.GetBoundCopy();
lightingColour.BindValueChanged(colour => lighting.Colour = colour.NewValue, true);
}
else
{
lighting.Colour = Color4.White;
}
public override void Apply(JudgementResult result, DrawableHitObject judgedObject)
{
base.Apply(result, judgedObject);
if (judgedObject?.HitObject is OsuHitObject osuObject)
{
Position = osuObject.StackedPosition;
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)
{
JudgementBody.Delay(FadeInDuration).FadeOut(400);
JudgementBody.FadeIn().Delay(FadeInDuration).FadeOut(400);
lighting.ScaleTo(0.8f).ScaleTo(1.2f, 600, Easing.Out);
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();
}
}

View File

@ -1,17 +1,23 @@
// 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 osuTK;
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Graphics;
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.Osu.Objects;
using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Rulesets.Osu.Objects.Drawables.Connections;
using osu.Game.Rulesets.UI;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Osu.Scoring;
using osu.Game.Rulesets.Osu.UI.Cursor;
using osu.Game.Rulesets.Scoring;
using osu.Game.Rulesets.UI;
using osu.Game.Skinning;
using osuTK;
namespace osu.Game.Rulesets.Osu.UI
{
@ -26,6 +32,8 @@ namespace osu.Game.Rulesets.Osu.UI
protected override GameplayCursorContainer CreateCursor() => new OsuCursorContainer();
private readonly IDictionary<HitResult, DrawablePool<DrawableOsuJudgement>> poolDictionary = new Dictionary<HitResult, DrawablePool<DrawableOsuJudgement>>();
public OsuPlayfield()
{
InternalChildren = new Drawable[]
@ -54,6 +62,13 @@ namespace osu.Game.Rulesets.Osu.UI
};
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)
@ -91,12 +106,7 @@ namespace osu.Game.Rulesets.Osu.UI
if (!judgedObject.DisplayResult || !DisplayJudgements.Value)
return;
DrawableOsuJudgement explosion = new DrawableOsuJudgement(result, judgedObject)
{
Origin = Anchor.Centre,
Position = ((OsuHitObject)judgedObject.HitObject).StackedEndPosition,
Scale = new Vector2(((OsuHitObject)judgedObject.HitObject).Scale)
};
DrawableOsuJudgement explosion = poolDictionary[result.Type].Get(doj => doj.Apply(result, judgedObject));
judgementLayer.Add(explosion);
}
@ -107,5 +117,26 @@ namespace osu.Game.Rulesets.Osu.UI
{
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.
// See the LICENCE file in the repository root for full licence text.
using System.Diagnostics;
using JetBrains.Annotations;
using osuTK;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Pooling;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
@ -18,16 +21,15 @@ namespace osu.Game.Rulesets.Judgements
/// <summary>
/// A drawable object which visualises the hit result of a <see cref="Judgements.Judgement"/>.
/// </summary>
public class DrawableJudgement : CompositeDrawable
public class DrawableJudgement : PoolableDrawable
{
private const float judgement_size = 128;
[Resolved]
private OsuColour colours { get; set; }
protected readonly JudgementResult Result;
public readonly DrawableHitObject JudgedObject;
public JudgementResult Result { get; private set; }
public DrawableHitObject JudgedObject { get; private set; }
protected Container JudgementBody;
protected SpriteText JudgementText;
@ -48,29 +50,21 @@ namespace osu.Game.Rulesets.Judgements
/// <param name="result">The judgement to visualise.</param>
/// <param name="judgedObject">The object which was judged.</param>
public DrawableJudgement(JudgementResult result, DrawableHitObject judgedObject)
: this()
{
Result = result;
JudgedObject = judgedObject;
Apply(result, judgedObject);
}
public DrawableJudgement()
{
Size = new Vector2(judgement_size);
Origin = Anchor.Centre;
}
[BackgroundDependencyLoader]
private void load()
{
InternalChild = JudgementBody = new Container
{
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)
};
prepareDrawables();
}
protected virtual void ApplyHitAnimations()
@ -81,11 +75,24 @@ namespace osu.Game.Rulesets.Judgements
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);
JudgementBody.ScaleTo(1);
JudgementBody.RotateTo(0);
JudgementBody.MoveTo(Vector2.Zero);
switch (Result.Type)
{
@ -109,5 +116,31 @@ namespace osu.Game.Rulesets.Judgements
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;
}
}
}