1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 14:52:55 +08:00
osu-lazer/osu.Game.Rulesets.Osu/UI/OrderedHitPolicy.cs

128 lines
5.4 KiB
C#
Raw Normal View History

2020-04-10 01:02:09 +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.
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.Osu.Objects;
using osu.Game.Rulesets.Osu.Objects.Drawables;
using osu.Game.Rulesets.UI;
namespace osu.Game.Rulesets.Osu.UI
{
/// <summary>
/// Ensures that <see cref="HitObject"/>s are hit in-order.
/// If a <see cref="HitObject"/> is hit out of order:
/// <list type="number">
/// <item><description>The hit is blocked if it occurred earlier than the previous <see cref="HitObject"/>'s start time.</description></item>
/// <item><description>The hit causes all previous <see cref="HitObject"/>s to missed otherwise.</description></item>
/// </list>
/// </summary>
public class OrderedHitPolicy
{
private readonly HitObjectContainer hitObjectContainer;
public OrderedHitPolicy(HitObjectContainer hitObjectContainer)
{
this.hitObjectContainer = hitObjectContainer;
}
/// <summary>
/// Determines whether a <see cref="DrawableHitObject"/> can be hit at a point in time.
/// </summary>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> to check.</param>
/// <param name="time">The time to check.</param>
/// <returns>Whether <paramref name="hitObject"/> can be hit at the given <paramref name="time"/>.</returns>
public bool IsHittable(DrawableHitObject hitObject, double time)
{
2020-04-10 01:41:37 +08:00
DrawableHitObject blockingObject = null;
2020-04-10 01:02:09 +08:00
2020-04-10 01:41:37 +08:00
// Find the last hitobject which blocks future hits.
foreach (var obj in hitObjectContainer.AliveObjects)
2020-04-10 01:02:09 +08:00
{
2020-04-10 01:41:37 +08:00
if (obj == hitObject)
2020-04-10 01:02:09 +08:00
break;
2020-04-10 01:41:37 +08:00
if (drawableCanBlockFutureHits(obj))
2020-04-10 01:41:37 +08:00
blockingObject = obj;
2020-04-10 01:02:09 +08:00
}
2020-04-10 01:41:37 +08:00
// If there is no previous hitobject, allow the hit.
if (blockingObject == null)
2020-04-10 01:02:09 +08:00
return true;
2020-04-10 01:41:37 +08:00
// A hit is allowed if:
// 1. The last blocking hitobject has been judged.
// 2. The current time is after the last hitobject's start time.
// Hits at exactly the same time as the blocking hitobject are allowed for maps that contain simultaneous hitobjects (e.g. /b/372245).
if (blockingObject.Judged || time >= blockingObject.HitObject.StartTime)
2020-04-10 01:02:09 +08:00
return true;
return false;
}
/// <summary>
/// Handles a <see cref="HitObject"/> being hit to potentially miss all earlier <see cref="HitObject"/>s.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> that was hit.</param>
public void HandleHit(HitObject hitObject)
{
2020-04-10 02:12:13 +08:00
// Hitobjects which themselves don't block future hitobjects don't cause misses (e.g. slider ticks, spinners).
if (!hitObjectCanBlockFutureHits(hitObject))
2020-04-10 01:02:09 +08:00
return;
2020-04-10 02:12:13 +08:00
double maximumTime = hitObject.StartTime;
2020-04-10 01:02:09 +08:00
2020-04-10 02:12:13 +08:00
// Iterate through and apply miss results to all top-level and nested hitobjects which block future hits.
2020-04-10 01:02:09 +08:00
foreach (var obj in hitObjectContainer.AliveObjects)
{
2020-04-10 02:12:13 +08:00
if (obj.Judged || obj.HitObject.StartTime >= maximumTime)
2020-04-10 01:41:37 +08:00
continue;
if (hitObjectCanBlockFutureHits(obj.HitObject))
applyMiss(obj);
2020-04-10 01:41:37 +08:00
foreach (var nested in obj.NestedHitObjects)
2020-04-10 01:02:09 +08:00
{
2020-04-10 02:12:13 +08:00
if (nested.Judged || nested.HitObject.StartTime >= maximumTime)
continue;
2020-04-10 01:02:09 +08:00
if (hitObjectCanBlockFutureHits(nested.HitObject))
2020-04-10 01:41:37 +08:00
applyMiss(nested);
2020-04-10 01:02:09 +08:00
}
}
2020-04-10 01:41:37 +08:00
static void applyMiss(DrawableHitObject obj) => ((DrawableOsuHitObject)obj).MissForcefully();
}
2020-04-10 01:02:09 +08:00
2020-04-10 01:41:37 +08:00
/// <summary>
/// Whether a <see cref="DrawableHitObject"/> blocks hits on future <see cref="DrawableHitObject"/>s until its start time is reached.
/// </summary>
/// <remarks>
/// This will ONLY match on top-most <see cref="DrawableHitObject"/>s.
2020-04-10 01:41:37 +08:00
/// </remarks>
/// <param name="hitObject">The <see cref="DrawableHitObject"/> to test.</param>
private static bool drawableCanBlockFutureHits(DrawableHitObject hitObject)
2020-04-10 01:41:37 +08:00
{
// Special considerations for slider tails aren't required since only top-most drawable hitobjects are being iterated over.
return hitObject is DrawableHitCircle || hitObject is DrawableSlider;
2020-04-10 01:02:09 +08:00
}
/// <summary>
/// Whether a <see cref="HitObject"/> blocks hits on future <see cref="HitObject"/>s until its start time is reached.
/// </summary>
2020-04-10 01:41:37 +08:00
/// <remarks>
/// This is more rigorous and may not match on top-most <see cref="HitObject"/>s as <see cref="drawableCanBlockFutureHits"/> does.
2020-04-10 01:41:37 +08:00
/// </remarks>
2020-04-10 01:02:09 +08:00
/// <param name="hitObject">The <see cref="HitObject"/> to test.</param>
private static bool hitObjectCanBlockFutureHits(HitObject hitObject)
2020-04-10 01:41:37 +08:00
{
// Unlike the above we will receive slider tails, but they do not block future hits.
if (hitObject is SliderTailCircle)
return false;
// All other hitcircles continue to block future hits.
return hitObject is HitCircle;
}
2020-04-10 01:02:09 +08:00
}
}