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..6bcf59f --- /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.Accumulate(time); + int rolls = machine.RetrieveAll(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..02cc9bf --- /dev/null +++ b/Timing/TimeMachine.cs @@ -0,0 +1,70 @@ +using System; + +namespace NEG.Utils.Timing +{ + public class TimeMachine + { + private double time; + + public TimeMachine() + { + time = 0; + } + + /// + /// Adds time into the TimeMachine + /// + /// Amount of time to be added + public void Accumulate(double time) + { + this.time += time; + } + + /// + /// Retrieves given amount of time from the TimeMachine + /// + /// + /// Amount of time retrievend + public double Retrieve(double maxTime) + { + if(!Double.IsFinite(maxTime)) + { + double timeRetrieved = time; + time = 0; + return timeRetrieved; + } + double timeLeft = time - maxTime; + time = Math.Max(timeLeft, 0); + return Math.Min(maxTime + timeLeft, maxTime); + } + + /// + /// Attempts to retrieves given amount of time from the TimeMachine
+ /// If there is enough accumulated in this machine subtructs that amount and returns true, otherwise returns false + ///
+ /// + public bool TryRetrieve(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 for larger values + /// + /// Single unit of warp time, must be positive/param> + /// Maximum amount of warps, must be positive + /// Amount of warps + public int RetrieveAll(double interval, int limit = int.MaxValue) + { + int result = Mathf.FloorToInt(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: