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

161 lines
4.9 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;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Bindings;
2018-06-29 07:49:01 +00:00
using osu.Game.Rulesets.Catch.Judgements;
using osu.Game.Rulesets.Catch.Objects.Drawables;
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets.Catch.Replays;
using osu.Game.Rulesets.Judgements;
2020-09-29 05:26:36 +00:00
using osu.Game.Rulesets.Scoring;
2018-06-11 14:00:26 +00:00
using osu.Game.Rulesets.UI;
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 class CatcherArea : Container, IKeyBindingHandler<CatchAction>
2018-04-13 09:19:50 +00:00
{
2019-01-31 16:57:59 +00:00
public const float CATCHER_SIZE = 106.75f;
2018-04-13 09:19:50 +00:00
public Catcher Catcher
2021-07-19 10:44:40 +00:00
{
get => catcher;
set
{
if (catcher != null)
Remove(catcher);
Add(catcher = value);
}
}
2020-09-12 20:39:06 +00:00
private readonly CatchComboDisplay comboDisplay;
2021-07-19 10:44:40 +00:00
private Catcher catcher;
/// <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;
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()
2018-04-13 09:19:50 +00:00
{
Size = new Vector2(CatchPlayfield.WIDTH, CATCHER_SIZE);
2021-07-19 10:44:40 +00:00
Child = comboDisplay = new CatchComboDisplay
{
2021-07-19 10:44:40 +00:00
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)
2018-04-13 09:19:50 +00:00
{
Catcher.OnNewResult(hitObject, result);
2020-09-29 05:26:36 +00:00
if (!result.Type.IsScorable())
return;
if (hitObject.HitObject.LastInCombo)
2018-04-13 09:19:50 +00:00
{
if (result.Judgement is CatchJudgement catchJudgement && catchJudgement.ShouldExplodeFor(result))
Catcher.Explode();
2018-04-13 09:19:50 +00:00
else
Catcher.Drop();
2018-04-13 09:19:50 +00:00
}
2020-09-12 20:39:06 +00:00
comboDisplay.OnNewResult(hitObject, result);
2018-04-13 09:19:50 +00:00
}
public void OnRevertResult(DrawableCatchHitObject hitObject, JudgementResult result)
{
comboDisplay.OnRevertResult(hitObject, result);
Catcher.OnRevertResult(hitObject, result);
2018-04-13 09:19:50 +00:00
}
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;
}
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;
}
public bool OnPressed(CatchAction action)
{
switch (action)
{
case CatchAction.MoveLeft:
currentDirection--;
return true;
case CatchAction.MoveRight:
currentDirection++;
return true;
case CatchAction.Dash:
Catcher.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:
Catcher.Dashing = false;
break;
}
}
}
2018-04-13 09:19:50 +00:00
}