// 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 System.Linq; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Pooling; using osu.Game.Graphics; using osu.Game.Rulesets.Objects.Drawables; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Objects; using osu.Game.Rulesets.Scoring; using osu.Game.Rulesets.UI; using osu.Game.Rulesets.UI.Scrolling; using osu.Game.Rulesets.Taiko.Objects.Drawables; using osu.Game.Rulesets.Taiko.Judgements; using osu.Game.Rulesets.Taiko.Objects; using osu.Game.Rulesets.Taiko.Scoring; using osu.Game.Skinning; using osuTK; namespace osu.Game.Rulesets.Taiko.UI { public class TaikoPlayfield : ScrollingPlayfield { /// /// Default height of a when inside a . /// public const float DEFAULT_HEIGHT = 200; /// /// Whether the hit target should be nudged further towards the left area, matching the stable "classic" position. /// public Bindable ClassicHitTargetPosition = new BindableBool(); private Container hitExplosionContainer; private Container kiaiExplosionContainer; private JudgementContainer judgementContainer; private ScrollingHitObjectContainer drumRollHitContainer; internal Drawable HitTarget; private SkinnableDrawable mascot; private readonly IDictionary> judgementPools = new Dictionary>(); private readonly IDictionary explosionPools = new Dictionary(); private ProxyContainer topLevelHitContainer; private InputDrum inputDrum; private Container rightArea; /// /// is purposefully not called on this to prevent i.e. being able to interact /// with bar lines in the editor. /// private BarLinePlayfield barLinePlayfield; private Container playfieldContent; private Container playfieldOverlay; [BackgroundDependencyLoader] private void load(OsuColour colours) { inputDrum = new InputDrum(HitObjectContainer) { Anchor = Anchor.CentreLeft, Origin = Anchor.CentreLeft, AutoSizeAxes = Axes.X, RelativeSizeAxes = Axes.Y, }; InternalChildren = new[] { new SkinnableDrawable(new TaikoSkinComponent(TaikoSkinComponents.PlayfieldBackgroundRight), _ => new PlayfieldBackgroundRight()), new Container { Name = "Left overlay", RelativeSizeAxes = Axes.Both, FillMode = FillMode.Fit, BorderColour = colours.Gray0, Children = new[] { new SkinnableDrawable(new TaikoSkinComponent(TaikoSkinComponents.PlayfieldBackgroundLeft), _ => new PlayfieldBackgroundLeft()), inputDrum.CreateProxy(), } }, mascot = new SkinnableDrawable(new TaikoSkinComponent(TaikoSkinComponents.Mascot), _ => Empty()) { Origin = Anchor.BottomLeft, Anchor = Anchor.TopLeft, RelativePositionAxes = Axes.Y, RelativeSizeAxes = Axes.None, Y = 0.2f }, rightArea = new Container { Name = "Right area", RelativeSizeAxes = Axes.Both, RelativePositionAxes = Axes.Both, Children = new Drawable[] { new Container { Name = "Elements before hit objects", RelativeSizeAxes = Axes.Both, FillMode = FillMode.Fit, Children = new[] { hitExplosionContainer = new Container { RelativeSizeAxes = Axes.Both, }, HitTarget = new SkinnableDrawable(new TaikoSkinComponent(TaikoSkinComponents.HitTarget), _ => new TaikoHitTarget()) { RelativeSizeAxes = Axes.Both, } } }, new Container { Name = "Masked hit objects content", RelativeSizeAxes = Axes.Both, Masking = true, Child = playfieldContent = new Container { RelativeSizeAxes = Axes.Both, Children = new Drawable[] { barLinePlayfield = new BarLinePlayfield(), HitObjectContainer, } } }, playfieldOverlay = new Container { Name = "Elements after hit objects", RelativeSizeAxes = Axes.Both, Children = new Drawable[] { drumRollHitContainer = new DrumRollHitContainer(), kiaiExplosionContainer = new Container { Name = "Kiai hit explosions", RelativeSizeAxes = Axes.Both, FillMode = FillMode.Fit, }, judgementContainer = new JudgementContainer { Name = "Judgements", RelativeSizeAxes = Axes.Y, }, } }, } }, topLevelHitContainer = new ProxyContainer { Name = "Top level hit objects", RelativeSizeAxes = Axes.Both, }, drumRollHitContainer.CreateProxy(), // this is added at the end of the hierarchy to receive input before taiko objects. // but is proxied below everything to not cover visual effects such as hit explosions. inputDrum, }; RegisterPool(50); RegisterPool(50); RegisterPool(5); RegisterPool(5); RegisterPool(100); RegisterPool(100); RegisterPool(5); RegisterPool(100); var hitWindows = new TaikoHitWindows(); foreach (var result in Enum.GetValues(typeof(HitResult)).OfType().Where(r => hitWindows.IsHitResultAllowed(r))) { judgementPools.Add(result, new DrawablePool(15)); explosionPools.Add(result, new HitExplosionPool(result)); } AddRangeInternal(judgementPools.Values); AddRangeInternal(explosionPools.Values); } protected override void LoadComplete() { base.LoadComplete(); NewResult += OnNewResult; } protected override void OnNewDrawableHitObject(DrawableHitObject drawableHitObject) { base.OnNewDrawableHitObject(drawableHitObject); var taikoObject = (DrawableTaikoHitObject)drawableHitObject; topLevelHitContainer.Add(taikoObject.CreateProxiedContent()); } protected override void Update() { base.Update(); // Padding is required to be updated for elements which are based on "absolute" X sized elements. // This is basically allowing for correct alignment as relative pieces move around them. rightArea.Padding = new MarginPadding { Left = inputDrum.Width }; playfieldContent.Padding = new MarginPadding { Left = HitTarget.DrawWidth / 2 }; playfieldOverlay.Padding = new MarginPadding { Left = HitTarget.DrawWidth / 2 }; mascot.Scale = new Vector2(DrawHeight / DEFAULT_HEIGHT); } #region Pooling support public override void Add(HitObject h) { switch (h) { case BarLine barLine: barLinePlayfield.Add(barLine); break; case TaikoHitObject taikoHitObject: base.Add(taikoHitObject); break; default: throw new ArgumentException($"Unsupported {nameof(HitObject)} type: {h.GetType()}"); } } public override bool Remove(HitObject h) { switch (h) { case BarLine barLine: return barLinePlayfield.Remove(barLine); case TaikoHitObject taikoHitObject: return base.Remove(taikoHitObject); default: throw new ArgumentException($"Unsupported {nameof(HitObject)} type: {h.GetType()}"); } } #endregion #region Non-pooling support public override void Add(DrawableHitObject h) { switch (h) { case DrawableBarLine barLine: barLinePlayfield.Add(barLine); break; case DrawableTaikoHitObject _: base.Add(h); break; default: throw new ArgumentException($"Unsupported {nameof(DrawableHitObject)} type: {h.GetType()}"); } } public override bool Remove(DrawableHitObject h) { switch (h) { case DrawableBarLine barLine: return barLinePlayfield.Remove(barLine); case DrawableTaikoHitObject _: return base.Remove(h); default: throw new ArgumentException($"Unsupported {nameof(DrawableHitObject)} type: {h.GetType()}"); } } #endregion internal void OnNewResult(DrawableHitObject judgedObject, JudgementResult result) { if (!DisplayJudgements.Value) return; if (!judgedObject.DisplayResult) return; switch (result.Judgement) { case TaikoStrongJudgement _: if (result.IsHit) hitExplosionContainer.Children.FirstOrDefault(e => e.JudgedObject == ((DrawableStrongNestedHit)judgedObject).ParentHitObject)?.VisualiseSecondHit(result); break; case TaikoDrumRollTickJudgement _: if (!result.IsHit) break; var drawableTick = (DrawableDrumRollTick)judgedObject; addDrumRollHit(drawableTick); break; default: judgementContainer.Add(judgementPools[result.Type].Get(j => { j.Apply(result, judgedObject); j.Anchor = result.IsHit ? Anchor.TopLeft : Anchor.CentreLeft; j.Origin = result.IsHit ? Anchor.BottomCentre : Anchor.Centre; j.RelativePositionAxes = Axes.X; j.X = result.IsHit ? judgedObject.Position.X : 0; })); var type = (judgedObject.HitObject as Hit)?.Type ?? HitType.Centre; addExplosion(judgedObject, result.Type, type); break; } } private void addDrumRollHit(DrawableDrumRollTick drawableTick) => drumRollHitContainer.Add(new DrawableFlyingHit(drawableTick)); private void addExplosion(DrawableHitObject drawableObject, HitResult result, HitType type) { hitExplosionContainer.Add(explosionPools[result] .Get(explosion => explosion.Apply(drawableObject))); if (drawableObject.HitObject.Kiai) kiaiExplosionContainer.Add(new KiaiHitExplosion(drawableObject, type)); } private class ProxyContainer : LifetimeManagementContainer { public new MarginPadding Padding { set => base.Padding = value; } public void Add(Drawable proxy) => AddInternal(proxy); } } }