mirror of
https://github.com/ppy/osu.git
synced 2024-12-15 14:02:55 +08:00
Merge pull request #8701 from iiSaLMaN/add-interval-list
Implement period-tracking component
This commit is contained in:
commit
59dd523d27
85
osu.Game.Tests/NonVisual/PeriodTrackerTest.cs
Normal file
85
osu.Game.Tests/NonVisual/PeriodTrackerTest.cs
Normal file
@ -0,0 +1,85 @@
|
||||
// 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 System;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using osu.Framework.Utils;
|
||||
using osu.Game.Utils;
|
||||
|
||||
namespace osu.Game.Tests.NonVisual
|
||||
{
|
||||
[TestFixture]
|
||||
public class PeriodTrackerTest
|
||||
{
|
||||
private static readonly Period[] single_period = { new Period(1.0, 2.0) };
|
||||
|
||||
private static readonly Period[] unordered_periods =
|
||||
{
|
||||
new Period(-9.1, -8.3),
|
||||
new Period(-3.4, 2.1),
|
||||
new Period(9.0, 50.0),
|
||||
new Period(5.25, 10.50)
|
||||
};
|
||||
|
||||
[Test]
|
||||
public void TestCheckValueInsideSinglePeriod()
|
||||
{
|
||||
var tracker = new PeriodTracker(single_period);
|
||||
|
||||
var period = single_period.Single();
|
||||
Assert.IsTrue(tracker.IsInAny(period.Start));
|
||||
Assert.IsTrue(tracker.IsInAny(getMidpoint(period)));
|
||||
Assert.IsTrue(tracker.IsInAny(period.End));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCheckValuesInsidePeriods()
|
||||
{
|
||||
var tracker = new PeriodTracker(unordered_periods);
|
||||
|
||||
foreach (var period in unordered_periods)
|
||||
Assert.IsTrue(tracker.IsInAny(getMidpoint(period)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCheckValuesInRandomOrder()
|
||||
{
|
||||
var tracker = new PeriodTracker(unordered_periods);
|
||||
|
||||
foreach (var period in unordered_periods.OrderBy(_ => RNG.Next()))
|
||||
Assert.IsTrue(tracker.IsInAny(getMidpoint(period)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestCheckValuesOutOfPeriods()
|
||||
{
|
||||
var tracker = new PeriodTracker(new[]
|
||||
{
|
||||
new Period(1.0, 2.0),
|
||||
new Period(3.0, 4.0)
|
||||
});
|
||||
|
||||
Assert.IsFalse(tracker.IsInAny(0.9), "Time before first period is being considered inside");
|
||||
|
||||
Assert.IsFalse(tracker.IsInAny(2.1), "Time right after first period is being considered inside");
|
||||
Assert.IsFalse(tracker.IsInAny(2.9), "Time right before second period is being considered inside");
|
||||
|
||||
Assert.IsFalse(tracker.IsInAny(4.1), "Time after last period is being considered inside");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestReversedPeriodHandling()
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
{
|
||||
_ = new PeriodTracker(new[]
|
||||
{
|
||||
new Period(2.0, 1.0)
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private double getMidpoint(Period period) => period.Start + (period.End - period.Start) / 2;
|
||||
}
|
||||
}
|
@ -52,7 +52,7 @@ namespace osu.Game.Tests.Visual.Gameplay
|
||||
AddStep($"seek to break {breakIndex}", () => Player.GameplayClockContainer.Seek(destBreak().StartTime));
|
||||
AddUntilStep("wait for seek to complete", () => Player.HUDOverlay.Progress.ReferenceClock.CurrentTime >= destBreak().StartTime);
|
||||
|
||||
BreakPeriod destBreak() => Player.ChildrenOfType<BreakTracker>().First().Breaks.ElementAt(breakIndex);
|
||||
BreakPeriod destBreak() => Beatmap.Value.Beatmap.Breaks.ElementAt(breakIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -97,8 +97,6 @@ namespace osu.Game.Tests.Visual.Gameplay
|
||||
loadBreaksStep("multiple breaks", testBreaks);
|
||||
|
||||
seekAndAssertBreak("seek to break start", testBreaks[1].StartTime, true);
|
||||
AddAssert("is skipped to break #2", () => breakTracker.CurrentBreakIndex == 1);
|
||||
|
||||
seekAndAssertBreak("seek to break middle", testBreaks[1].StartTime + testBreaks[1].Duration / 2, true);
|
||||
seekAndAssertBreak("seek to break end", testBreaks[1].EndTime, false);
|
||||
seekAndAssertBreak("seek to break after end", testBreaks[1].EndTime + 500, false);
|
||||
@ -174,8 +172,6 @@ namespace osu.Game.Tests.Visual.Gameplay
|
||||
private readonly ManualClock manualClock;
|
||||
private IFrameBasedClock originalClock;
|
||||
|
||||
public new int CurrentBreakIndex => base.CurrentBreakIndex;
|
||||
|
||||
public double ManualClockTime
|
||||
{
|
||||
get => manualClock.CurrentTime;
|
||||
|
@ -2,40 +2,37 @@
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Game.Beatmaps.Timing;
|
||||
using osu.Game.Rulesets.Scoring;
|
||||
using osu.Game.Utils;
|
||||
|
||||
namespace osu.Game.Screens.Play
|
||||
{
|
||||
public class BreakTracker : Component
|
||||
{
|
||||
private readonly ScoreProcessor scoreProcessor;
|
||||
|
||||
private readonly double gameplayStartTime;
|
||||
|
||||
private PeriodTracker breaks;
|
||||
|
||||
/// <summary>
|
||||
/// Whether the gameplay is currently in a break.
|
||||
/// </summary>
|
||||
public IBindable<bool> IsBreakTime => isBreakTime;
|
||||
|
||||
protected int CurrentBreakIndex;
|
||||
|
||||
private readonly BindableBool isBreakTime = new BindableBool();
|
||||
|
||||
private IReadOnlyList<BreakPeriod> breaks;
|
||||
|
||||
public IReadOnlyList<BreakPeriod> Breaks
|
||||
{
|
||||
get => breaks;
|
||||
set
|
||||
{
|
||||
breaks = value;
|
||||
|
||||
// reset index in case the new breaks list is smaller than last one
|
||||
isBreakTime.Value = false;
|
||||
CurrentBreakIndex = 0;
|
||||
|
||||
breaks = new PeriodTracker(value.Where(b => b.HasEffect)
|
||||
.Select(b => new Period(b.StartTime, b.EndTime - BreakOverlay.BREAK_FADE_DURATION)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,34 +46,11 @@ namespace osu.Game.Screens.Play
|
||||
{
|
||||
base.Update();
|
||||
|
||||
isBreakTime.Value = getCurrentBreak()?.HasEffect == true
|
||||
|| Clock.CurrentTime < gameplayStartTime
|
||||
var time = Clock.CurrentTime;
|
||||
|
||||
isBreakTime.Value = breaks?.IsInAny(time) == true
|
||||
|| time < gameplayStartTime
|
||||
|| scoreProcessor?.HasCompleted.Value == true;
|
||||
}
|
||||
|
||||
private BreakPeriod getCurrentBreak()
|
||||
{
|
||||
if (breaks?.Count > 0)
|
||||
{
|
||||
var time = Clock.CurrentTime;
|
||||
|
||||
if (time > breaks[CurrentBreakIndex].EndTime)
|
||||
{
|
||||
while (time > breaks[CurrentBreakIndex].EndTime && CurrentBreakIndex < breaks.Count - 1)
|
||||
CurrentBreakIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (time < breaks[CurrentBreakIndex].StartTime && CurrentBreakIndex > 0)
|
||||
CurrentBreakIndex--;
|
||||
}
|
||||
|
||||
var closest = breaks[CurrentBreakIndex];
|
||||
|
||||
return closest.Contains(time) ? closest : null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
69
osu.Game/Utils/PeriodTracker.cs
Normal file
69
osu.Game/Utils/PeriodTracker.cs
Normal file
@ -0,0 +1,69 @@
|
||||
// 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 System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace osu.Game.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a tracking component used for whether a specific time instant falls into any of the provided periods.
|
||||
/// </summary>
|
||||
public class PeriodTracker
|
||||
{
|
||||
private readonly List<Period> periods;
|
||||
private int nearestIndex;
|
||||
|
||||
public PeriodTracker(IEnumerable<Period> periods)
|
||||
{
|
||||
this.periods = periods.OrderBy(period => period.Start).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether the provided time is in any of the added periods.
|
||||
/// </summary>
|
||||
/// <param name="time">The time value to check.</param>
|
||||
public bool IsInAny(double time)
|
||||
{
|
||||
if (periods.Count == 0)
|
||||
return false;
|
||||
|
||||
if (time > periods[nearestIndex].End)
|
||||
{
|
||||
while (time > periods[nearestIndex].End && nearestIndex < periods.Count - 1)
|
||||
nearestIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (time < periods[nearestIndex].Start && nearestIndex > 0)
|
||||
nearestIndex--;
|
||||
}
|
||||
|
||||
var nearest = periods[nearestIndex];
|
||||
return time >= nearest.Start && time <= nearest.End;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct Period
|
||||
{
|
||||
/// <summary>
|
||||
/// The start time of this period.
|
||||
/// </summary>
|
||||
public readonly double Start;
|
||||
|
||||
/// <summary>
|
||||
/// The end time of this period.
|
||||
/// </summary>
|
||||
public readonly double End;
|
||||
|
||||
public Period(double start, double end)
|
||||
{
|
||||
if (start >= end)
|
||||
throw new ArgumentException($"Invalid period provided, {nameof(start)} must be less than {nameof(end)}");
|
||||
|
||||
Start = start;
|
||||
End = end;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user