2018-01-05 20:21:19 +09:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-02-07 13:59:30 +09:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-10-19 19:44:03 +09:00
using System ;
2017-02-16 17:02:36 +09:00
using System.Collections.Generic ;
2017-05-11 14:48:08 +09:00
using System.Linq ;
2018-02-23 20:34:08 +09:00
using osu.Framework.Allocation ;
2017-11-02 21:21:07 +09:00
using osu.Framework.Configuration ;
2018-02-23 20:34:08 +09:00
using osu.Framework.Graphics.Containers ;
2017-12-02 00:26:02 +09:00
using osu.Framework.Graphics.Primitives ;
2018-02-23 20:34:08 +09:00
using osu.Game.Audio ;
using osu.Game.Graphics ;
using osu.Game.Rulesets.Judgements ;
using osu.Game.Rulesets.Objects.Types ;
2017-12-30 21:23:18 +01:00
using osu.Game.Rulesets.Scoring ;
2018-02-22 17:34:35 +09:00
using osu.Game.Skinning ;
2018-02-23 20:34:08 +09:00
using OpenTK ;
using OpenTK.Graphics ;
2016-10-19 19:44:03 +09:00
2017-04-18 16:05:58 +09:00
namespace osu.Game.Rulesets.Objects.Drawables
2016-10-19 19:44:03 +09:00
{
2017-09-11 04:51:44 +09:00
public abstract class DrawableHitObject : Container , IHasAccentColour
2016-10-19 19:44:03 +09:00
{
2017-05-26 18:48:18 +09:00
public readonly HitObject HitObject ;
2017-05-11 17:07:46 +09:00
/// <summary>
/// The colour used for various elements of this DrawableHitObject.
/// </summary>
2017-09-11 13:43:52 +09:00
public virtual Color4 AccentColour { get ; set ; } = Color4 . Gray ;
2017-05-11 17:07:46 +09:00
2018-01-13 12:55:52 +01:00
// Todo: Rulesets should be overriding the resources instead, but we need to figure out where/when to apply overrides first
protected virtual string SampleNamespace = > null ;
2018-02-23 20:34:08 +09:00
protected SkinnableSound Samples ;
2018-02-24 23:07:02 +09:00
protected virtual IEnumerable < SampleInfo > GetSamples ( ) = > HitObject . Samples ;
2018-01-13 12:55:52 +01:00
2018-01-11 15:08:30 +09:00
private List < DrawableHitObject > nestedHitObjects ;
public IReadOnlyList < DrawableHitObject > NestedHitObjects = > nestedHitObjects ;
2018-01-13 12:42:42 +01:00
public event Action < DrawableHitObject , Judgement > OnJudgement ;
public event Action < DrawableHitObject , Judgement > OnJudgementRemoved ;
public IReadOnlyList < Judgement > Judgements = > judgements ;
private readonly List < Judgement > judgements = new List < Judgement > ( ) ;
2018-01-11 15:08:30 +09:00
2017-10-09 20:17:05 +09:00
/// <summary>
/// Whether a visible judgement should be displayed when this representation is hit.
/// </summary>
public virtual bool DisplayJudgement = > true ;
2017-12-02 00:26:02 +09:00
/// <summary>
2018-01-13 13:05:23 +01:00
/// Whether this <see cref="DrawableHitObject"/> and all of its nested <see cref="DrawableHitObject"/>s have been hit.
2017-12-02 00:26:02 +09:00
/// </summary>
2018-01-13 13:05:23 +01:00
public bool IsHit = > Judgements . Any ( j = > j . Final & & j . IsHit ) & & ( NestedHitObjects ? . All ( n = > n . IsHit ) ? ? true ) ;
2017-12-02 00:26:02 +09:00
/// <summary>
2018-01-13 12:42:42 +01:00
/// Whether this <see cref="DrawableHitObject"/> and all of its nested <see cref="DrawableHitObject"/>s have been judged.
2017-12-02 00:26:02 +09:00
/// </summary>
2018-01-13 12:42:42 +01:00
public bool AllJudged = > ( ! ProvidesJudgement | | judgementFinalized ) & & ( NestedHitObjects ? . All ( h = > h . AllJudged ) ? ? true ) ;
2017-03-15 18:55:38 +09:00
2017-09-06 17:02:13 +09:00
/// <summary>
/// Whether this <see cref="DrawableHitObject"/> can be judged.
/// </summary>
protected virtual bool ProvidesJudgement = > true ;
2018-01-13 12:42:42 +01:00
private bool judgementOccurred ;
private bool judgementFinalized = > judgements . LastOrDefault ( ) ? . Final = = true ;
2017-03-15 18:55:38 +09:00
2018-01-13 12:42:42 +01:00
public bool Interactive = true ;
2018-01-16 19:47:55 +09:00
public override bool HandleKeyboardInput = > Interactive ;
public override bool HandleMouseInput = > Interactive ;
2017-04-05 21:34:28 +09:00
2018-01-13 12:42:42 +01:00
public override bool RemoveWhenNotAlive = > false ;
public override bool RemoveCompletedTransforms = > false ;
protected override bool RequiresChildrenUpdate = > true ;
2017-12-26 19:55:56 +09:00
2017-11-02 21:21:07 +09:00
public readonly Bindable < ArmedState > State = new Bindable < ArmedState > ( ) ;
2018-01-13 12:42:42 +01:00
protected DrawableHitObject ( HitObject hitObject )
2017-05-26 18:48:18 +09:00
{
2017-09-06 18:05:51 +09:00
HitObject = hitObject ;
2017-05-26 18:48:18 +09:00
}
2016-10-19 19:44:03 +09:00
2017-11-02 21:21:07 +09:00
[BackgroundDependencyLoader]
2018-02-23 20:34:08 +09:00
private void load ( )
2016-10-19 19:44:03 +09:00
{
2018-02-23 20:34:08 +09:00
var samples = GetSamples ( ) . ToArray ( ) ;
2017-12-26 14:18:23 +09:00
if ( samples . Any ( ) )
2017-12-23 17:20:14 +09:00
{
2017-12-23 18:06:46 +09:00
if ( HitObject . SampleControlPoint = = null )
2017-12-25 16:41:18 +09:00
throw new ArgumentNullException ( nameof ( HitObject . SampleControlPoint ) , $"{nameof(HitObject)}s must always have an attached {nameof(HitObject.SampleControlPoint)}."
+ $" This is an indication that {nameof(HitObject.ApplyDefaults)} has not been invoked on {this}." ) ;
2018-02-23 20:34:08 +09:00
AddInternal ( Samples = new SkinnableSound ( samples . Select ( s = > new SampleInfo
2017-12-23 17:20:14 +09:00
{
2018-02-23 20:34:08 +09:00
Bank = s . Bank ? ? HitObject . SampleControlPoint . SampleBank ,
Name = s . Name ,
Volume = s . Volume > 0 ? s . Volume : HitObject . SampleControlPoint . SampleVolume ,
Namespace = SampleNamespace
} ) . ToArray ( ) ) ) ;
2017-11-02 21:21:07 +09:00
}
}
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
State . ValueChanged + = state = >
{
2016-10-19 19:44:03 +09:00
UpdateState ( state ) ;
2016-12-15 21:47:57 +09:00
2017-12-26 17:25:18 +01:00
// apply any custom state overrides
ApplyCustomUpdateState ? . Invoke ( this , state ) ;
2017-12-07 21:18:51 +01:00
if ( State = = ArmedState . Hit )
2017-04-05 21:34:28 +09:00
PlaySamples ( ) ;
2017-11-02 21:21:07 +09:00
} ;
State . TriggerChange ( ) ;
2016-10-19 19:44:03 +09:00
}
2018-01-13 12:42:42 +01:00
protected abstract void UpdateState ( ArmedState state ) ;
/// <summary>
/// Bind to apply a custom state which can override the default implementation.
/// </summary>
public event Action < DrawableHitObject , ArmedState > ApplyCustomUpdateState ;
2018-01-24 20:05:11 +09:00
/// <summary>
/// Plays all the hitsounds for this <see cref="DrawableHitObject"/>.
/// </summary>
2018-02-23 20:34:08 +09:00
public void PlaySamples ( ) = > Samples ? . Play ( ) ;
2018-01-13 12:55:52 +01:00
2018-01-13 12:42:42 +01:00
protected override void Update ( )
2017-04-06 12:24:17 +09:00
{
2018-01-13 12:42:42 +01:00
base . Update ( ) ;
var endTime = ( HitObject as IHasEndTime ) ? . EndTime ? ? HitObject . StartTime ;
while ( judgements . Count > 0 )
{
var lastJudgement = judgements [ judgements . Count - 1 ] ;
if ( lastJudgement . TimeOffset + endTime < = Time . Current )
break ;
judgements . RemoveAt ( judgements . Count - 1 ) ;
State . Value = ArmedState . Idle ;
OnJudgementRemoved ? . Invoke ( this , lastJudgement ) ;
}
2017-04-06 12:24:17 +09:00
}
2018-01-13 12:42:42 +01:00
protected override void UpdateAfterChildren ( )
2017-04-06 12:24:17 +09:00
{
2018-01-13 12:42:42 +01:00
base . UpdateAfterChildren ( ) ;
2017-09-06 17:02:13 +09:00
2018-01-13 12:42:42 +01:00
UpdateJudgement ( false ) ;
2017-04-06 12:24:17 +09:00
}
2018-01-13 12:42:42 +01:00
protected virtual void AddNested ( DrawableHitObject h )
{
if ( nestedHitObjects = = null )
nestedHitObjects = new List < DrawableHitObject > ( ) ;
2017-09-06 17:02:13 +09:00
2018-01-15 20:35:38 +09:00
h . OnJudgement + = ( d , j ) = > OnJudgement ? . Invoke ( d , j ) ;
h . OnJudgementRemoved + = ( d , j ) = > OnJudgementRemoved ? . Invoke ( d , j ) ;
h . ApplyCustomUpdateState + = ( d , j ) = > ApplyCustomUpdateState ? . Invoke ( d , j ) ;
2018-01-13 12:42:42 +01:00
nestedHitObjects . Add ( h ) ;
}
2017-05-11 14:48:08 +09:00
2016-11-25 16:26:50 +09:00
/// <summary>
2017-09-06 17:02:13 +09:00
/// Notifies that a new judgement has occurred for this <see cref="DrawableHitObject"/>.
2016-11-25 16:26:50 +09:00
/// </summary>
2017-09-06 17:02:13 +09:00
/// <param name="judgement">The <see cref="Judgement"/>.</param>
protected void AddJudgement ( Judgement judgement )
2016-11-02 12:57:43 +09:00
{
2017-09-06 17:02:13 +09:00
judgementOccurred = true ;
2017-03-29 17:57:36 +09:00
2017-09-06 17:02:13 +09:00
// Ensure that the judgement is given a valid time offset, because this may not get set by the caller
var endTime = ( HitObject as IHasEndTime ) ? . EndTime ? ? HitObject . StartTime ;
judgement . TimeOffset = Time . Current - endTime ;
2016-11-02 12:57:43 +09:00
2017-09-06 17:02:13 +09:00
judgements . Add ( judgement ) ;
2016-11-25 16:26:50 +09:00
2017-09-06 17:20:41 +09:00
switch ( judgement . Result )
{
case HitResult . None :
break ;
case HitResult . Miss :
2017-11-02 21:21:07 +09:00
State . Value = ArmedState . Miss ;
2017-09-06 17:20:41 +09:00
break ;
default :
2017-11-02 21:21:07 +09:00
State . Value = ArmedState . Hit ;
2017-09-06 17:20:41 +09:00
break ;
}
2017-09-06 17:02:13 +09:00
OnJudgement ? . Invoke ( this , judgement ) ;
}
2016-11-25 16:26:50 +09:00
2017-09-06 17:02:13 +09:00
/// <summary>
/// Processes this <see cref="DrawableHitObject"/>, checking if any judgements have occurred.
/// </summary>
/// <param name="userTriggered">Whether the user triggered this process.</param>
/// <returns>Whether a judgement has occurred from this <see cref="DrawableHitObject"/> or any nested <see cref="DrawableHitObject"/>s.</returns>
protected bool UpdateJudgement ( bool userTriggered )
{
judgementOccurred = false ;
2017-03-29 17:57:36 +09:00
2017-12-23 20:58:09 +09:00
if ( AllJudged )
2016-11-02 12:57:43 +09:00
return false ;
2017-09-06 17:02:13 +09:00
if ( NestedHitObjects ! = null )
foreach ( var d in NestedHitObjects )
2017-09-12 18:50:30 +09:00
judgementOccurred | = d . UpdateJudgement ( userTriggered ) ;
2017-03-29 17:57:36 +09:00
2017-11-02 21:54:28 +09:00
if ( ! ProvidesJudgement | | judgementFinalized | | judgementOccurred )
2017-09-12 18:49:50 +09:00
return judgementOccurred ;
2017-09-06 17:02:13 +09:00
var endTime = ( HitObject as IHasEndTime ) ? . EndTime ? ? HitObject . StartTime ;
CheckForJudgements ( userTriggered , Time . Current - endTime ) ;
2016-11-02 12:57:43 +09:00
2017-09-06 17:02:13 +09:00
return judgementOccurred ;
2016-11-26 16:51:51 +09:00
}
2017-09-06 17:02:13 +09:00
/// <summary>
/// Checks if any judgements have occurred for this <see cref="DrawableHitObject"/>. This method must construct
/// all <see cref="Judgement"/>s and notify of them through <see cref="AddJudgement"/>.
/// </summary>
/// <param name="userTriggered">Whether the user triggered this check.</param>
/// <param name="timeOffset">The offset from the <see cref="HitObject"/> end time at which this check occurred. A <paramref name="timeOffset"/> > 0
/// implies that this check occurred after the end time of <see cref="HitObject"/>. </param>
2018-01-13 12:42:42 +01:00
protected virtual void CheckForJudgements ( bool userTriggered , double timeOffset )
2016-10-19 19:44:03 +09:00
{
2017-03-06 13:59:11 +09:00
}
2018-01-13 12:42:42 +01:00
/// <summary>
/// The screen-space point that causes this <see cref="DrawableHitObject"/> to be selected in the Editor.
/// </summary>
public virtual Vector2 SelectionPoint = > ScreenSpaceDrawQuad . Centre ;
2017-11-02 21:21:07 +09:00
2018-01-13 12:42:42 +01:00
/// <summary>
/// The screen-space quad that outlines this <see cref="DrawableHitObject"/> for selections in the Editor.
/// </summary>
public virtual Quad SelectionQuad = > ScreenSpaceDrawQuad ;
}
2017-11-02 21:21:07 +09:00
2018-01-13 12:42:42 +01:00
public abstract class DrawableHitObject < TObject > : DrawableHitObject
where TObject : HitObject
{
public new readonly TObject HitObject ;
2017-03-06 13:59:11 +09:00
2018-01-13 12:42:42 +01:00
protected DrawableHitObject ( TObject hitObject )
: base ( hitObject )
2017-03-06 13:59:11 +09:00
{
2018-01-13 12:42:42 +01:00
HitObject = hitObject ;
2017-03-06 13:59:11 +09:00
}
2016-10-19 19:44:03 +09:00
}
}