Rename parameter to repeatCount and add guards

This commit is contained in:
Dean Herbert
2021-10-01 18:24:46 +09:00
parent f148fbcc94
commit 3faafd7200
3 changed files with 24 additions and 13 deletions

View File

@ -176,8 +176,8 @@ namespace osu.Game.Beatmaps.Formats
case "L": case "L":
{ {
var startTime = Parsing.ParseDouble(split[1]); var startTime = Parsing.ParseDouble(split[1]);
var loopCount = Parsing.ParseInt(split[2]); var repeatCount = Parsing.ParseInt(split[2]);
timelineGroup = storyboardSprite?.AddLoop(startTime, loopCount); timelineGroup = storyboardSprite?.AddLoop(startTime, Math.Max(0, repeatCount));
break; break;
} }

View File

@ -9,20 +9,31 @@ namespace osu.Game.Storyboards
public class CommandLoop : CommandTimelineGroup public class CommandLoop : CommandTimelineGroup
{ {
public double LoopStartTime; public double LoopStartTime;
public int LoopCount;
/// <summary>
/// The total number of times this loop is played back. Always greater than zero.
/// </summary>
public readonly int TotalIterations;
public override double StartTime => LoopStartTime + CommandsStartTime; public override double StartTime => LoopStartTime + CommandsStartTime;
public override double EndTime => StartTime + CommandsDuration * LoopCount; public override double EndTime => StartTime + CommandsDuration * TotalIterations;
public CommandLoop(double startTime, int loopCount) /// <summary>
/// Construct a new command loop.
/// </summary>
/// <param name="startTime">The start time of the loop.</param>
/// <param name="repeatCount">The number of times the loop should repeat. Should be greater than zero. Zero means a single playback.</param>
public CommandLoop(double startTime, int repeatCount)
{ {
if (repeatCount < 0) throw new ArgumentException("Repeat count must be zero or above.", nameof(repeatCount));
LoopStartTime = startTime; LoopStartTime = startTime;
LoopCount = Math.Max(1, loopCount); TotalIterations = repeatCount + 1;
} }
public override IEnumerable<CommandTimeline<T>.TypedCommand> GetCommands<T>(CommandTimelineSelector<T> timelineSelector, double offset = 0) public override IEnumerable<CommandTimeline<T>.TypedCommand> GetCommands<T>(CommandTimelineSelector<T> timelineSelector, double offset = 0)
{ {
for (var loop = 0; loop < LoopCount; loop++) for (var loop = 0; loop < TotalIterations; loop++)
{ {
var loopOffset = LoopStartTime + loop * CommandsDuration; var loopOffset = LoopStartTime + loop * CommandsDuration;
foreach (var command in base.GetCommands(timelineSelector, offset + loopOffset)) foreach (var command in base.GetCommands(timelineSelector, offset + loopOffset))
@ -31,6 +42,6 @@ namespace osu.Game.Storyboards
} }
public override string ToString() public override string ToString()
=> $"{LoopStartTime} x{LoopCount}"; => $"{LoopStartTime} x{TotalIterations}";
} }
} }

View File

@ -1,13 +1,13 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using osuTK;
using osu.Framework.Graphics;
using osu.Game.Storyboards.Drawables;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using JetBrains.Annotations; using JetBrains.Annotations;
using osu.Framework.Graphics;
using osu.Game.Storyboards.Drawables;
using osuTK;
namespace osu.Game.Storyboards namespace osu.Game.Storyboards
{ {
@ -78,9 +78,9 @@ namespace osu.Game.Storyboards
InitialPosition = initialPosition; InitialPosition = initialPosition;
} }
public CommandLoop AddLoop(double startTime, int loopCount) public CommandLoop AddLoop(double startTime, int repeatCount)
{ {
var loop = new CommandLoop(startTime, loopCount); var loop = new CommandLoop(startTime, repeatCount);
loops.Add(loop); loops.Add(loop);
return loop; return loop;
} }