Timing helpers

This commit is contained in:
Matt Thorson 2020-05-04 21:11:12 -07:00
parent 35c29f3f74
commit 654398a137
2 changed files with 43 additions and 1 deletions

View File

@ -5,6 +5,8 @@ namespace Strawberry
{ {
public class Scene public class Scene
{ {
public float TimeStarted { get; private set; }
private List<Entity> entities; private List<Entity> entities;
private HashSet<Entity> toRemove; private HashSet<Entity> toRemove;
private HashSet<Entity> toAdd; private HashSet<Entity> toAdd;
@ -33,7 +35,7 @@ namespace Strawberry
public virtual void Started() public virtual void Started()
{ {
TimeStarted = Time.Elapsed;
} }
public virtual void Update() public virtual void Update()
@ -95,6 +97,36 @@ namespace Strawberry
} }
} }
// Time
public float TimeElapsed
{
[Inline]
get
{
return Time.Elapsed - TimeStarted;
}
}
public float PreviousTimeElapsed
{
[Inline]
get
{
return Time.PreviousElapsed - TimeStarted;
}
}
public bool TimeOnInterval(float interval, float offset = 0)
{
return (int)((TimeElapsed - offset) / interval) != (int)((PreviousTimeElapsed - offset) / interval);
}
public bool TimeBetweenInterval(float interval, float offset = 0)
{
return (TimeElapsed - offset) % (interval * 2) >= interval;
}
// Finding Entities // Finding Entities
public T First<T>() where T : Entity public T First<T>() where T : Entity

View File

@ -5,5 +5,15 @@ namespace Strawberry
static public float Elapsed; static public float Elapsed;
static public float PreviousElapsed; static public float PreviousElapsed;
static public float Delta = 1 / 60f; static public float Delta = 1 / 60f;
static public bool OnInterval(float interval, float offset = 0)
{
return (int)((Elapsed - offset) / interval) != (int)((PreviousElapsed - offset) / interval);
}
static public bool BetweenInterval(float interval, float offset = 0)
{
return (Elapsed - offset) % (interval * 2) >= interval;
}
} }
} }