time machines
This commit is contained in:
parent
1318e8fea4
commit
c60b15dc51
8
Timing.meta
Normal file
8
Timing.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d3f27225e5dd00546b927b1a8bd253ee
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
33
Timing/AutoTimeMachine.cs
Normal file
33
Timing/AutoTimeMachine.cs
Normal 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.Forward(time);
|
||||||
|
int rolls = machine.MultiWarp(Interval);
|
||||||
|
for (int i = 0; i < rolls; i++)
|
||||||
|
{
|
||||||
|
Action();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Timing/AutoTimeMachine.cs.meta
Normal file
11
Timing/AutoTimeMachine.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: cf2d68c5a76882f4ead172644f643fd5
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
64
Timing/TimeMachine.cs
Normal file
64
Timing/TimeMachine.cs
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace NEG.Utils.Timing
|
||||||
|
{
|
||||||
|
public class TimeMachine
|
||||||
|
{
|
||||||
|
private double time;
|
||||||
|
|
||||||
|
public TimeMachine()
|
||||||
|
{
|
||||||
|
time = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds time int this TimeMachine
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="time">Amount of time to be added</param>
|
||||||
|
public void Forward(double time)
|
||||||
|
{
|
||||||
|
this.time += time;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Decreeses the amount of time in this TimeMashine by up to given amount
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="maxTime"></param>
|
||||||
|
/// <returns>Amount of time decressed</returns>
|
||||||
|
public double Warp(double maxTime)
|
||||||
|
{
|
||||||
|
double timeLeft = time - maxTime;
|
||||||
|
time = Math.Max(timeLeft, 0);
|
||||||
|
return Math.Min(maxTime + timeLeft, maxTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attempts to decrese the amount of time in this TimeMachine <br/>
|
||||||
|
/// If there is at least <paramref name="time"/> time accumulated in this machine subtructs that amount and returns true, otherwise returns false
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="time"></param>
|
||||||
|
public bool TryWarp(double time)
|
||||||
|
{
|
||||||
|
if (this.time >= time)
|
||||||
|
{
|
||||||
|
this.time -= time;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Result is equivalent to calling <see cref="TryWarp(double)"/> as many times as possible, but is faster fo larger <paramref name="limit"/> values
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="interval">Single unit of warp time, must be non negative/param>
|
||||||
|
/// <param name="limit">Maximum amount of warps, must be non negative</param>
|
||||||
|
/// <returns>Amount of warps</returns>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Timing/TimeMachine.cs.meta
Normal file
11
Timing/TimeMachine.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 2f4db23cbb7389a41bf48808a3d5696f
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Loading…
x
Reference in New Issue
Block a user