Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class FixedRateUtils
- {
- public static void EnableFixedRateWithCatchUpCorrected(ComponentSystemGroup group, float timeStep)
- {
- var manager = new FixedRateCatchUpManagerCorrected(timeStep);
- group.UpdateCallback = manager.UpdateCallback;
- }
- public static void DisableFixedRate(ComponentSystemGroup group)
- {
- group.UpdateCallback = null;
- }
- internal class FixedRateCatchUpManagerCorrected
- {
- protected float m_FixedTimeStep;
- protected double m_LastFixedUpdateTime;
- protected int m_FixedUpdateCount;
- protected bool m_DidPushTime;
- internal FixedRateCatchUpManagerCorrected(float fixedStep)
- {
- m_FixedTimeStep = fixedStep;
- }
- internal bool UpdateCallback(ComponentSystemGroup group)
- {
- m_LastFixedUpdateTime = group.World.Time.ElapsedTime;
- m_FixedUpdateCount = 1;
- group.World.PushTime(new TimeData(
- elapsedTime: m_LastFixedUpdateTime,
- deltaTime: m_FixedTimeStep));
- group.UpdateCallback = this.InternalUpdateCallback;
- m_DidPushTime = true;
- return true;
- }
- internal bool InternalUpdateCallback(ComponentSystemGroup group)
- {
- // if this is true, means we're being called a second or later time in a loop
- if (m_DidPushTime)
- {
- group.World.PopTime();
- }
- var elapsedTime = group.World.Time.ElapsedTime;
- if (elapsedTime - m_LastFixedUpdateTime >= m_FixedTimeStep)
- {
- // Note that m_FixedTimeStep of 0.0f will never update
- m_LastFixedUpdateTime += m_FixedTimeStep;
- m_FixedUpdateCount++;
- }
- else
- {
- m_DidPushTime = false;
- return false;
- }
- group.World.PushTime(new TimeData(
- elapsedTime: m_LastFixedUpdateTime,
- deltaTime: m_FixedTimeStep));
- m_DidPushTime = true;
- return true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement