osu/osu.Game.Rulesets.Catch/UI/CatcherArea.cs

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

176 lines
6.0 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.
2018-04-13 09:19:50 +00:00
using System;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Bindings;
2021-09-16 09:26:12 +00:00
using osu.Framework.Input.Events;
using osu.Game.Rulesets.Catch.Objects.Drawables;
2018-01-12 09:35:28 +00:00
using osu.Game.Rulesets.Catch.Replays;
using osu.Game.Rulesets.Judgements;
2018-06-11 14:00:26 +00:00
using osu.Game.Rulesets.UI;
2023-07-08 11:31:21 +00:00
using osu.Game.Screens.Play;
2018-11-20 07:51:59 +00:00
using osuTK;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Rulesets.Catch.UI
{
2021-07-21 07:40:35 +00:00
/// <summary>
/// The horizontal band at the bottom of the playfield the catcher is moving on.
/// It holds a <see cref="Catcher"/> as a child and translates input to the catcher movement.
/// It also holds a combo display that is above the catcher, and judgment results are translated to the catcher and the combo display.
/// </summary>
public partial class CatcherArea : Container, IKeyBindingHandler<CatchAction>
{
public Catcher Catcher
2021-07-19 10:44:40 +00:00
{
get => catcher;
set => catcherContainer.Child = catcher = value;
2021-07-19 10:44:40 +00:00
}
private readonly Container<Catcher> catcherContainer;
2020-09-12 20:39:06 +00:00
private readonly CatchComboDisplay comboDisplay;
private readonly CatcherTrailDisplay catcherTrails;
2023-01-15 07:26:11 +00:00
private Catcher catcher = null!;
2021-07-19 10:44:40 +00:00
/// <summary>
/// <c>-1</c> when only left button is pressed.
/// <c>1</c> when only right button is pressed.
/// <c>0</c> when none or both left and right buttons are pressed.
/// </summary>
private int currentDirection;
// TODO: support replay rewind
private bool lastHyperDashState;
2021-07-21 07:40:35 +00:00
/// <remarks>
/// <see cref="Catcher"/> must be set before loading.
/// </remarks>
2021-07-19 10:44:40 +00:00
public CatcherArea()
{
Size = new Vector2(CatchPlayfield.WIDTH, Catcher.BASE_SIZE);
Children = new Drawable[]
{
catcherContainer = new Container<Catcher> { RelativeSizeAxes = Axes.Both },
catcherTrails = new CatcherTrailDisplay(),
comboDisplay = new CatchComboDisplay
{
RelativeSizeAxes = Axes.None,
AutoSizeAxes = Axes.Both,
Anchor = Anchor.TopLeft,
Origin = Anchor.Centre,
Margin = new MarginPadding { Bottom = 350f },
X = CatchPlayfield.CENTER_X
}
};
}
2018-04-13 09:19:50 +00:00
public void OnNewResult(DrawableCatchHitObject hitObject, JudgementResult result)
{
Catcher.OnNewResult(hitObject, result);
comboDisplay.OnNewResult(hitObject, result);
}
2018-04-13 09:19:50 +00:00
public void OnRevertResult(JudgementResult result)
{
comboDisplay.OnRevertResult(result);
Catcher.OnRevertResult(result);
}
protected override void Update()
{
base.Update();
var replayState = (GetContainingInputManager()!.CurrentState as RulesetInputManagerInputState<CatchAction>)?.LastReplayState as CatchFramedReplayInputHandler.CatchReplayState;
SetCatcherPosition(
replayState?.CatcherX ??
(float)(Catcher.X + Catcher.Speed * currentDirection * Clock.ElapsedFrameTime));
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
comboDisplay.X = Catcher.X;
2023-07-08 11:31:21 +00:00
if ((Clock as IGameplayClock)?.IsRewinding == true)
{
// This is probably a wrong value, but currently the true value is not recorded.
2021-07-30 06:44:09 +00:00
// Setting `true` will prevent generation of false-positive after-images (with more false-negatives).
lastHyperDashState = true;
return;
}
if (!lastHyperDashState && Catcher.HyperDashing)
2021-08-02 06:08:42 +00:00
displayCatcherTrail(CatcherTrailAnimation.HyperDashAfterImage);
if (Catcher.Dashing || Catcher.HyperDashing)
{
const double trail_generation_interval = 16;
if (Time.Current - catcherTrails.LastDashTrailTime >= trail_generation_interval)
displayCatcherTrail(Catcher.HyperDashing ? CatcherTrailAnimation.HyperDashing : CatcherTrailAnimation.Dashing);
}
lastHyperDashState = Catcher.HyperDashing;
}
public void SetCatcherPosition(float x)
{
float lastPosition = Catcher.X;
float newPosition = Math.Clamp(x, 0, CatchPlayfield.WIDTH);
Catcher.X = newPosition;
if (lastPosition < newPosition)
Catcher.VisualDirection = Direction.Right;
else if (lastPosition > newPosition)
Catcher.VisualDirection = Direction.Left;
}
2021-09-16 09:26:12 +00:00
public bool OnPressed(KeyBindingPressEvent<CatchAction> e)
{
2021-09-16 09:26:12 +00:00
switch (e.Action)
{
case CatchAction.MoveLeft:
currentDirection--;
return true;
case CatchAction.MoveRight:
currentDirection++;
return true;
case CatchAction.Dash:
Catcher.Dashing = true;
return true;
}
return false;
}
2021-09-16 09:26:12 +00:00
public void OnReleased(KeyBindingReleaseEvent<CatchAction> e)
{
2021-09-16 09:26:12 +00:00
switch (e.Action)
{
case CatchAction.MoveLeft:
currentDirection++;
break;
case CatchAction.MoveRight:
currentDirection--;
break;
case CatchAction.Dash:
Catcher.Dashing = false;
break;
}
}
private void displayCatcherTrail(CatcherTrailAnimation animation) => catcherTrails.Add(new CatcherTrailEntry(Time.Current, Catcher.CurrentState, Catcher.X, Catcher.BodyScale, animation));
}
}