Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. public static class FixedRateUtils
  2. {
  3.    
  4.     public static void EnableFixedRateWithCatchUpCorrected(ComponentSystemGroup group, float timeStep)
  5.     {
  6.         var manager = new FixedRateCatchUpManagerCorrected(timeStep);
  7.         group.UpdateCallback = manager.UpdateCallback;
  8.     }
  9.  
  10.     public static void DisableFixedRate(ComponentSystemGroup group)
  11.     {
  12.         group.UpdateCallback = null;
  13.     }
  14.  
  15.     internal class FixedRateCatchUpManagerCorrected
  16.     {
  17.         protected float m_FixedTimeStep;
  18.         protected double m_LastFixedUpdateTime;
  19.         protected int m_FixedUpdateCount;
  20.         protected bool m_DidPushTime;
  21.         internal FixedRateCatchUpManagerCorrected(float fixedStep)
  22.         {
  23.             m_FixedTimeStep = fixedStep;
  24.         }
  25.         internal bool UpdateCallback(ComponentSystemGroup group)
  26.         {
  27.             m_LastFixedUpdateTime = group.World.Time.ElapsedTime;
  28.             m_FixedUpdateCount = 1;
  29.  
  30.             group.World.PushTime(new TimeData(
  31.             elapsedTime: m_LastFixedUpdateTime,
  32.             deltaTime: m_FixedTimeStep));
  33.  
  34.             group.UpdateCallback = this.InternalUpdateCallback;
  35.  
  36.             m_DidPushTime = true;
  37.             return true;
  38.         }
  39.  
  40.         internal bool InternalUpdateCallback(ComponentSystemGroup group)
  41.         {
  42.             // if this is true, means we're being called a second or later time in a loop
  43.             if (m_DidPushTime)
  44.             {
  45.                 group.World.PopTime();
  46.             }
  47.  
  48.             var elapsedTime = group.World.Time.ElapsedTime;
  49.  
  50.             if (elapsedTime - m_LastFixedUpdateTime >= m_FixedTimeStep)
  51.             {
  52.                 // Note that m_FixedTimeStep of 0.0f will never update
  53.                 m_LastFixedUpdateTime += m_FixedTimeStep;
  54.                 m_FixedUpdateCount++;
  55.             }
  56.             else
  57.             {
  58.                 m_DidPushTime = false;
  59.                 return false;
  60.             }
  61.  
  62.             group.World.PushTime(new TimeData(
  63.                 elapsedTime: m_LastFixedUpdateTime,
  64.                 deltaTime: m_FixedTimeStep));
  65.  
  66.             m_DidPushTime = true;
  67.             return true;
  68.         }
  69.     }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement