Merge branch 'time-machines' into 'main'

time machines

See merge request negames/NegUtils!1
This commit is contained in:
Michał Osiński 2023-01-05 12:07:56 +00:00
commit 58ef8a0268
5 changed files with 133 additions and 0 deletions

8
Timing.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d3f27225e5dd00546b927b1a8bd253ee
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

33
Timing/AutoTimeMachine.cs Normal file
View File

@ -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();
}
/// <summary>
/// Forwards the time by given amount, triggers assigned action relevant amount of times
/// </summary>
/// <param name="time">Amount of time to forward by</param>
public void Forward(double time)
{
machine.Accumulate(time);
int rolls = machine.RetrieveAll(Interval);
for (int i = 0; i < rolls; i++)
{
Action();
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cf2d68c5a76882f4ead172644f643fd5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

70
Timing/TimeMachine.cs Normal file
View File

@ -0,0 +1,70 @@
using System;
namespace NEG.Utils.Timing
{
public class TimeMachine
{
private double time;
public TimeMachine()
{
time = 0;
}
/// <summary>
/// Adds time into the TimeMachine
/// </summary>
/// <param name="time">Amount of time to be added</param>
public void Accumulate(double time)
{
this.time += time;
}
/// <summary>
/// Retrieves given amount of time from the TimeMachine
/// </summary>
/// <param name="maxTime"></param>
/// <returns>Amount of time retrievend</returns>
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);
}
/// <summary>
/// Attempts to retrieves given amount of time from the TimeMachine <br/>
/// If there is enough <paramref name="time"/> accumulated in this machine subtructs that amount and returns true, otherwise returns false
/// </summary>
/// <param name="time"></param>
public bool TryRetrieve(double time)
{
if (this.time >= time)
{
this.time -= time;
return true;
}
return false;
}
/// <summary>
/// Result is equivalent to calling <see cref="TryRetrieve(double)"/> as many times as possible, but is faster for larger <paramref name="limit"/> values
/// </summary>
/// <param name="interval">Single unit of warp time, must be positive/param>
/// <param name="limit">Maximum amount of warps, must be positive</param>
/// <returns>Amount of warps</returns>
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;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2f4db23cbb7389a41bf48808a3d5696f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: