/// <summary> /// Runs a frame of the game server. /// </summary> public void Run() { // Target frequency const int targetFrequency = 20; const int targetMilliseconds = 1000 / targetFrequency; TimeSpan frameTime = new TimeSpan(FrameTimer.ElapsedTicks); FrameTimer.Restart(); // Update ECS system. ECS.Update(frameTime); // Update game state switch (GameMaster.State) { case GameMaster.GameState.WaitingForPlayers: // Check timer hasn't exceeded max wait time if (Timer.Elapsed.TotalSeconds >= Settings.MaxWaitingTime) { GameMaster.StartCountdown( Settings.CountdownTime ); } break; case GameMaster.GameState.Countdown: // Check timer hasn't exceeded countdown time if (Timer.Elapsed.TotalSeconds >= Settings.CountdownTime) { GameMaster.StartGame(); } break; case GameMaster.GameState.InGame: // Check clients are still connected if (NetworkState.ConnectedClients <= 0) { GameMaster.EndGame(); } break; case GameMaster.GameState.GameOver: // Check timer hasn't exceeded end game time if (Timer.Elapsed.TotalSeconds >= Settings.EndgameTime) { GameMaster.State = GameMaster.GameState.Shutdown; } break; } // Wait for timer to reach target time while (FrameTimer.ElapsedMilliseconds < targetMilliseconds) { Thread.Sleep(1); } }