// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.Linq; using osu.Framework.Allocation; using osu.Framework.Configuration; using osu.Framework.Extensions.TypeExtensions; using osu.Framework.Graphics.Primitives; using osu.Game.Audio; using osu.Game.Graphics; using osu.Game.Rulesets.Judgements; using osu.Game.Rulesets.Objects.Types; using osu.Game.Rulesets.Scoring; using osu.Game.Skinning; using osuTK.Graphics; namespace osu.Game.Rulesets.Objects.Drawables { public abstract class DrawableHitObject : SkinReloadableDrawable, IHasAccentColour { public readonly HitObject HitObject; /// /// The colour used for various elements of this DrawableHitObject. /// public virtual Color4 AccentColour { get; set; } = Color4.Gray; // 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; protected SkinnableSound Samples; protected virtual IEnumerable GetSamples() => HitObject.Samples; private readonly Lazy> nestedHitObjects = new Lazy>(); public IEnumerable NestedHitObjects => nestedHitObjects.IsValueCreated ? nestedHitObjects.Value : Enumerable.Empty(); /// /// Invoked when a has been applied by this or a nested . /// public event Action OnNewResult; /// /// Invoked when a is being reverted by this or a nested . /// public event Action OnRevertResult; /// /// Whether a visual indicator should be displayed when a scoring result occurs. /// public virtual bool DisplayResult => true; /// /// Whether this and all of its nested s have been judged. /// public bool AllJudged => Judged && NestedHitObjects.All(h => h.AllJudged); /// /// Whether this has been hit. This occurs if is . /// Note: This does NOT include nested hitobjects. /// public bool IsHit => Result?.IsHit ?? false; /// /// Whether this has been judged. /// Note: This does NOT include nested hitobjects. /// public bool Judged => Result?.HasResult ?? true; /// /// The scoring result of this . /// public JudgementResult Result { get; private set; } private bool judgementOccurred; public bool Interactive = true; public override bool HandleNonPositionalInput => Interactive; public override bool HandlePositionalInput => Interactive; public override bool RemoveWhenNotAlive => false; public override bool RemoveCompletedTransforms => false; protected override bool RequiresChildrenUpdate => true; public override bool IsPresent => base.IsPresent || State.Value == ArmedState.Idle && Clock?.CurrentTime >= LifetimeStart; public readonly Bindable State = new Bindable(); protected DrawableHitObject(HitObject hitObject) { HitObject = hitObject; } [BackgroundDependencyLoader] private void load() { var judgement = HitObject.CreateJudgement(); if (judgement != null) { Result = CreateResult(judgement); if (Result == null) throw new InvalidOperationException($"{GetType().ReadableName()} must provide a {nameof(JudgementResult)} through {nameof(CreateResult)}."); } var samples = GetSamples().ToArray(); if (samples.Any()) { if (HitObject.SampleControlPoint == null) 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}."); samples = samples.Select(s => HitObject.SampleControlPoint.ApplyTo(s)).ToArray(); foreach (var s in samples) s.Namespace = SampleNamespace; AddInternal(Samples = new SkinnableSound(samples)); } } protected override void LoadComplete() { base.LoadComplete(); State.ValueChanged += state => { UpdateState(state); // apply any custom state overrides ApplyCustomUpdateState?.Invoke(this, state); if (State == ArmedState.Hit) PlaySamples(); }; State.TriggerChange(); } protected abstract void UpdateState(ArmedState state); /// /// Bind to apply a custom state which can override the default implementation. /// public event Action ApplyCustomUpdateState; /// /// Plays all the hit sounds for this . /// This is invoked automatically when this is hit. /// public void PlaySamples() => Samples?.Play(); protected override void Update() { base.Update(); if (Result != null && Result.HasResult) { var endTime = (HitObject as IHasEndTime)?.EndTime ?? HitObject.StartTime; if (Result.TimeOffset + endTime > Time.Current) { OnRevertResult?.Invoke(this, Result); Result.Type = HitResult.None; State.Value = ArmedState.Idle; } } } protected override bool ComputeIsMaskedAway(RectangleF maskingBounds) => AllJudged && base.ComputeIsMaskedAway(maskingBounds); protected override void UpdateAfterChildren() { base.UpdateAfterChildren(); UpdateResult(false); } protected virtual void AddNested(DrawableHitObject h) { h.OnNewResult += (d, r) => OnNewResult?.Invoke(d, r); h.OnRevertResult += (d, r) => OnRevertResult?.Invoke(d, r); h.ApplyCustomUpdateState += (d, j) => ApplyCustomUpdateState?.Invoke(d, j); nestedHitObjects.Value.Add(h); } /// /// Applies the of this , notifying responders such as /// the of the . /// /// The callback that applies changes to the . protected void ApplyResult(Action application) { application?.Invoke(Result); if (!Result.HasResult) throw new InvalidOperationException($"{GetType().ReadableName()} applied a {nameof(JudgementResult)} but did not update {nameof(JudgementResult.Type)}."); judgementOccurred = true; // 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; Result.TimeOffset = Time.Current - endTime; switch (Result.Type) { case HitResult.None: break; case HitResult.Miss: State.Value = ArmedState.Miss; break; default: State.Value = ArmedState.Hit; break; } OnNewResult?.Invoke(this, Result); } /// /// Should be called at least once after lifetime of this hit object is end. /// public void OnLifetimeEnd() { foreach (var nested in NestedHitObjects) nested.OnLifetimeEnd(); UpdateResult(false); } /// /// Processes this , checking if a scoring result has occurred. /// /// Whether the user triggered this process. /// Whether a scoring result has occurred from this or any nested . protected bool UpdateResult(bool userTriggered) { judgementOccurred = false; if (AllJudged) return false; foreach (var d in NestedHitObjects) judgementOccurred |= d.UpdateResult(userTriggered); if (judgementOccurred || Judged) return judgementOccurred; var endTime = (HitObject as IHasEndTime)?.EndTime ?? HitObject.StartTime; CheckForResult(userTriggered, Time.Current - endTime); return judgementOccurred; } /// /// Checks if a scoring result has occurred for this . /// /// /// If a scoring result has occurred, this method must invoke to update the result and notify responders. /// /// Whether the user triggered this check. /// The offset from the end time of the at which this check occurred. /// A > 0 implies that this check occurred after the end time of the . protected virtual void CheckForResult(bool userTriggered, double timeOffset) { } /// /// Creates the that represents the scoring result for this . /// /// The that provides the scoring information. protected virtual JudgementResult CreateResult(Judgement judgement) => new JudgementResult(judgement); } public abstract class DrawableHitObject : DrawableHitObject where TObject : HitObject { public new readonly TObject HitObject; protected DrawableHitObject(TObject hitObject) : base(hitObject) { HitObject = hitObject; } } }