1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-25 23:43:20 +08:00
osu-lazer/osu.Game.Rulesets.Taiko/Objects/Drawables/DrawableHit.cs

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

290 lines
9.6 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
2022-06-17 15:37:17 +08:00
#nullable disable
2018-08-03 15:56:46 +08:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
2017-04-04 11:38:55 +08:00
using System.Linq;
2020-12-15 05:53:46 +08:00
using JetBrains.Annotations;
2020-05-26 13:43:38 +08:00
using osu.Framework.Bindables;
2017-03-28 09:33:23 +08:00
using osu.Framework.Graphics;
2021-09-16 17:26:12 +08:00
using osu.Framework.Input.Events;
using osu.Game.Audio;
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Scoring;
2020-12-07 11:30:25 +08:00
using osu.Game.Rulesets.Taiko.Skinning.Default;
using osu.Game.Skinning;
2018-04-13 17:19:50 +08:00
2017-04-18 15:05:58 +08:00
namespace osu.Game.Rulesets.Taiko.Objects.Drawables
{
public class DrawableHit : DrawableTaikoStrongableHitObject<Hit, Hit.StrongNestedHit>
{
/// <summary>
/// A list of keys which can result in hits for this HitObject.
/// </summary>
2020-05-26 13:43:38 +08:00
public TaikoAction[] HitActions { get; private set; }
/// <summary>
/// The action that caused this <see cref="DrawableHit"/> to be hit.
/// </summary>
public TaikoAction? HitAction
{
get;
private set;
}
private bool validActionPressed;
2018-04-13 17:19:50 +08:00
2018-09-28 16:18:34 +08:00
private bool pressHandledThisFrame;
2020-12-15 05:53:46 +08:00
private readonly Bindable<HitType> type = new Bindable<HitType>();
2020-05-26 13:43:38 +08:00
2020-12-15 05:53:46 +08:00
public DrawableHit()
: this(null)
{
}
public DrawableHit([CanBeNull] Hit hit)
2017-03-24 13:59:59 +08:00
: base(hit)
{
FillMode = FillMode.Fit;
2020-05-26 13:43:38 +08:00
}
2020-12-15 05:53:46 +08:00
protected override void OnApply()
2020-05-26 13:43:38 +08:00
{
2020-12-15 05:53:46 +08:00
type.BindTo(HitObject.TypeBindable);
// this doesn't need to be run inline as RecreatePieces is called by the base call below.
type.BindValueChanged(_ => Scheduler.AddOnce(RecreatePieces));
2020-12-15 05:53:46 +08:00
base.OnApply();
}
protected override void RecreatePieces()
{
updateActionsFromType();
base.RecreatePieces();
}
2020-12-15 05:53:46 +08:00
protected override void OnFree()
{
base.OnFree();
type.UnbindFrom(HitObject.TypeBindable);
type.UnbindEvents();
UnproxyContent();
HitActions = null;
HitAction = null;
validActionPressed = pressHandledThisFrame = false;
}
private void updateActionsFromType()
2020-05-26 13:43:38 +08:00
{
HitActions =
HitObject.Type == HitType.Centre
? new[] { TaikoAction.LeftCentre, TaikoAction.RightCentre }
: new[] { TaikoAction.LeftRim, TaikoAction.RightRim };
}
2018-04-13 17:19:50 +08:00
protected override SkinnableDrawable CreateMainPiece() => HitObject.Type == HitType.Centre
? new SkinnableDrawable(new TaikoSkinComponent(TaikoSkinComponents.CentreHit), _ => new CentreHitCirclePiece(), confineMode: ConfineMode.ScaleToFit)
: new SkinnableDrawable(new TaikoSkinComponent(TaikoSkinComponents.RimHit), _ => new RimHitCirclePiece(), confineMode: ConfineMode.ScaleToFit);
2020-05-19 22:28:13 +08:00
public override IEnumerable<HitSampleInfo> GetSamples()
{
// normal and claps are always handled by the drum (see DrumSampleMapping).
// in addition, whistles are excluded as they are an alternative rim marker.
var samples = HitObject.Samples.Where(s =>
s.Name != HitSampleInfo.HIT_NORMAL
&& s.Name != HitSampleInfo.HIT_CLAP
&& s.Name != HitSampleInfo.HIT_WHISTLE);
if (HitObject.Type == HitType.Rim && HitObject.IsStrong)
{
// strong + rim always maps to whistle.
// TODO: this should really be in the legacy decoder, but can't be because legacy encoding parity would be broken.
// when we add a taiko editor, this is probably not going to play nice.
var corrected = samples.ToList();
for (int i = 0; i < corrected.Count; i++)
{
var s = corrected[i];
if (s.Name != HitSampleInfo.HIT_FINISH)
continue;
2020-12-01 14:37:51 +08:00
corrected[i] = s.With(HitSampleInfo.HIT_WHISTLE);
}
return corrected;
}
return samples;
}
protected override void CheckForResult(bool userTriggered, double timeOffset)
{
Debug.Assert(HitObject.HitWindows != null);
if (!userTriggered)
{
2018-02-02 17:53:30 +08:00
if (!HitObject.HitWindows.CanBeHit(timeOffset))
ApplyResult(r => r.Type = r.Judgement.MinResult);
return;
}
2018-04-13 17:19:50 +08:00
2018-02-08 13:15:47 +08:00
var result = HitObject.HitWindows.ResultFor(timeOffset);
2018-12-06 20:04:54 +08:00
if (result == HitResult.None)
return;
2018-04-13 17:19:50 +08:00
if (!validActionPressed)
ApplyResult(r => r.Type = r.Judgement.MinResult);
2018-02-02 17:53:30 +08:00
else
ApplyResult(r => r.Type = result);
}
2018-04-13 17:19:50 +08:00
2021-09-16 17:26:12 +08:00
public override bool OnPressed(KeyBindingPressEvent<TaikoAction> e)
{
2018-09-28 16:18:34 +08:00
if (pressHandledThisFrame)
return true;
if (Judged)
return false;
2021-09-16 17:26:12 +08:00
validActionPressed = HitActions.Contains(e.Action);
2018-04-13 17:19:50 +08:00
// Only count this as handled if the new judgement is a hit
bool result = UpdateResult(true);
if (IsHit)
2021-09-16 17:26:12 +08:00
HitAction = e.Action;
// Regardless of whether we've hit or not, any secondary key presses in the same frame should be discarded
// E.g. hitting a non-strong centre as a strong should not fall through and perform a hit on the next note
2018-09-28 16:18:34 +08:00
pressHandledThisFrame = true;
return result;
}
2021-09-16 17:26:12 +08:00
public override void OnReleased(KeyBindingReleaseEvent<TaikoAction> e)
{
2021-09-16 17:26:12 +08:00
if (e.Action == HitAction)
HitAction = null;
2021-09-16 17:26:12 +08:00
base.OnReleased(e);
}
2018-04-13 17:19:50 +08:00
protected override void Update()
{
base.Update();
2018-04-13 17:19:50 +08:00
// The input manager processes all input prior to us updating, so this is the perfect time
// for us to remove the extra press blocking, before input is handled in the next frame
2018-09-28 16:18:34 +08:00
pressHandledThisFrame = false;
}
2018-04-13 17:19:50 +08:00
2020-11-04 15:19:07 +08:00
protected override void UpdateHitStateTransforms(ArmedState state)
2017-03-28 09:33:23 +08:00
{
Debug.Assert(HitObject.HitWindows != null);
switch (state)
2017-03-28 09:33:23 +08:00
{
case ArmedState.Idle:
validActionPressed = false;
2019-04-01 11:16:05 +08:00
UnproxyContent();
break;
2019-04-01 11:16:05 +08:00
case ArmedState.Miss:
2019-09-12 18:29:08 +08:00
this.FadeOut(100);
break;
case ArmedState.Hit:
// If we're far enough away from the left stage, we should bring outselves in front of it
ProxyContent();
2019-04-01 11:16:05 +08:00
var flash = (MainPiece.Drawable as CirclePiece)?.FlashBox;
flash?.FadeTo(0.9f).FadeOut(300);
2018-04-13 17:19:50 +08:00
const float gravity_time = 300;
const float gravity_travel_height = 200;
2018-04-13 17:19:50 +08:00
this.ScaleTo(0.8f, gravity_time * 2, Easing.OutQuad);
2018-04-13 17:19:50 +08:00
this.MoveToY(-gravity_travel_height, gravity_time, Easing.Out)
.Then()
.MoveToY(gravity_travel_height * 2, gravity_time * 2, Easing.In);
2018-04-13 17:19:50 +08:00
2019-09-12 18:29:08 +08:00
this.FadeOut(800);
break;
2017-03-28 09:33:23 +08:00
}
}
protected override DrawableStrongNestedHit CreateStrongNestedHit(Hit.StrongNestedHit hitObject) => new StrongNestedHit(hitObject);
2018-08-03 15:56:46 +08:00
public class StrongNestedHit : DrawableStrongNestedHit
2018-08-03 15:56:46 +08:00
{
public new DrawableHit ParentHitObject => (DrawableHit)base.ParentHitObject;
2018-08-03 15:56:46 +08:00
/// <summary>
/// The lenience for the second key press.
/// This does not adjust by map difficulty in ScoreV2 yet.
/// </summary>
private const double second_hit_window = 30;
public StrongNestedHit()
: this(null)
{
}
2018-08-03 15:56:46 +08:00
public StrongNestedHit([CanBeNull] Hit.StrongNestedHit nestedHit)
: base(nestedHit)
2018-08-03 15:56:46 +08:00
{
}
protected override void CheckForResult(bool userTriggered, double timeOffset)
2018-08-03 15:56:46 +08:00
{
if (!ParentHitObject.Result.HasResult)
2018-08-03 15:56:46 +08:00
{
base.CheckForResult(userTriggered, timeOffset);
2018-08-03 15:56:46 +08:00
return;
}
if (!ParentHitObject.Result.IsHit)
2018-08-03 15:56:46 +08:00
{
2020-09-29 14:13:11 +08:00
ApplyResult(r => r.Type = r.Judgement.MinResult);
2018-08-03 15:56:46 +08:00
return;
}
2018-11-29 10:06:40 +08:00
2018-08-03 15:56:46 +08:00
if (!userTriggered)
{
if (timeOffset - ParentHitObject.Result.TimeOffset > second_hit_window)
2020-09-29 14:13:11 +08:00
ApplyResult(r => r.Type = r.Judgement.MinResult);
2018-08-03 15:56:46 +08:00
return;
}
if (Math.Abs(timeOffset - ParentHitObject.Result.TimeOffset) <= second_hit_window)
2020-09-29 14:13:11 +08:00
ApplyResult(r => r.Type = r.Judgement.MaxResult);
2018-08-03 15:56:46 +08:00
}
2021-09-16 17:26:12 +08:00
public override bool OnPressed(KeyBindingPressEvent<TaikoAction> e)
2018-08-03 15:56:46 +08:00
{
// Don't process actions until the main hitobject is hit
if (!ParentHitObject.IsHit)
2018-08-03 15:56:46 +08:00
return false;
// Don't process actions if the pressed button was released
if (ParentHitObject.HitAction == null)
2018-08-03 15:56:46 +08:00
return false;
// Don't handle invalid hit action presses
2021-09-16 17:26:12 +08:00
if (!ParentHitObject.HitActions.Contains(e.Action))
2018-08-03 15:56:46 +08:00
return false;
return UpdateResult(true);
2018-08-03 15:56:46 +08:00
}
}
}
}