1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-24 04:07:26 +08:00
osu-lazer/osu.Game.Rulesets.Osu/Objects/Drawables/DrawableHitCircle.cs

228 lines
7.3 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 17:19:50 +08:00
using System;
using System.Diagnostics;
using JetBrains.Annotations;
2018-11-09 12:58:46 +08:00
using osu.Framework.Allocation;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Game.Rulesets.Judgements;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Osu.Judgements;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets.Osu.Objects.Drawables.Pieces;
using osu.Game.Rulesets.Scoring;
using osu.Game.Skinning;
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Rulesets.Osu.Objects.Drawables
{
public class DrawableHitCircle : DrawableOsuHitObject, IDrawableHitObjectWithProxiedApproach
{
public OsuAction? HitAction => HitArea.HitAction;
protected virtual OsuSkinComponents CirclePieceComponent => OsuSkinComponents.HitCircle;
2020-11-05 12:51:46 +08:00
public ApproachCircle ApproachCircle { get; private set; }
public HitReceptor HitArea { get; private set; }
public SkinnableDrawable CirclePiece { get; private set; }
private Container scaleContainer;
private InputManager inputManager;
public DrawableHitCircle([CanBeNull] HitCircle h = null)
2018-04-13 17:19:50 +08:00
: base(h)
{
2020-11-05 12:51:46 +08:00
}
2018-04-13 17:19:50 +08:00
2020-11-05 12:51:46 +08:00
[BackgroundDependencyLoader]
private void load()
{
Origin = Anchor.Centre;
2018-04-13 17:19:50 +08:00
InternalChildren = new Drawable[]
{
scaleContainer = new Container
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
HitArea = new HitReceptor
{
Hit = () =>
{
if (AllJudged)
return false;
UpdateResult(true);
return true;
},
},
CirclePiece = new SkinnableDrawable(new OsuSkinComponent(CirclePieceComponent), _ => new MainCirclePiece()),
ApproachCircle = new ApproachCircle
{
Alpha = 0,
Scale = new Vector2(4),
}
}
2018-04-13 17:19:50 +08:00
},
};
Size = HitArea.DrawSize;
2018-04-13 17:19:50 +08:00
PositionBindable.BindValueChanged(_ => Position = HitObject.StackedPosition);
StackHeightBindable.BindValueChanged(_ => Position = HitObject.StackedPosition);
ScaleBindable.BindValueChanged(scale => scaleContainer.Scale = new Vector2(scale.NewValue));
AccentColour.BindValueChanged(accent => ApproachCircle.Colour = accent.NewValue);
2018-04-13 17:19:50 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
inputManager = GetContainingInputManager();
}
2019-09-12 18:29:08 +08:00
public override double LifetimeStart
{
get => base.LifetimeStart;
set
{
base.LifetimeStart = value;
ApproachCircle.LifetimeStart = value;
}
}
public override double LifetimeEnd
{
get => base.LifetimeEnd;
set
{
base.LifetimeEnd = value;
ApproachCircle.LifetimeEnd = value;
}
}
protected override void CheckForResult(bool userTriggered, double timeOffset)
2018-04-13 17:19:50 +08:00
{
Debug.Assert(HitObject.HitWindows != null);
2018-04-13 17:19:50 +08:00
if (!userTriggered)
{
if (!HitObject.HitWindows.CanBeHit(timeOffset))
ApplyResult(r => r.Type = r.Judgement.MinResult);
2018-04-13 17:19:50 +08:00
return;
}
var result = HitObject.HitWindows.ResultFor(timeOffset);
2019-04-01 11:16:05 +08:00
2020-04-10 01:02:09 +08:00
if (result == HitResult.None || CheckHittable?.Invoke(this, Time.Current) == false)
{
2019-09-06 14:24:00 +08:00
Shake(Math.Abs(timeOffset) - HitObject.HitWindows.WindowFor(HitResult.Miss));
2018-04-13 17:19:50 +08:00
return;
}
2018-04-13 17:19:50 +08:00
ApplyResult(r =>
{
var circleResult = (OsuHitCircleJudgementResult)r;
2020-06-19 18:58:35 +08:00
// Todo: This should also consider misses, but they're a little more interesting to handle, since we don't necessarily know the position at the time of a miss.
if (result.IsHit())
{
var localMousePosition = ToLocalSpace(inputManager.CurrentState.Mouse.Position);
2020-06-19 18:58:35 +08:00
circleResult.CursorPositionAtHit = HitObject.StackedPosition + (localMousePosition - DrawSize / 2);
}
circleResult.Type = result;
});
2018-04-13 17:19:50 +08:00
}
2019-07-22 14:33:12 +08:00
protected override void UpdateInitialTransforms()
2018-04-13 17:19:50 +08:00
{
2019-07-22 14:33:12 +08:00
base.UpdateInitialTransforms();
2018-04-13 17:19:50 +08:00
CirclePiece.FadeInFromZero(HitObject.TimeFadeIn);
ApproachCircle.FadeIn(Math.Min(HitObject.TimeFadeIn * 2, HitObject.TimePreempt));
2019-07-24 18:32:24 +08:00
ApproachCircle.ScaleTo(1f, HitObject.TimePreempt);
ApproachCircle.Expire(true);
2018-04-13 17:19:50 +08:00
}
2020-11-04 15:19:07 +08:00
protected override void UpdateHitStateTransforms(ArmedState state)
2018-04-13 17:19:50 +08:00
{
Debug.Assert(HitObject.HitWindows != null);
2018-04-13 17:19:50 +08:00
switch (state)
{
case ArmedState.Idle:
this.Delay(HitObject.TimePreempt).FadeOut(500);
HitArea.HitAction = null;
2018-04-13 17:19:50 +08:00
break;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ArmedState.Miss:
ApproachCircle.FadeOut(50);
this.FadeOut(100);
break;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
case ArmedState.Hit:
ApproachCircle.FadeOut(50);
// todo: temporary / arbitrary
2019-09-12 18:29:08 +08:00
this.Delay(800).FadeOut();
break;
}
}
2018-04-13 17:19:50 +08:00
public Drawable ProxiedLayer => ApproachCircle;
2018-04-13 17:19:50 +08:00
protected override JudgementResult CreateResult(Judgement judgement) => new OsuHitCircleJudgementResult(HitObject, judgement);
public class HitReceptor : CompositeDrawable, IKeyBindingHandler<OsuAction>
{
// IsHovered is used
public override bool HandlePositionalInput => true;
2018-04-13 17:19:50 +08:00
public Func<bool> Hit;
2018-04-13 17:19:50 +08:00
public OsuAction? HitAction;
public HitReceptor()
{
Size = new Vector2(OsuHitObject.OBJECT_RADIUS * 2);
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
CornerRadius = OsuHitObject.OBJECT_RADIUS;
CornerExponent = 2;
2018-04-13 17:19:50 +08:00
}
public bool OnPressed(OsuAction action)
{
switch (action)
{
case OsuAction.LeftButton:
case OsuAction.RightButton:
if (IsHovered && (Hit?.Invoke() ?? false))
{
HitAction = action;
return true;
}
break;
}
return false;
}
public void OnReleased(OsuAction action)
{
}
}
2018-04-13 17:19:50 +08:00
}
}