private void WorldUpdate()
{
//Check if we're supposed to be running
while (m_updateWorld)
{
//Calculate update delta
TimeSpan dt = TimeSpan.FromTicks(Environment.TickCount - m_lastWorldUpdateTime);
DateTime updateStartTime = DateTime.Now;
//Time for some expensive O(n) badness...
foreach (ServerEntity entity in m_entities.Values)
{
//Handle disconnect
if (!entity.IsConnected)
{
ServerEntity e = null;
if (m_entities.TryRemove(entity.WorldID, out e))
{
Console.WriteLine("Entity disconnected: " + e.WorldID);
ZoneManager.RemoveEntity(e);
e.Dispose();
}
continue;
}
if (entity.AuthState == EntityAuthState.Authorised)
{
//Update nearby entities with each other
ZoneManager.PushNearbyEntities(entity);
}
//Call update on this entity
entity.Update(dt);
}
//Track update times
m_lastWorldUpdateTime = Environment.TickCount;
//Calculate how long to sleep for, based on how long the world update took
LastUpdateDelta = (int)(DateTime.Now - updateStartTime).TotalMilliseconds;
int sleepTime = WORLD_UPDATE_TARGET_MS - LastUpdateDelta;
Thread.Sleep(Math.Max(0, sleepTime));
}
}