Created
January 6, 2021 04:21
-
-
Save AldeRoberge/b6e87a0ea14948109574e2b882828333 to your computer and use it in GitHub Desktop.
Ticking logic for world
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void TickLoop() | |
{ | |
Log.Info("Logic loop started."); | |
int loopTime = 0; | |
Stopwatch watch = Stopwatch.StartNew(); | |
RealmTime t = new RealmTime(); | |
while (true) | |
{ | |
t.TotalElapsedMs = watch.ElapsedMilliseconds; | |
t.TickDelta = loopTime / LogicTickerConstants.MillisecondsPerTick; | |
t.TickCount += t.TickDelta; | |
t.ElaspedMsDelta = t.TickDelta * LogicTickerConstants.MillisecondsPerTick; | |
if (t.TickDelta > 3) | |
{ | |
Log.Warn("LAGGED! | ticks:" + t.TickDelta + " ms: " + loopTime + " tps: " + t.TickCount / (t.TotalElapsedMs / 1000.0)); | |
} | |
if (Terminating) | |
{ | |
break; | |
} | |
// Tick the worlds | |
TickWorlds(t); | |
// Sleep for the amount of time left for this tick (might be 0 if tick took too long to compute) | |
int logicTime = (int) (watch.ElapsedMilliseconds - t.TotalElapsedMs); | |
ManualResetEvent.WaitOne(Math.Max(0, LogicTickerConstants.MillisecondsPerTick - logicTime)); | |
// Calculate time this loop took | |
loopTime += (int) (watch.ElapsedMilliseconds - t.TotalElapsedMs) - t.ElaspedMsDelta; | |
} | |
Log.Info("Logic loop stopped."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment