From b582da90e5e9b7f3b80cf5d3c86a0e666bb55a6e Mon Sep 17 00:00:00 2001 From: PopSlime Date: Tue, 13 Dec 2022 10:50:34 +0800 Subject: [PATCH] Add optional offset to TimingModel constructor. --- Assets/Cryville/Crtr/Extensions/TimingModel.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Assets/Cryville/Crtr/Extensions/TimingModel.cs b/Assets/Cryville/Crtr/Extensions/TimingModel.cs index acc4a8c..de7332a 100644 --- a/Assets/Cryville/Crtr/Extensions/TimingModel.cs +++ b/Assets/Cryville/Crtr/Extensions/TimingModel.cs @@ -1,4 +1,5 @@ using Cryville.Common.Math; +using System; namespace Cryville.Crtr.Extensions { public abstract class TimingModel { @@ -6,13 +7,16 @@ namespace Cryville.Crtr.Extensions { public double BeatTime { get; protected set; } public BeatTime FractionalBeatTime { get; protected set; } public double BPM { get; set; } - public TimingModel() { + public TimingModel(double offset) { + Time = offset; FractionalBeatTime = new BeatTime(0, 0, 1); } } public class FractionalBeatTimeTimingModel : TimingModel { + public FractionalBeatTimeTimingModel(double offset = 0) : base(offset) { } public void ForwardTo(BeatTime t) { if (t == FractionalBeatTime) return; + if (BPM == 0) throw new InvalidOperationException("BPM not determined"); FractionalBeatTime = t; var nt = t.Decimal; Time += (nt - BeatTime) / BPM * 60; @@ -20,13 +24,15 @@ namespace Cryville.Crtr.Extensions { } } public class BeatTimeTimingModel : TimingModel { + public BeatTimeTimingModel(double offset = 0) : base(offset) { } public void ForwardTo(double t) { if (t == BeatTime) return; + if (BPM == 0) throw new InvalidOperationException("BPM not determined"); Time += (t - BeatTime) / BPM * 60; BeatTime = t; FractionalBeatTime = ToBeatTime(t); } - BeatTime ToBeatTime(double beat, double error = 1e-4) { + static BeatTime ToBeatTime(double beat, double error = 1e-4) { int i, n, d; FractionUtils.ToFraction(beat, error, out n, out d); i = n / d; n %= d;