1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 06:32:54 +08:00
osu-lazer/osu.Game.Rulesets.Catch/UI/Catcher.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

465 lines
17 KiB
C#
Raw Normal View History

// 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;
2023-01-15 15:26:11 +08:00
using System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
2020-07-14 11:52:34 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-12-01 10:32:20 +08:00
using osu.Framework.Graphics.Pooling;
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
{
2021-07-26 01:00:37 +08:00
[Cached]
public partial class Catcher : SkinReloadableDrawable
{
/// <summary>
/// The size of the catcher at 1x scale.
/// </summary>
public const float BASE_SIZE = 106.75f;
/// <summary>
/// The width of the catcher which can receive fruit. Equivalent to "catchMargin" in osu-stable.
/// </summary>
public const float ALLOWED_CATCH_RANGE = 0.8f;
/// <summary>
/// The default colour used to tint hyper-dash fruit, along with the moving catcher, its trail and after-image during a hyper-dash.
/// </summary>
public static readonly Color4 DEFAULT_HYPER_DASH_COLOUR = Color4.Red;
/// <summary>
/// The duration between transitioning to hyper-dash state.
/// </summary>
public const double HYPER_DASH_TRANSITION_DURATION = 180;
/// <summary>
/// Whether we are hyper-dashing or not.
/// </summary>
public bool HyperDashing => hyperDashModifier != 1;
2021-03-30 13:33:55 +08:00
/// <summary>
/// Whether <see cref="DrawablePalpableCatchHitObject"/> fruit should appear on the plate.
/// </summary>
public bool CatchFruitOnPlate { get; set; } = true;
/// <summary>
/// The speed of the catcher when the catcher is dashing.
/// </summary>
public const double BASE_DASH_SPEED = 1.0;
/// <summary>
/// The speed of the catcher when the catcher is not dashing.
/// </summary>
public const double BASE_WALK_SPEED = 0.5;
/// <summary>
/// The current speed of the catcher with the hyper-dash modifier applied.
/// </summary>
public double Speed => (Dashing ? BASE_DASH_SPEED : BASE_WALK_SPEED) * hyperDashModifier;
/// <summary>
/// The amount by which caught fruit should be scaled down to fit on the plate.
/// </summary>
private const float caught_fruit_scale_adjust = 0.5f;
/// <summary>
/// Contains caught objects on the plate.
/// </summary>
private readonly Container<CaughtObject> caughtObjectContainer;
/// <summary>
/// Contains objects dropped from the plate.
/// </summary>
private readonly DroppedObjectContainer droppedObjectTarget;
public CatcherAnimationState CurrentState
{
get => body.AnimationState.Value;
private set => body.AnimationState.Value = value;
}
2021-07-26 16:50:10 +08:00
/// <summary>
/// Whether the catcher is currently dashing.
/// </summary>
public bool Dashing { get; set; }
/// <summary>
/// The currently facing direction.
/// </summary>
public Direction VisualDirection { get; set; } = Direction.Right;
public Vector2 BodyScale => Scale * body.Scale;
/// <summary>
/// Whether the contents of the catcher plate should be visually flipped when the catcher direction is changed.
/// </summary>
private bool flipCatcherPlate;
/// <summary>
/// Width of the area that can be used to attempt catches during gameplay.
/// </summary>
2021-07-26 01:00:37 +08:00
public readonly float CatchWidth;
private readonly SkinnableCatcher body;
private Color4 hyperDashColour = DEFAULT_HYPER_DASH_COLOUR;
private double hyperDashModifier = 1;
private int hyperDashDirection;
private float hyperDashTargetPosition;
2023-01-15 15:26:11 +08:00
private Bindable<bool> hitLighting = null!;
private readonly HitExplosionContainer hitExplosionContainer;
2020-12-01 10:32:20 +08:00
private readonly DrawablePool<CaughtFruit> caughtFruitPool;
private readonly DrawablePool<CaughtBanana> caughtBananaPool;
private readonly DrawablePool<CaughtDroplet> caughtDropletPool;
2023-01-15 15:26:11 +08:00
public Catcher(DroppedObjectContainer droppedObjectTarget, IBeatmapDifficultyInfo? difficulty = null)
{
this.droppedObjectTarget = droppedObjectTarget;
Origin = Anchor.TopCentre;
Size = new Vector2(BASE_SIZE);
if (difficulty != null)
2020-04-22 12:37:49 +08:00
Scale = calculateScale(difficulty);
2021-07-26 01:00:37 +08:00
CatchWidth = CalculateCatchWidth(Scale);
InternalChildren = new Drawable[]
{
caughtFruitPool = new DrawablePool<CaughtFruit>(50),
caughtBananaPool = new DrawablePool<CaughtBanana>(100),
// less capacity is needed compared to fruit because droplet is not stacked
caughtDropletPool = new DrawablePool<CaughtDroplet>(25),
caughtObjectContainer = new Container<CaughtObject>
2020-12-02 18:28:47 +08:00
{
Anchor = Anchor.TopCentre,
Origin = Anchor.BottomCentre,
// offset fruit vertically to better place "above" the plate.
Y = -5
2020-12-02 18:28:47 +08:00
},
body = new SkinnableCatcher(),
hitExplosionContainer = new HitExplosionContainer
2020-12-01 10:32:20 +08:00
{
Anchor = Anchor.TopCentre,
Origin = Anchor.BottomCentre,
},
};
2020-12-02 18:28:47 +08:00
}
2020-12-02 18:28:47 +08:00
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
hitLighting = config.GetBindable<bool>(OsuSetting.HitLighting);
}
2020-07-16 14:35:19 +08:00
/// <summary>
/// Creates proxied content to be displayed beneath hitobjects.
/// </summary>
public Drawable CreateProxiedContent() => caughtObjectContainer.CreateProxy();
2020-07-16 14:35:19 +08:00
/// <summary>
/// Calculates the scale of the catcher based off the provided beatmap difficulty.
/// </summary>
private static Vector2 calculateScale(IBeatmapDifficultyInfo difficulty) => new Vector2(1.0f - 0.7f * (difficulty.CircleSize - 5) / 5);
/// <summary>
/// Calculates the width of the area used for attempting catches in gameplay.
/// </summary>
2020-04-22 12:36:59 +08:00
/// <param name="scale">The scale of the catcher.</param>
public static float CalculateCatchWidth(Vector2 scale) => BASE_SIZE * Math.Abs(scale.X) * ALLOWED_CATCH_RANGE;
/// <summary>
/// Calculates the width of the area used for attempting catches in gameplay.
/// </summary>
2020-04-22 12:36:59 +08:00
/// <param name="difficulty">The beatmap difficulty.</param>
public static float CalculateCatchWidth(IBeatmapDifficultyInfo difficulty) => CalculateCatchWidth(calculateScale(difficulty));
/// <summary>
/// Determine if this catcher can catch a <see cref="CatchHitObject"/> in the current position.
/// </summary>
public bool CanCatch(CatchHitObject hitObject)
{
if (!(hitObject is PalpableCatchHitObject fruit))
return false;
2021-07-26 01:00:37 +08:00
float halfCatchWidth = CatchWidth * 0.5f;
return fruit.EffectiveX >= X - halfCatchWidth &&
fruit.EffectiveX <= X + halfCatchWidth;
}
2021-03-30 13:33:55 +08:00
public void OnNewResult(DrawableCatchHitObject drawableObject, JudgementResult result)
{
var catchResult = (CatchJudgementResult)result;
catchResult.CatcherAnimationState = CurrentState;
catchResult.CatcherHyperDash = HyperDashing;
// Ignore JuiceStreams and BananaShowers
2020-12-09 09:25:35 +08:00
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);
2021-03-30 13:33:55 +08:00
if (CatchFruitOnPlate)
placeCaughtObject(palpableObject, positionInStack);
if (hitLighting.Value)
addLighting(result, drawableObject.AccentColour.Value, positionInStack.X);
}
2020-12-04 09:24:25 +08:00
// droplet doesn't affect the catcher state
if (hitObject is TinyDroplet) return;
2020-12-04 09:24:25 +08:00
2023-01-15 15:26:11 +08:00
if (result.IsHit && hitObject.HyperDashTarget is CatchHitObject target)
{
double timeDifference = target.StartTime - hitObject.StartTime;
double positionDifference = target.EffectiveX - X;
double velocity = positionDifference / Math.Max(1.0, timeDifference - 1000.0 / 60.0);
2020-12-04 09:24:25 +08:00
SetHyperDashState(Math.Abs(velocity) / BASE_DASH_SPEED, target.EffectiveX);
}
2020-12-04 09:24:25 +08:00
else
SetHyperDashState();
if (result.IsHit)
CurrentState = hitObject.Kiai ? CatcherAnimationState.Kiai : CatcherAnimationState.Idle;
else if (!(hitObject is Banana))
CurrentState = CatcherAnimationState.Fail;
if (palpableObject.HitObject.LastInCombo)
{
if (result.Judgement is CatchJudgement catchJudgement && catchJudgement.ShouldExplodeFor(result))
Explode();
else
Drop();
}
}
2020-12-08 14:24:39 +08:00
public void OnRevertResult(DrawableCatchHitObject drawableObject, JudgementResult result)
{
var catchResult = (CatchJudgementResult)result;
CurrentState = catchResult.CatcherAnimationState;
if (HyperDashing != catchResult.CatcherHyperDash)
{
if (catchResult.CatcherHyperDash)
SetHyperDashState(2);
else
SetHyperDashState();
}
2020-12-08 14:24:39 +08:00
caughtObjectContainer.RemoveAll(d => d.HitObject == drawableObject.HitObject, false);
droppedObjectTarget.RemoveAll(d => d.HitObject == drawableObject.HitObject, false);
}
/// <summary>
/// Set hyper-dash state.
/// </summary>
/// <param name="modifier">The speed multiplier. If this is less or equals to 1, this catcher will be non-hyper-dashing state.</param>
/// <param name="targetPosition">When this catcher crosses this position, this catcher ends hyper-dashing.</param>
public void SetHyperDashState(double modifier = 1, float targetPosition = -1)
{
bool 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)
runHyperDashStateTransition(true);
}
}
/// <summary>
/// Drop any fruit off the plate.
/// </summary>
public void Drop() => clearPlate(DroppedObjectAnimation.Drop);
/// <summary>
/// Explode all fruit off the plate.
/// </summary>
public void Explode() => clearPlate(DroppedObjectAnimation.Explode);
2020-12-04 13:36:40 +08:00
private void runHyperDashStateTransition(bool hyperDashing)
{
this.FadeColour(hyperDashing ? hyperDashColour : Color4.White, HYPER_DASH_TRANSITION_DURATION, Easing.OutQuint);
2020-12-04 13:36:40 +08:00
}
protected override void SkinChanged(ISkinSource skin)
{
base.SkinChanged(skin);
hyperDashColour =
skin.GetConfig<CatchSkinColour, Color4>(CatchSkinColour.HyperDash)?.Value ??
DEFAULT_HYPER_DASH_COLOUR;
flipCatcherPlate = skin.GetConfig<CatchSkinConfiguration, bool>(CatchSkinConfiguration.FlipCatcherPlate)?.Value ?? true;
runHyperDashStateTransition(HyperDashing);
}
protected override void Update()
{
base.Update();
var scaleFromDirection = new Vector2((int)VisualDirection, 1);
body.Scale = scaleFromDirection;
caughtObjectContainer.Scale = hitExplosionContainer.Scale = flipCatcherPlate ? scaleFromDirection : Vector2.One;
// Correct overshooting.
2020-03-14 14:35:59 +08:00
if ((hyperDashDirection > 0 && hyperDashTargetPosition < X) ||
(hyperDashDirection < 0 && hyperDashTargetPosition > X))
{
X = hyperDashTargetPosition;
SetHyperDashState();
}
}
2020-12-09 09:25:35 +08:00
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);
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(JudgementResult judgementResult, Color4 colour, float x) =>
hitExplosionContainer.Add(new HitExplosionEntry(Time.Current, judgementResult, colour, x));
2023-01-15 15:26:11 +08:00
private CaughtObject? getCaughtObject(PalpableCatchHitObject source)
{
switch (source)
{
2022-06-24 20:25:23 +08:00
case Fruit:
return caughtFruitPool.Get();
2022-06-24 20:25:23 +08:00
case Banana:
return caughtBananaPool.Get();
2022-06-24 20:25:23 +08:00
case Droplet:
return caughtDropletPool.Get();
default:
return null;
}
}
private CaughtObject getDroppedObject(CaughtObject caughtObject)
{
var droppedObject = getCaughtObject(caughtObject.HitObject);
2023-01-15 15:26:11 +08:00
Debug.Assert(droppedObject != null);
droppedObject.CopyStateFrom(caughtObject);
droppedObject.Anchor = Anchor.TopLeft;
droppedObject.Position = caughtObjectContainer.ToSpaceOfOtherDrawable(caughtObject.DrawPosition, droppedObjectTarget);
return droppedObject;
}
private void clearPlate(DroppedObjectAnimation animation)
{
2020-12-09 09:36:54 +08:00
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, false);
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:
float originalX = droppedObjectTarget.ToSpaceOfOtherDrawable(d.DrawPosition, caughtObjectContainer).X * caughtObjectContainer.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();
}
2020-12-03 17:45:38 +08:00
private enum DroppedObjectAnimation
{
Drop,
Explode
}
}
}