1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 09:27:29 +08:00

Introduce StartFrame and EndFrame to simplify the replay interpolation code

This commit is contained in:
ekrctb 2021-04-16 12:53:58 +09:00
parent 84bc81a6de
commit 91c7d8d26c
5 changed files with 33 additions and 72 deletions

View File

@ -7,7 +7,6 @@ using osu.Framework.Input.StateChanges;
using osu.Framework.Utils;
using osu.Game.Replays;
using osu.Game.Rulesets.Replays;
using osuTK;
namespace osu.Game.Rulesets.EmptyFreeform.Replays
{
@ -20,24 +19,13 @@ namespace osu.Game.Rulesets.EmptyFreeform.Replays
protected override bool IsImportant(EmptyFreeformReplayFrame frame) => frame.Actions.Any();
protected Vector2 Position
{
get
{
var frame = CurrentFrame;
if (frame == null)
return Vector2.Zero;
return Interpolation.ValueAt(CurrentTime, frame.Position, NextFrame.Position, frame.Time, NextFrame.Time);
}
}
public override void CollectPendingInputs(List<IInput> inputs)
{
var position = Interpolation.ValueAt(CurrentTime, StartFrame.Position, EndFrame.Position, StartFrame.Time, EndFrame.Time);
inputs.Add(new MousePositionAbsoluteInput
{
Position = GamefieldToScreenSpace(Position),
Position = GamefieldToScreenSpace(position),
});
inputs.Add(new ReplayState<EmptyFreeformAction>
{

View File

@ -6,7 +6,6 @@ using osu.Framework.Input.StateChanges;
using osu.Framework.Utils;
using osu.Game.Replays;
using osu.Game.Rulesets.Replays;
using osuTK;
namespace osu.Game.Rulesets.Pippidon.Replays
{
@ -19,24 +18,13 @@ namespace osu.Game.Rulesets.Pippidon.Replays
protected override bool IsImportant(PippidonReplayFrame frame) => true;
protected Vector2 Position
{
get
{
var frame = CurrentFrame;
if (frame == null)
return Vector2.Zero;
return NextFrame != null ? Interpolation.ValueAt(CurrentTime, frame.Position, NextFrame.Position, frame.Time, NextFrame.Time) : frame.Position;
}
}
public override void CollectPendingInputs(List<IInput> inputs)
{
var position = Interpolation.ValueAt(CurrentTime, StartFrame.Position, EndFrame.Position, StartFrame.Time, EndFrame.Time);
inputs.Add(new MousePositionAbsoluteInput
{
Position = GamefieldToScreenSpace(Position)
Position = GamefieldToScreenSpace(position)
});
}
}

View File

@ -19,27 +19,14 @@ namespace osu.Game.Rulesets.Catch.Replays
protected override bool IsImportant(CatchReplayFrame frame) => frame.Actions.Any();
protected float? Position
{
get
{
var frame = CurrentFrame;
if (frame == null)
return null;
return NextFrame != null ? Interpolation.ValueAt(CurrentTime, frame.Position, NextFrame.Position, frame.Time, NextFrame.Time) : frame.Position;
}
}
public override void CollectPendingInputs(List<IInput> inputs)
{
if (!Position.HasValue) return;
var position = Interpolation.ValueAt(CurrentTime, StartFrame.Position, EndFrame.Position, StartFrame.Time, EndFrame.Time);
inputs.Add(new CatchReplayState
{
PressedActions = CurrentFrame?.Actions ?? new List<CatchAction>(),
CatcherX = Position.Value
CatcherX = position
});
}

View File

@ -7,7 +7,6 @@ using osu.Framework.Input.StateChanges;
using osu.Framework.Utils;
using osu.Game.Replays;
using osu.Game.Rulesets.Replays;
using osuTK;
namespace osu.Game.Rulesets.Osu.Replays
{
@ -20,22 +19,11 @@ namespace osu.Game.Rulesets.Osu.Replays
protected override bool IsImportant(OsuReplayFrame frame) => frame.Actions.Any();
protected Vector2? Position
{
get
{
var frame = CurrentFrame;
if (frame == null)
return null;
return NextFrame != null ? Interpolation.ValueAt(CurrentTime, frame.Position, NextFrame.Position, frame.Time, NextFrame.Time) : frame.Position;
}
}
public override void CollectPendingInputs(List<IInput> inputs)
{
inputs.Add(new MousePositionAbsoluteInput { Position = GamefieldToScreenSpace(Position ?? Vector2.Zero) });
var position = Interpolation.ValueAt(CurrentTime, StartFrame.Position, EndFrame.Position, StartFrame.Time, EndFrame.Time);
inputs.Add(new MousePositionAbsoluteInput { Position = GamefieldToScreenSpace(position) });
inputs.Add(new ReplayState<OsuAction> { PressedActions = CurrentFrame?.Actions ?? new List<OsuAction>() });
}
}

View File

@ -33,32 +33,42 @@ namespace osu.Game.Rulesets.Replays
/// The current time is always between the start and the end time of the current frame.
/// </summary>
/// <remarks>Returns null if the current time is strictly before the first frame.</remarks>
public TFrame? CurrentFrame => currentFrameIndex == -1 ? null : (TFrame)Frames[currentFrameIndex];
/// <summary>
/// The next frame of the replay.
/// The start time of <see cref="NextFrame"/> is always greater or equal to the start time of <see cref="CurrentFrame"/> regardless of the seeking direction.
/// </summary>
/// <remarks>Returns null if the current frame is the last frame.</remarks>
public TFrame? NextFrame => currentFrameIndex == Frames.Count - 1 ? null : (TFrame)Frames[currentFrameIndex + 1];
/// <summary>
/// The frame for the start value of the interpolation of the replay movement.
/// </summary>
/// <exception cref="InvalidOperationException">The replay is empty.</exception>
public TFrame? CurrentFrame
public TFrame StartFrame
{
get
{
if (!HasFrames)
throw new InvalidOperationException($"Attempted to get {nameof(CurrentFrame)} of an empty replay");
throw new InvalidOperationException($"Attempted to get {nameof(StartFrame)} of an empty replay");
return currentFrameIndex == -1 ? null : (TFrame)Frames[currentFrameIndex];
return (TFrame)Frames[Math.Max(0, currentFrameIndex)];
}
}
/// <summary>
/// The next frame of the replay.
/// The start time is always greater or equal to the start time of <see cref="CurrentFrame"/> regardless of the seeking direction.
/// The frame for the end value of the interpolation of the replay movement.
/// </summary>
/// <remarks>Returns null if the current frame is the last frame.</remarks>
/// <exception cref="InvalidOperationException">The replay is empty.</exception>
public TFrame? NextFrame
public TFrame EndFrame
{
get
{
if (!HasFrames)
throw new InvalidOperationException($"Attempted to get {nameof(NextFrame)} of an empty replay");
throw new InvalidOperationException($"Attempted to get {nameof(EndFrame)} of an empty replay");
return currentFrameIndex == Frames.Count - 1 ? null : (TFrame)Frames[currentFrameIndex + 1];
return (TFrame)Frames[Math.Min(currentFrameIndex + 1, Frames.Count - 1)];
}
}
@ -98,11 +108,11 @@ namespace osu.Game.Rulesets.Replays
{
get
{
if (!HasFrames || !FrameAccuratePlayback || CurrentFrame == null)
if (!HasFrames || !FrameAccuratePlayback || currentFrameIndex == -1)
return false;
return IsImportant(CurrentFrame) && // a button is in a pressed state
Math.Abs(CurrentTime - NextFrame?.Time ?? 0) <= AllowedImportantTimeSpan; // the next frame is within an allowable time span
return IsImportant(StartFrame) && // a button is in a pressed state
Math.Abs(CurrentTime - EndFrame.Time) <= AllowedImportantTimeSpan; // the next frame is within an allowable time span
}
}