From c60b15dc517ed685476059ecb5ef1a73fbb4bce5 Mon Sep 17 00:00:00 2001 From: LubieKakao1212 Date: Fri, 16 Dec 2022 21:30:23 +0100 Subject: [PATCH 1/4] time machines --- Timing.meta | 8 +++++ Timing/AutoTimeMachine.cs | 33 ++++++++++++++++++ Timing/AutoTimeMachine.cs.meta | 11 ++++++ Timing/TimeMachine.cs | 64 ++++++++++++++++++++++++++++++++++ Timing/TimeMachine.cs.meta | 11 ++++++ 5 files changed, 127 insertions(+) create mode 100644 Timing.meta create mode 100644 Timing/AutoTimeMachine.cs create mode 100644 Timing/AutoTimeMachine.cs.meta create mode 100644 Timing/TimeMachine.cs create mode 100644 Timing/TimeMachine.cs.meta 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: From 927d835d2e367007ab3590af547892758ac6a193 Mon Sep 17 00:00:00 2001 From: LubieKakao1212 Date: Mon, 19 Dec 2022 15:23:50 +0100 Subject: [PATCH 2/4] Improved naming and documentation --- Timing/TimeMachine.cs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Timing/TimeMachine.cs b/Timing/TimeMachine.cs index e253bdc..9cef9b7 100644 --- a/Timing/TimeMachine.cs +++ b/Timing/TimeMachine.cs @@ -12,32 +12,38 @@ namespace NEG.Utils.Timing } /// - /// Adds time int this TimeMachine + /// Adds time into the TimeMachine /// /// Amount of time to be added - public void Forward(double time) + public void Accumulate(double time) { this.time += time; } /// - /// Decreeses the amount of time in this TimeMashine by up to given amount + /// Retrieves given amount of time from the TimeMachine /// /// - /// Amount of time decressed - public double Warp(double maxTime) + /// 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 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 + /// 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 TryWarp(double time) + public bool TryRetrieve(double time) { if (this.time >= time) { @@ -48,12 +54,12 @@ namespace NEG.Utils.Timing } /// - /// Result is equivalent to calling as many times as possible, but is faster fo larger values + /// Result is equivalent to calling as many times as possible, but is faster for larger values /// - /// Single unit of warp time, must be non negative/param> - /// Maximum amount of warps, must be non negative + /// Single unit of warp time, must be positive/param> + /// Maximum amount of warps, must be positive /// Amount of warps - public int MultiWarp(double interval, int limit = int.MaxValue) + public int RetrieveAll(double interval, int limit = int.MaxValue) { int result = (int)Math.Floor(time / interval); result = Math.Clamp(result, 0, limit); From df046e33039602181384f7f31be7d5c54b5a3806 Mon Sep 17 00:00:00 2001 From: LubieKakao1212 Date: Mon, 19 Dec 2022 19:35:14 +0100 Subject: [PATCH 3/4] Fixed AutoTimeMachine --- Timing/AutoTimeMachine.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Timing/AutoTimeMachine.cs b/Timing/AutoTimeMachine.cs index 6380ecc..6bcf59f 100644 --- a/Timing/AutoTimeMachine.cs +++ b/Timing/AutoTimeMachine.cs @@ -22,8 +22,8 @@ namespace NEG.Utils.Timing /// Amount of time to forward by public void Forward(double time) { - machine.Forward(time); - int rolls = machine.MultiWarp(Interval); + machine.Accumulate(time); + int rolls = machine.RetrieveAll(Interval); for (int i = 0; i < rolls; i++) { Action(); From 57bc81065e5ce6f43b47fef1655ca24e3ab65058 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Osi=C5=84ski?= Date: Tue, 3 Jan 2023 12:09:37 +0000 Subject: [PATCH 4/4] Apply 1 suggestion(s) to 1 file(s) --- Timing/TimeMachine.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Timing/TimeMachine.cs b/Timing/TimeMachine.cs index 9cef9b7..02cc9bf 100644 --- a/Timing/TimeMachine.cs +++ b/Timing/TimeMachine.cs @@ -61,7 +61,7 @@ namespace NEG.Utils.Timing /// Amount of warps public int RetrieveAll(double interval, int limit = int.MaxValue) { - int result = (int)Math.Floor(time / interval); + int result = Mathf.FloorToInt(time / interval); result = Math.Clamp(result, 0, limit); time -= result * interval; return result;