Skip to content

Instantly share code, notes, and snippets.

@AldeRoberge
Created January 6, 2021 04:21
Show Gist options
  • Save AldeRoberge/b6e87a0ea14948109574e2b882828333 to your computer and use it in GitHub Desktop.
Save AldeRoberge/b6e87a0ea14948109574e2b882828333 to your computer and use it in GitHub Desktop.
Ticking logic for world
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