using Unity.Entities;
using Unity.Sample.Core;
using UnityEngine;
public struct GameTime
{
/// Number of ticks per second.
public int tickRate
{
get { return m_tickRate; }
set
{
m_tickRate = value;
tickInterval = 1.0f / m_tickRate;
}
}
/// Length of each world tick at current tickrate, e.g. 0.0166s if ticking at 60fps.
public float tickInterval { get; private set; } // Time between ticks
public int tick; // Current tick
public float tickDuration; // Duration of current tick
public GameTime(int tickRate)
{
this.m_tickRate = tickRate;
this.tickInterval = 1.0f / m_tickRate;
this.tick = 1;
this.tickDuration = 0;
}
public float TickDurationAsFraction
{
get { return tickDuration / tickInterval; }
}
public void SetTime(int tick, float tickDuration)
{
this.tick = tick;
this.tickDuration = tickDuration;
}
public float DurationSinceTick(int tick)
{
return (this.tick - tick) * tickInterval + tickDuration;
}
public void AddDuration(float duration)
{
tickDuration += duration;
int deltaTicks = Mathf.FloorToInt(tickDuration * (float)tickRate);
tick += deltaTicks;
tickDuration = tickDuration % tickInterval;
}
public static float GetDuration(GameTime start, GameTime end)
{
if (start.tickRate != end.tickRate)
{
GameDebug.LogError("Trying to compare time with different tick rates (" + start.tickRate + " and " + end.tickRate + ")");
return 0;
}
float result = (end.tick - start.tick) * start.tickInterval + end.tickDuration - start.tickDuration;
return result;
}
int m_tickRate;
}
public struct GlobalGameTime : IComponentData
{
public GameTime gameTime;
public float frameDuration;
}