// 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.Linq; using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Animations; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Pooling; using osu.Framework.Input.Bindings; using osu.Framework.Utils; using osu.Game.Beatmaps; using osu.Game.Configuration; using osu.Game.Rulesets.Catch.Judgements; using osu.Game.Rulesets.Catch.Objects; using osu.Game.Rulesets.Catch.Objects.Drawables; using osu.Game.Rulesets.Catch.Skinning; using osu.Game.Rulesets.Judgements; using osu.Game.Skinning; using osuTK; using osuTK.Graphics; namespace osu.Game.Rulesets.Catch.UI { public class Catcher : SkinReloadableDrawable, IKeyBindingHandler { /// /// The default colour used to tint hyper-dash fruit, along with the moving catcher, its trail /// and end glow/after-image during a hyper-dash. /// public static readonly Color4 DEFAULT_HYPER_DASH_COLOUR = Color4.Red; /// /// The duration between transitioning to hyper-dash state. /// public const double HYPER_DASH_TRANSITION_DURATION = 180; /// /// Whether we are hyper-dashing or not. /// public bool HyperDashing => hyperDashModifier != 1; /// /// Whether fruit should appear on the plate. /// public bool CatchFruitOnPlate { get; set; } = true; /// /// The relative space to cover in 1 millisecond. based on 1 game pixel per millisecond as in osu-stable. /// public const double BASE_SPEED = 1.0; /// /// The amount by which caught fruit should be offset from the plate surface to make them look visually "caught". /// public const float CAUGHT_FRUIT_VERTICAL_OFFSET = -5; /// /// The amount by which caught fruit should be scaled down to fit on the plate. /// private const float caught_fruit_scale_adjust = 0.5f; [NotNull] private readonly Container trailsTarget; private CatcherTrailDisplay trails; /// /// Contains caught objects on the plate. /// private readonly Container caughtObjectContainer; /// /// Contains objects dropped from the plate. /// private readonly Container droppedObjectTarget; public CatcherAnimationState CurrentState { get; private set; } /// /// The width of the catcher which can receive fruit. Equivalent to "catchMargin" in osu-stable. /// public const float ALLOWED_CATCH_RANGE = 0.8f; /// /// The drawable catcher for . /// internal Drawable CurrentDrawableCatcher => currentCatcher.Drawable; private bool dashing; public bool Dashing { get => dashing; protected set { if (value == dashing) return; dashing = value; updateTrailVisibility(); } } /// /// Width of the area that can be used to attempt catches during gameplay. /// private readonly float catchWidth; private readonly CatcherSprite catcherIdle; private readonly CatcherSprite catcherKiai; private readonly CatcherSprite catcherFail; private CatcherSprite currentCatcher; private Color4 hyperDashColour = DEFAULT_HYPER_DASH_COLOUR; private Color4 hyperDashEndGlowColour = DEFAULT_HYPER_DASH_COLOUR; private int currentDirection; private double hyperDashModifier = 1; private int hyperDashDirection; private float hyperDashTargetPosition; private Bindable hitLighting; private readonly HitExplosionContainer hitExplosionContainer; private readonly DrawablePool caughtFruitPool; private readonly DrawablePool caughtBananaPool; private readonly DrawablePool caughtDropletPool; public Catcher([NotNull] Container trailsTarget, [NotNull] Container droppedObjectTarget, BeatmapDifficulty difficulty = null) { this.trailsTarget = trailsTarget; this.droppedObjectTarget = droppedObjectTarget; Origin = Anchor.TopCentre; Size = new Vector2(CatcherArea.CATCHER_SIZE); if (difficulty != null) Scale = calculateScale(difficulty); catchWidth = CalculateCatchWidth(Scale); InternalChildren = new Drawable[] { caughtFruitPool = new DrawablePool(50), caughtBananaPool = new DrawablePool(100), // less capacity is needed compared to fruit because droplet is not stacked caughtDropletPool = new DrawablePool(25), caughtObjectContainer = new Container { Anchor = Anchor.TopCentre, Origin = Anchor.BottomCentre, }, catcherIdle = new CatcherSprite(CatcherAnimationState.Idle) { Anchor = Anchor.TopCentre, Alpha = 0, }, catcherKiai = new CatcherSprite(CatcherAnimationState.Kiai) { Anchor = Anchor.TopCentre, Alpha = 0, }, catcherFail = new CatcherSprite(CatcherAnimationState.Fail) { Anchor = Anchor.TopCentre, Alpha = 0, }, hitExplosionContainer = new HitExplosionContainer { Anchor = Anchor.TopCentre, Origin = Anchor.BottomCentre, }, }; } [BackgroundDependencyLoader] private void load(OsuConfigManager config) { hitLighting = config.GetBindable(OsuSetting.HitLighting); trails = new CatcherTrailDisplay(this); updateCatcher(); } protected override void LoadComplete() { base.LoadComplete(); // don't add in above load as we may potentially modify a parent in an unsafe manner. trailsTarget.Add(trails); } /// /// Creates proxied content to be displayed beneath hitobjects. /// public Drawable CreateProxiedContent() => caughtObjectContainer.CreateProxy(); /// /// Calculates the scale of the catcher based off the provided beatmap difficulty. /// private static Vector2 calculateScale(BeatmapDifficulty difficulty) => new Vector2(1.0f - 0.7f * (difficulty.CircleSize - 5) / 5); /// /// Calculates the width of the area used for attempting catches in gameplay. /// /// The scale of the catcher. public static float CalculateCatchWidth(Vector2 scale) => CatcherArea.CATCHER_SIZE * Math.Abs(scale.X) * ALLOWED_CATCH_RANGE; /// /// Calculates the width of the area used for attempting catches in gameplay. /// /// The beatmap difficulty. public static float CalculateCatchWidth(BeatmapDifficulty difficulty) => CalculateCatchWidth(calculateScale(difficulty)); /// /// Determine if this catcher can catch a in the current position. /// public bool CanCatch(CatchHitObject hitObject) { if (!(hitObject is PalpableCatchHitObject fruit)) return false; var halfCatchWidth = catchWidth * 0.5f; // this stuff wil disappear once we move fruit to non-relative coordinate space in the future. var catchObjectPosition = fruit.EffectiveX; var catcherPosition = Position.X; return catchObjectPosition >= catcherPosition - halfCatchWidth && catchObjectPosition <= catcherPosition + halfCatchWidth; } public void OnNewResult(DrawableCatchHitObject drawableObject, JudgementResult result) { var catchResult = (CatchJudgementResult)result; catchResult.CatcherAnimationState = CurrentState; catchResult.CatcherHyperDash = HyperDashing; if (!(drawableObject is DrawablePalpableCatchHitObject palpableObject)) return; var hitObject = palpableObject.HitObject; if (result.IsHit) { var positionInStack = computePositionInStack(new Vector2(palpableObject.X - X, 0), palpableObject.DisplaySize.X); if (CatchFruitOnPlate) placeCaughtObject(palpableObject, positionInStack); if (hitLighting.Value) addLighting(hitObject, positionInStack.X, drawableObject.AccentColour.Value); } // droplet doesn't affect the catcher state if (hitObject is TinyDroplet) return; if (result.IsHit && hitObject.HyperDash) { var target = hitObject.HyperDashTarget; var timeDifference = target.StartTime - hitObject.StartTime; double positionDifference = target.EffectiveX - X; var velocity = positionDifference / Math.Max(1.0, timeDifference - 1000.0 / 60.0); SetHyperDashState(Math.Abs(velocity), target.EffectiveX); } else SetHyperDashState(); if (result.IsHit) updateState(hitObject.Kiai ? CatcherAnimationState.Kiai : CatcherAnimationState.Idle); else if (!(hitObject is Banana)) updateState(CatcherAnimationState.Fail); } public void OnRevertResult(DrawableCatchHitObject drawableObject, JudgementResult result) { var catchResult = (CatchJudgementResult)result; if (CurrentState != catchResult.CatcherAnimationState) updateState(catchResult.CatcherAnimationState); if (HyperDashing != catchResult.CatcherHyperDash) { if (catchResult.CatcherHyperDash) SetHyperDashState(2); else SetHyperDashState(); } caughtObjectContainer.RemoveAll(d => d.HitObject == drawableObject.HitObject); droppedObjectTarget.RemoveAll(d => d.HitObject == drawableObject.HitObject); } /// /// Set hyper-dash state. /// /// The speed multiplier. If this is less or equals to 1, this catcher will be non-hyper-dashing state. /// When this catcher crosses this position, this catcher ends hyper-dashing. public void SetHyperDashState(double modifier = 1, float targetPosition = -1) { var wasHyperDashing = HyperDashing; if (modifier <= 1 || X == targetPosition) { hyperDashModifier = 1; hyperDashDirection = 0; if (wasHyperDashing) runHyperDashStateTransition(false); } else { hyperDashModifier = modifier; hyperDashDirection = Math.Sign(targetPosition - X); hyperDashTargetPosition = targetPosition; if (!wasHyperDashing) { trails.DisplayEndGlow(); runHyperDashStateTransition(true); } } } public void UpdatePosition(float position) { position = Math.Clamp(position, 0, CatchPlayfield.WIDTH); if (position == X) return; Scale = new Vector2(Math.Abs(Scale.X) * (position > X ? 1 : -1), Scale.Y); X = position; } public bool OnPressed(CatchAction action) { switch (action) { case CatchAction.MoveLeft: currentDirection--; return true; case CatchAction.MoveRight: currentDirection++; return true; case CatchAction.Dash: Dashing = true; return true; } return false; } public void OnReleased(CatchAction action) { switch (action) { case CatchAction.MoveLeft: currentDirection++; break; case CatchAction.MoveRight: currentDirection--; break; case CatchAction.Dash: Dashing = false; break; } } /// /// Drop any fruit off the plate. /// public void Drop() => clearPlate(DroppedObjectAnimation.Drop); /// /// Explode all fruit off the plate. /// public void Explode() => clearPlate(DroppedObjectAnimation.Explode); private void runHyperDashStateTransition(bool hyperDashing) { updateTrailVisibility(); this.FadeColour(hyperDashing ? hyperDashColour : Color4.White, HYPER_DASH_TRANSITION_DURATION, Easing.OutQuint); } private void updateTrailVisibility() => trails.DisplayTrail = Dashing || HyperDashing; protected override void SkinChanged(ISkinSource skin, bool allowFallback) { base.SkinChanged(skin, allowFallback); hyperDashColour = skin.GetConfig(CatchSkinColour.HyperDash)?.Value ?? DEFAULT_HYPER_DASH_COLOUR; hyperDashEndGlowColour = skin.GetConfig(CatchSkinColour.HyperDashAfterImage)?.Value ?? hyperDashColour; trails.HyperDashTrailsColour = hyperDashColour; trails.EndGlowSpritesColour = hyperDashEndGlowColour; runHyperDashStateTransition(HyperDashing); } protected override void Update() { base.Update(); if (currentDirection == 0) return; var direction = Math.Sign(currentDirection); var dashModifier = Dashing ? 1 : 0.5; var speed = BASE_SPEED * dashModifier * hyperDashModifier; UpdatePosition((float)(X + direction * Clock.ElapsedFrameTime * speed)); // Correct overshooting. if ((hyperDashDirection > 0 && hyperDashTargetPosition < X) || (hyperDashDirection < 0 && hyperDashTargetPosition > X)) { X = hyperDashTargetPosition; SetHyperDashState(); } } private void updateCatcher() { currentCatcher?.Hide(); switch (CurrentState) { default: currentCatcher = catcherIdle; break; case CatcherAnimationState.Fail: currentCatcher = catcherFail; break; case CatcherAnimationState.Kiai: currentCatcher = catcherKiai; break; } currentCatcher.Show(); (currentCatcher.Drawable as IFramedAnimation)?.GotoFrame(0); } private void updateState(CatcherAnimationState state) { if (CurrentState == state) return; CurrentState = state; updateCatcher(); } private void placeCaughtObject(DrawablePalpableCatchHitObject drawableObject, Vector2 position) { var caughtObject = getCaughtObject(drawableObject.HitObject); if (caughtObject == null) return; caughtObject.CopyStateFrom(drawableObject); caughtObject.Anchor = Anchor.TopCentre; caughtObject.Position = position; caughtObject.Scale *= caught_fruit_scale_adjust; caughtObjectContainer.Add(caughtObject); if (!caughtObject.StaysOnPlate) removeFromPlate(caughtObject, DroppedObjectAnimation.Explode); } private Vector2 computePositionInStack(Vector2 position, float displayRadius) { // this is taken from osu-stable (lenience should be 10 * 10 at standard scale). const float lenience_adjust = 10 / CatchHitObject.OBJECT_RADIUS; float adjustedRadius = displayRadius * lenience_adjust; float checkDistance = MathF.Pow(adjustedRadius, 2); // offset fruit vertically to better place "above" the plate. position.Y += CAUGHT_FRUIT_VERTICAL_OFFSET; while (caughtObjectContainer.Any(f => Vector2Extensions.DistanceSquared(f.Position, position) < checkDistance)) { position.X += RNG.NextSingle(-adjustedRadius, adjustedRadius); position.Y -= RNG.NextSingle(0, 5); } return position; } private void addLighting(CatchHitObject hitObject, float x, Color4 colour) => hitExplosionContainer.Add(new HitExplosionEntry(Time.Current, x, hitObject.Scale, colour, hitObject.RandomSeed)); private CaughtObject getCaughtObject(PalpableCatchHitObject source) { switch (source) { case Fruit _: return caughtFruitPool.Get(); case Banana _: return caughtBananaPool.Get(); case Droplet _: return caughtDropletPool.Get(); default: return null; } } private CaughtObject getDroppedObject(CaughtObject caughtObject) { var droppedObject = getCaughtObject(caughtObject.HitObject); droppedObject.CopyStateFrom(caughtObject); droppedObject.Anchor = Anchor.TopLeft; droppedObject.Position = caughtObjectContainer.ToSpaceOfOtherDrawable(caughtObject.DrawPosition, droppedObjectTarget); return droppedObject; } private void clearPlate(DroppedObjectAnimation animation) { var droppedObjects = caughtObjectContainer.Children.Select(getDroppedObject).ToArray(); caughtObjectContainer.Clear(false); droppedObjectTarget.AddRange(droppedObjects); foreach (var droppedObject in droppedObjects) applyDropAnimation(droppedObject, animation); } private void removeFromPlate(CaughtObject caughtObject, DroppedObjectAnimation animation) { var droppedObject = getDroppedObject(caughtObject); caughtObjectContainer.Remove(caughtObject); droppedObjectTarget.Add(droppedObject); applyDropAnimation(droppedObject, animation); } private void applyDropAnimation(Drawable d, DroppedObjectAnimation animation) { switch (animation) { case DroppedObjectAnimation.Drop: d.MoveToY(d.Y + 75, 750, Easing.InSine); d.FadeOut(750); break; case DroppedObjectAnimation.Explode: var originalX = droppedObjectTarget.ToSpaceOfOtherDrawable(d.DrawPosition, caughtObjectContainer).X * Scale.X; d.MoveToY(d.Y - 50, 250, Easing.OutSine).Then().MoveToY(d.Y + 50, 500, Easing.InSine); d.MoveToX(d.X + originalX * 6, 1000); d.FadeOut(750); break; } d.Expire(); } private enum DroppedObjectAnimation { Drop, Explode } } }