2017-02-07 12:59:30 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-10-19 18:44:03 +08:00
using System ;
2017-02-16 16:02:36 +08:00
using System.Collections.Generic ;
2016-12-08 18:54:22 +08:00
using osu.Framework.Allocation ;
using osu.Framework.Audio ;
using osu.Framework.Audio.Sample ;
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Judgements ;
2016-11-26 15:51:51 +08:00
using Container = osu . Framework . Graphics . Containers . Container ;
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Objects.Types ;
2017-03-22 14:22:02 +08:00
using OpenTK.Graphics ;
2017-04-06 10:41:16 +08:00
using osu.Game.Audio ;
2017-05-11 13:48:08 +08:00
using System.Linq ;
2017-09-11 03:51:44 +08:00
using osu.Game.Graphics ;
2017-11-02 20:21:07 +08:00
using osu.Framework.Configuration ;
2017-12-01 23:26:02 +08:00
using OpenTK ;
using osu.Framework.Graphics.Primitives ;
2016-10-19 18:44:03 +08:00
2017-04-18 15:05:58 +08:00
namespace osu.Game.Rulesets.Objects.Drawables
2016-10-19 18:44:03 +08:00
{
2017-09-11 03:51:44 +08:00
public abstract class DrawableHitObject : Container , IHasAccentColour
2016-10-19 18:44:03 +08:00
{
2017-05-26 17:48:18 +08:00
public readonly HitObject HitObject ;
2017-05-11 16:07:46 +08:00
/// <summary>
/// The colour used for various elements of this DrawableHitObject.
/// </summary>
2017-09-11 12:43:52 +08:00
public virtual Color4 AccentColour { get ; set ; } = Color4 . Gray ;
2017-05-11 16:07:46 +08:00
2017-10-09 19:17:05 +08:00
/// <summary>
/// Whether a visible judgement should be displayed when this representation is hit.
/// </summary>
public virtual bool DisplayJudgement = > true ;
2017-11-02 20:21:07 +08:00
public override bool RemoveCompletedTransforms = > false ;
public override bool RemoveWhenNotAlive = > false ;
2017-05-26 17:48:18 +08:00
protected DrawableHitObject ( HitObject hitObject )
{
HitObject = hitObject ;
}
2017-12-01 23:26:02 +08:00
/// <summary>
2017-12-11 21:19:02 +08:00
/// The screen-space point that causes this <see cref="DrawableHitObject"/> to be selected in the Editor.
2017-12-01 23:26:02 +08:00
/// </summary>
2017-12-11 21:19:02 +08:00
public virtual Vector2 SelectionPoint = > ScreenSpaceDrawQuad . Centre ;
2017-12-01 23:26:02 +08:00
/// <summary>
2017-12-11 21:19:02 +08:00
/// The screen-space quad that outlines this <see cref="DrawableHitObject"/> for selections in the Editor.
2017-12-01 23:26:02 +08:00
/// </summary>
public virtual Quad SelectionQuad = > ScreenSpaceDrawQuad ;
2017-05-26 17:48:18 +08:00
}
public abstract class DrawableHitObject < TObject > : DrawableHitObject
where TObject : HitObject
{
2017-09-06 16:02:13 +08:00
public event Action < DrawableHitObject , Judgement > OnJudgement ;
2017-11-02 20:21:07 +08:00
public event Action < DrawableHitObject , Judgement > OnJudgementRemoved ;
2017-05-26 17:48:18 +08:00
2017-09-06 17:05:51 +08:00
public new readonly TObject HitObject ;
2016-11-26 15:51:51 +08:00
2017-09-06 17:05:51 +08:00
public override bool HandleInput = > Interactive ;
2017-03-15 17:55:38 +08:00
public bool Interactive = true ;
2017-09-06 16:02:13 +08:00
/// <summary>
/// Whether this <see cref="DrawableHitObject"/> can be judged.
/// </summary>
protected virtual bool ProvidesJudgement = > true ;
private readonly List < Judgement > judgements = new List < Judgement > ( ) ;
public IReadOnlyList < Judgement > Judgements = > judgements ;
2017-03-15 17:55:38 +08:00
2017-05-26 17:48:18 +08:00
protected List < SampleChannel > Samples = new List < SampleChannel > ( ) ;
2017-12-26 13:18:23 +08:00
protected virtual IEnumerable < SampleInfo > GetSamples ( ) = > HitObject . Samples ;
2017-04-05 20:34:28 +08:00
2017-11-02 20:21:07 +08:00
public readonly Bindable < ArmedState > State = new Bindable < ArmedState > ( ) ;
2017-05-26 17:48:18 +08:00
protected DrawableHitObject ( TObject hitObject )
: base ( hitObject )
{
2017-09-06 17:05:51 +08:00
HitObject = hitObject ;
2017-05-26 17:48:18 +08:00
}
2016-10-19 18:44:03 +08:00
2017-11-02 20:21:07 +08:00
[BackgroundDependencyLoader]
private void load ( AudioManager audio )
2016-10-19 18:44:03 +08:00
{
2017-12-26 13:18:23 +08:00
var samples = GetSamples ( ) ;
if ( samples . Any ( ) )
2017-12-23 16:20:14 +08:00
{
2017-12-23 17:06:46 +08:00
if ( HitObject . SampleControlPoint = = null )
2017-12-25 15:41:18 +08: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}." ) ;
2017-12-23 17:06:46 +08:00
2017-12-26 13:18:23 +08:00
foreach ( SampleInfo s in samples )
2017-12-23 16:20:14 +08:00
{
2017-12-23 17:06:46 +08:00
SampleInfo localSampleInfo = new SampleInfo
{
Bank = s . Bank ? ? HitObject . SampleControlPoint . SampleBank ,
Name = s . Name ,
Volume = s . Volume > 0 ? s . Volume : HitObject . SampleControlPoint . SampleVolume
} ;
2017-12-21 15:02:33 +08:00
2017-12-23 17:06:46 +08:00
SampleChannel channel = localSampleInfo . GetChannel ( audio . Sample ) ;
2016-10-19 18:44:03 +08:00
2017-12-23 17:06:46 +08:00
if ( channel = = null )
continue ;
2017-03-22 08:43:44 +08:00
2017-12-23 17:06:46 +08:00
Samples . Add ( channel ) ;
}
2017-11-02 20:21:07 +08:00
}
}
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
State . ValueChanged + = state = >
{
2016-10-19 18:44:03 +08:00
UpdateState ( state ) ;
2016-12-15 20:47:57 +08:00
2017-12-08 04:18:51 +08:00
if ( State = = ArmedState . Hit )
2017-04-05 20:34:28 +08:00
PlaySamples ( ) ;
2017-11-02 20:21:07 +08:00
} ;
State . TriggerChange ( ) ;
2016-10-19 18:44:03 +08:00
}
2017-04-06 11:24:17 +08:00
protected void PlaySamples ( )
{
Samples . ForEach ( s = > s ? . Play ( ) ) ;
}
2017-09-06 16:02:13 +08:00
private bool judgementOccurred ;
2017-11-02 20:54:28 +08:00
private bool judgementFinalized = > judgements . LastOrDefault ( ) ? . Final = = true ;
2017-09-06 16:02:13 +08:00
2017-05-11 13:48:08 +08:00
/// <summary>
2017-09-06 16:02:13 +08:00
/// Whether this <see cref="DrawableHitObject"/> and all of its nested <see cref="DrawableHitObject"/>s have been judged.
2017-05-11 13:48:08 +08:00
/// </summary>
2017-11-02 20:56:09 +08:00
public bool AllJudged = > ( ! ProvidesJudgement | | judgementFinalized ) & & ( NestedHitObjects ? . All ( h = > h . AllJudged ) ? ? true ) ;
2017-05-11 13:48:08 +08:00
2016-11-25 15:26:50 +08:00
/// <summary>
2017-09-06 16:02:13 +08:00
/// Notifies that a new judgement has occurred for this <see cref="DrawableHitObject"/>.
2016-11-25 15:26:50 +08:00
/// </summary>
2017-09-06 16:02:13 +08:00
/// <param name="judgement">The <see cref="Judgement"/>.</param>
protected void AddJudgement ( Judgement judgement )
2016-11-02 11:57:43 +08:00
{
2017-09-06 16:02:13 +08:00
judgementOccurred = true ;
2017-03-29 16:57:36 +08:00
2017-09-06 16:02:13 +08: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 11:57:43 +08:00
2017-09-06 16:02:13 +08:00
judgements . Add ( judgement ) ;
2016-11-25 15:26:50 +08:00
2017-09-06 16:20:41 +08:00
switch ( judgement . Result )
{
case HitResult . None :
break ;
case HitResult . Miss :
2017-11-02 20:21:07 +08:00
State . Value = ArmedState . Miss ;
2017-09-06 16:20:41 +08:00
break ;
default :
2017-11-02 20:21:07 +08:00
State . Value = ArmedState . Hit ;
2017-09-06 16:20:41 +08:00
break ;
}
2017-09-06 16:02:13 +08:00
OnJudgement ? . Invoke ( this , judgement ) ;
}
2016-11-25 15:26:50 +08:00
2017-09-06 16:02:13 +08: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 16:57:36 +08:00
2017-12-23 19:58:09 +08:00
if ( AllJudged )
2016-11-02 11:57:43 +08:00
return false ;
2017-09-06 16:02:13 +08:00
if ( NestedHitObjects ! = null )
2016-11-25 15:26:50 +08:00
{
2017-09-06 16:02:13 +08:00
foreach ( var d in NestedHitObjects )
2017-09-12 17:50:30 +08:00
judgementOccurred | = d . UpdateJudgement ( userTriggered ) ;
2017-09-06 16:02:13 +08:00
}
2017-03-29 16:57:36 +08:00
2017-11-02 20:54:28 +08:00
if ( ! ProvidesJudgement | | judgementFinalized | | judgementOccurred )
2017-09-12 17:49:50 +08:00
return judgementOccurred ;
2017-09-06 16:02:13 +08:00
var endTime = ( HitObject as IHasEndTime ) ? . EndTime ? ? HitObject . StartTime ;
CheckForJudgements ( userTriggered , Time . Current - endTime ) ;
2016-11-02 11:57:43 +08:00
2017-09-06 16:02:13 +08:00
return judgementOccurred ;
2016-11-26 15:51:51 +08:00
}
2017-09-06 16:02:13 +08: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>
protected virtual void CheckForJudgements ( bool userTriggered , double timeOffset ) { }
2017-11-02 20:21:07 +08:00
protected override void Update ( )
2016-10-19 18:44:03 +08:00
{
2017-11-02 20:21:07 +08:00
base . Update ( ) ;
2016-10-19 18:44:03 +08:00
2017-11-02 20:21:07 +08:00
var endTime = ( HitObject as IHasEndTime ) ? . EndTime ? ? HitObject . StartTime ;
2016-10-19 18:44:03 +08:00
2017-11-02 20:21:07 +08:00
while ( judgements . Count > 0 )
2017-04-06 11:24:17 +08:00
{
2017-11-02 20:21:07 +08:00
var lastJudgement = judgements [ judgements . Count - 1 ] ;
if ( lastJudgement . TimeOffset + endTime < = Time . Current )
break ;
2017-04-06 10:41:16 +08:00
2017-11-02 20:21:07 +08:00
judgements . RemoveAt ( judgements . Count - 1 ) ;
State . Value = ArmedState . Idle ;
2017-04-06 10:41:16 +08:00
2017-11-02 20:21:07 +08:00
OnJudgementRemoved ? . Invoke ( this , lastJudgement ) ;
2017-04-06 11:24:17 +08:00
}
2017-03-06 12:59:11 +08:00
}
2017-11-02 20:21:07 +08:00
protected override void UpdateAfterChildren ( )
{
base . UpdateAfterChildren ( ) ;
UpdateJudgement ( false ) ;
}
2017-09-06 17:05:51 +08:00
private List < DrawableHitObject < TObject > > nestedHitObjects ;
protected IEnumerable < DrawableHitObject < TObject > > NestedHitObjects = > nestedHitObjects ;
2017-03-06 12:59:11 +08:00
2017-09-06 17:05:51 +08:00
protected virtual void AddNested ( DrawableHitObject < TObject > h )
2017-03-06 12:59:11 +08:00
{
if ( nestedHitObjects = = null )
2017-09-06 17:05:51 +08:00
nestedHitObjects = new List < DrawableHitObject < TObject > > ( ) ;
2017-03-06 12:59:11 +08:00
2017-09-06 16:02:13 +08:00
h . OnJudgement + = ( d , j ) = > OnJudgement ? . Invoke ( d , j ) ;
2017-11-13 13:00:35 +08:00
h . OnJudgementRemoved + = ( d , j ) = > OnJudgementRemoved ? . Invoke ( d , j ) ;
2017-03-06 12:59:11 +08:00
nestedHitObjects . Add ( h ) ;
}
2017-05-26 17:48:18 +08:00
protected abstract void UpdateState ( ArmedState state ) ;
2016-10-19 18:44:03 +08:00
}
}