2021-08-24 17:23:02 +08:00
|
|
|
// 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.
|
|
|
|
|
2023-06-20 19:45:02 +08:00
|
|
|
using System.Collections.Generic;
|
2021-08-24 17:23:02 +08:00
|
|
|
using System.Linq;
|
2023-06-20 19:47:56 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-08-24 17:23:02 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Game.Audio;
|
|
|
|
using osu.Game.Rulesets.Objects;
|
2023-06-20 19:04:02 +08:00
|
|
|
using osu.Game.Rulesets.Scoring;
|
2023-06-20 19:47:56 +08:00
|
|
|
using osu.Game.Screens.Play;
|
2021-08-24 17:23:02 +08:00
|
|
|
using osu.Game.Skinning;
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.UI
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A component which can trigger the most appropriate hit sound for a given point in time, based on the state of a <see cref="HitObjectContainer"/>
|
|
|
|
/// </summary>
|
2022-11-24 13:32:20 +08:00
|
|
|
public partial class GameplaySampleTriggerSource : CompositeDrawable
|
2021-08-24 17:23:02 +08:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The number of concurrent samples allowed to be played concurrently so that it feels better when spam-pressing a key.
|
|
|
|
/// </summary>
|
|
|
|
private const int max_concurrent_hitsounds = OsuGameBase.SAMPLE_CONCURRENCY;
|
|
|
|
|
2021-08-25 13:57:41 +08:00
|
|
|
private readonly HitObjectContainer hitObjectContainer;
|
|
|
|
|
|
|
|
private int nextHitSoundIndex;
|
|
|
|
|
2021-08-24 17:23:02 +08:00
|
|
|
private readonly Container<SkinnableSound> hitSounds;
|
|
|
|
|
2023-06-20 20:03:55 +08:00
|
|
|
private HitObjectLifetimeEntry? mostValidObject;
|
|
|
|
|
2023-06-20 19:47:56 +08:00
|
|
|
[Resolved]
|
2023-06-21 16:33:42 +08:00
|
|
|
private IGameplayClock? gameplayClock { get; set; }
|
2023-06-20 19:47:56 +08:00
|
|
|
|
2021-08-24 17:23:02 +08:00
|
|
|
public GameplaySampleTriggerSource(HitObjectContainer hitObjectContainer)
|
|
|
|
{
|
|
|
|
this.hitObjectContainer = hitObjectContainer;
|
2021-08-25 14:29:47 +08:00
|
|
|
|
|
|
|
InternalChild = hitSounds = new Container<SkinnableSound>
|
2021-08-24 17:23:02 +08:00
|
|
|
{
|
2021-08-25 14:29:47 +08:00
|
|
|
Name = "concurrent sample pool",
|
2021-08-26 16:15:36 +08:00
|
|
|
ChildrenEnumerable = Enumerable.Range(0, max_concurrent_hitsounds).Select(_ => new PausableSkinnableSound())
|
2021-08-24 17:23:02 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Play the most appropriate hit sound for the current point in time.
|
|
|
|
/// </summary>
|
2021-08-25 14:24:01 +08:00
|
|
|
public virtual void Play()
|
|
|
|
{
|
2023-06-20 20:03:55 +08:00
|
|
|
HitObject? nextObject = GetMostValidObject();
|
2021-08-25 14:24:01 +08:00
|
|
|
|
|
|
|
if (nextObject == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var samples = nextObject.Samples
|
|
|
|
.Cast<ISampleInfo>()
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
PlaySamples(samples);
|
|
|
|
}
|
|
|
|
|
2023-05-21 22:47:16 +08:00
|
|
|
protected virtual void PlaySamples(ISampleInfo[] samples) => Schedule(() =>
|
2021-08-25 14:24:01 +08:00
|
|
|
{
|
|
|
|
var hitSound = getNextSample();
|
|
|
|
hitSound.Samples = samples;
|
|
|
|
hitSound.Play();
|
2022-06-16 15:04:52 +08:00
|
|
|
});
|
2021-08-25 14:24:01 +08:00
|
|
|
|
2023-06-20 20:03:55 +08:00
|
|
|
protected HitObject? GetMostValidObject()
|
2021-08-24 17:23:02 +08:00
|
|
|
{
|
2023-06-20 19:04:02 +08:00
|
|
|
if (mostValidObject == null || isAlreadyHit(mostValidObject))
|
2021-08-24 18:05:59 +08:00
|
|
|
{
|
2023-02-16 16:34:38 +08:00
|
|
|
// We need to use lifetime entries to find the next object (we can't just use `hitObjectContainer.Objects` due to pooling - it may even be empty).
|
|
|
|
// If required, we can make this lookup more efficient by adding support to get next-future-entry in LifetimeEntryManager.
|
2023-06-20 19:30:07 +08:00
|
|
|
var candidate =
|
|
|
|
// Use alive entries first as an optimisation.
|
|
|
|
hitObjectContainer.AliveEntries.Select(tuple => tuple.Entry).Where(e => !isAlreadyHit(e)).MinBy(e => e.HitObject.StartTime)
|
|
|
|
?? hitObjectContainer.Entries.Where(e => !isAlreadyHit(e)).MinBy(e => e.HitObject.StartTime);
|
2023-02-16 16:34:38 +08:00
|
|
|
|
2023-02-17 17:59:31 +08:00
|
|
|
// In the case there are no non-judged objects, the last hit object should be used instead.
|
2023-06-20 19:04:02 +08:00
|
|
|
if (candidate == null)
|
2023-06-20 19:30:07 +08:00
|
|
|
{
|
2023-06-20 19:04:02 +08:00
|
|
|
mostValidObject = hitObjectContainer.Entries.LastOrDefault();
|
2023-06-20 19:30:07 +08:00
|
|
|
}
|
2023-06-20 19:04:02 +08:00
|
|
|
else
|
|
|
|
{
|
2023-06-21 16:33:42 +08:00
|
|
|
if (isCloseEnoughToCurrentTime(candidate.HitObject))
|
2023-06-20 19:30:07 +08:00
|
|
|
{
|
2023-06-20 19:04:02 +08:00
|
|
|
mostValidObject = candidate;
|
2023-06-20 19:30:07 +08:00
|
|
|
}
|
2023-06-20 19:04:02 +08:00
|
|
|
else
|
2023-06-20 19:30:07 +08:00
|
|
|
{
|
2023-06-20 19:04:02 +08:00
|
|
|
mostValidObject ??= hitObjectContainer.Entries.FirstOrDefault();
|
2023-06-20 19:30:07 +08:00
|
|
|
}
|
2023-06-20 19:04:02 +08:00
|
|
|
}
|
2021-08-24 18:05:59 +08:00
|
|
|
}
|
2021-08-24 17:23:02 +08:00
|
|
|
|
2023-06-20 19:04:02 +08:00
|
|
|
if (mostValidObject == null)
|
2023-02-16 16:34:38 +08:00
|
|
|
return null;
|
|
|
|
|
|
|
|
// If the fallback has been judged then we want the sample from the object itself.
|
2023-06-20 19:04:02 +08:00
|
|
|
if (isAlreadyHit(mostValidObject))
|
|
|
|
return mostValidObject.HitObject;
|
2023-02-16 16:34:38 +08:00
|
|
|
|
2023-06-20 19:45:02 +08:00
|
|
|
// Else we want the earliest valid nested.
|
2023-02-16 16:34:38 +08:00
|
|
|
// In cases of nested objects, they will always have earlier sample data than their parent object.
|
2023-06-23 12:48:13 +08:00
|
|
|
return getAllNested(mostValidObject.HitObject).OrderBy(h => h.GetEndTime()).SkipWhile(h => h.GetEndTime() <= getReferenceTime()).FirstOrDefault() ?? mostValidObject.HitObject;
|
2023-02-16 16:34:38 +08:00
|
|
|
}
|
|
|
|
|
2023-06-20 19:04:02 +08:00
|
|
|
private bool isAlreadyHit(HitObjectLifetimeEntry h) => h.Result?.HasResult == true;
|
2023-06-21 16:33:42 +08:00
|
|
|
private bool isCloseEnoughToCurrentTime(HitObject h) => getReferenceTime() >= h.StartTime - h.HitWindows.WindowFor(HitResult.Miss) * 2;
|
|
|
|
|
2023-06-23 02:02:10 +08:00
|
|
|
private double getReferenceTime() => gameplayClock?.CurrentTime ?? Clock.CurrentTime;
|
2023-02-16 16:34:38 +08:00
|
|
|
|
2023-06-20 19:45:02 +08:00
|
|
|
private IEnumerable<HitObject> getAllNested(HitObject hitObject)
|
2023-02-16 16:34:38 +08:00
|
|
|
{
|
2023-06-20 19:45:02 +08:00
|
|
|
foreach (var h in hitObject.NestedHitObjects)
|
|
|
|
{
|
|
|
|
yield return h;
|
2023-02-16 16:34:38 +08:00
|
|
|
|
2023-06-20 19:45:02 +08:00
|
|
|
foreach (var n in getAllNested(h))
|
|
|
|
yield return n;
|
|
|
|
}
|
2021-08-24 17:23:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private SkinnableSound getNextSample()
|
|
|
|
{
|
2021-08-25 14:29:47 +08:00
|
|
|
SkinnableSound hitSound = hitSounds[nextHitSoundIndex];
|
2021-08-24 17:23:02 +08:00
|
|
|
|
|
|
|
// round robin over available samples to allow for concurrent playback.
|
|
|
|
nextHitSoundIndex = (nextHitSoundIndex + 1) % max_concurrent_hitsounds;
|
|
|
|
|
|
|
|
return hitSound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|