Ejemplo n.º 1
0
        private static async void RunGame(CancellationToken stoppingToken)
        {
            // MAIN LOOP
            const int   pollInterval        = 1000;              // ms
            int         timeToNextPoll      = pollInterval;
            int         timeToNextWorldTick = WorldTickInterval; // 1 worldTick = 10 seconds
            const int   sleepTime           = 100;
            const ulong sleepTimeLong       = (ulong)sleepTime;

            while (!stoppingToken.IsCancellationRequested && Running)
            {
                // Timing control
                await Task.Delay(sleepTime, stoppingToken);

                if (!Paused)
                {
                    WorldTime           += sleepTimeLong;
                    timeToNextPoll      -= sleepTime;
                    timeToNextWorldTick -= sleepTime;
                }

                // Check for and remove undetectable disconnected clients
                if (timeToNextPoll <= 0)
                {
                    NetworkServer.GetConnectionPolling();
                    timeToNextPoll += pollInterval;
                }

                // World Tick
                if (timeToNextWorldTick <= 0)
                {
                    GameEngine.RunWorldTick();
                    timeToNextWorldTick += WorldTickInterval;
                }

                // Player Turns
                GetActors().ForEach(p => p.DoTurn());

                // Update Changes to Clients
                Players.FindAll(p => p.HasClient && p.Client.HasUpdates).ForEach(p => p.Client.SendUpdate(p.Client.GetFullUpdateForClient()));
            }
        }