Пример #1
0
        private static void GameFrameLoop(object sender, DoWorkEventArgs e)
        {
            object[]                   parameters           = e.Argument as object[];
            GameController             gameController       = (GameController)parameters[0];
            ServerNetworkingController networkingController = (ServerNetworkingController)parameters[1];

            Stopwatch stopwatch            = new Stopwatch();
            int       millisecondsPerFrame = gameController.GameSettings.MillisecondsPerFrame; // just testing

            while (true)
            {
                // Wait for millisecondsPerFrame
                stopwatch.Start();
                while (stopwatch.ElapsedMilliseconds < millisecondsPerFrame)
                {
                    Thread.Sleep(1);
                }
                stopwatch.Reset();

                // Update the world through the gameController
                lock (gameController) {
                    gameController.UpdateWorld();
                }

                // Send the JSON reperesenting the game world to the clients
                string latestGameWorldJson = gameController.SerializeGameWorld();
                networkingController.SendMessageToEveryClient(latestGameWorldJson);

                // check if cancellation is pending. if so, exit method
                if (e.Cancel)
                {
                    return;
                }
            }
        }