diff --git a/Timing.meta b/Timing.meta new file mode 100644 index 0000000..aa993a9 --- /dev/null +++ b/Timing.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d3f27225e5dd00546b927b1a8bd253ee +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Timing/AutoTimeMachine.cs b/Timing/AutoTimeMachine.cs new file mode 100644 index 0000000..6380ecc --- /dev/null +++ b/Timing/AutoTimeMachine.cs @@ -0,0 +1,33 @@ +using System; + +namespace NEG.Utils.Timing +{ + public class AutoTimeMachine + { + public double Interval { get; set; } + public Action Action { get; set; } + + private readonly TimeMachine machine; + + public AutoTimeMachine(Action action, double interval) + { + Action = action; + Interval = interval; + machine = new TimeMachine(); + } + + /// + /// Forwards the time by given amount, triggers assigned action relevant amount of times + /// + /// Amount of time to forward by + public void Forward(double time) + { + machine.Forward(time); + int rolls = machine.MultiWarp(Interval); + for (int i = 0; i < rolls; i++) + { + Action(); + } + } + } +} diff --git a/Timing/AutoTimeMachine.cs.meta b/Timing/AutoTimeMachine.cs.meta new file mode 100644 index 0000000..3c13533 --- /dev/null +++ b/Timing/AutoTimeMachine.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cf2d68c5a76882f4ead172644f643fd5 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Timing/TimeMachine.cs b/Timing/TimeMachine.cs new file mode 100644 index 0000000..e253bdc --- /dev/null +++ b/Timing/TimeMachine.cs @@ -0,0 +1,64 @@ +using System; + +namespace NEG.Utils.Timing +{ + public class TimeMachine + { + private double time; + + public TimeMachine() + { + time = 0; + } + + /// + /// Adds time int this TimeMachine + /// + /// Amount of time to be added + public void Forward(double time) + { + this.time += time; + } + + /// + /// Decreeses the amount of time in this TimeMashine by up to given amount + /// + /// + /// Amount of time decressed + public double Warp(double maxTime) + { + double timeLeft = time - maxTime; + time = Math.Max(timeLeft, 0); + return Math.Min(maxTime + timeLeft, maxTime); + } + + /// + /// Attempts to decrese the amount of time in this TimeMachine
+ /// If there is at least time accumulated in this machine subtructs that amount and returns true, otherwise returns false + ///
+ /// + public bool TryWarp(double time) + { + if (this.time >= time) + { + this.time -= time; + return true; + } + return false; + } + + /// + /// Result is equivalent to calling as many times as possible, but is faster fo larger values + /// + /// Single unit of warp time, must be non negative/param> + /// Maximum amount of warps, must be non negative + /// Amount of warps + public int MultiWarp(double interval, int limit = int.MaxValue) + { + int result = (int)Math.Floor(time / interval); + result = Math.Clamp(result, 0, limit); + time -= result * interval; + return result; + } + } +} diff --git a/Timing/TimeMachine.cs.meta b/Timing/TimeMachine.cs.meta new file mode 100644 index 0000000..5b85c31 --- /dev/null +++ b/Timing/TimeMachine.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2f4db23cbb7389a41bf48808a3d5696f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: