public void GameLoop() { int tickRate = 30; double oneSecondInMs = 1000.0; double oneTickLength = oneSecondInMs / tickRate; isRunning = true; sw = new Stopwatch(); sw.Start(); while (isRunning) { double startTime = sw.Elapsed.TotalMilliseconds; map.SetServerTime(sw.Elapsed.TotalMilliseconds); GameLogic(); double elapsedTime = sw.Elapsed.TotalMilliseconds - startTime; if (Server.CheckRoundEnd()) { foreach (Player player in playerList) { if (player.IsAlive()) { Server.scoreboard.AddScore(player, 1); break; } } string jsonMap = map.GetJson(settings); string jsonPlayers = JsonConvert.SerializeObject(Server.GetPlayers(), settings); string jsonScoreboard = JsonConvert.SerializeObject(Server.scoreboard); foreach (var player in playerList) { player.update(context, jsonMap, jsonPlayers, jsonScoreboard, 1); } ScoreboardTemplateProxy proxy = new ScoreboardTemplateProxy(scoreboard); ScoreboardTemplateProxy serverProxy = new ScoreboardTemplateProxy(Server.scoreboard); Console.WriteLine("Scoreboard"); Console.WriteLine(proxy.Log()); Console.WriteLine(); Console.WriteLine("Server scoreboard"); Console.WriteLine(serverProxy.Log()); GameSetup(); foreach (Player player in playerList) { player.ResetPlayer(); } Console.WriteLine("Baigtas raundas"); Thread.Sleep(2000); Console.WriteLine("Naujas raundas"); } if (elapsedTime < oneTickLength) { //await Task.Delay((int)(oneTickLength - elapsedTime)); Thread.Sleep((int)(oneTickLength - elapsedTime)); } } }
public void GameLogic() { foreach (Player player in playerList) { if (player.IsAlive()) { map.PerformPlayerActions(player, sw.Elapsed.TotalMilliseconds, scoreboard); } } map.UpdateExplosives(sw.Elapsed.TotalMilliseconds); //TODO: Make copies of map and players as it sometimes crashes string jsonMap = map.GetJson(settings); string jsonPlayers = JsonConvert.SerializeObject(Server.GetPlayers(), settings); string jsonScoreboard = JsonConvert.SerializeObject(scoreboard); foreach (var player in playerList) { player.update(context, jsonMap, jsonPlayers, jsonScoreboard, 0); } //context.All.SendAsync("SendData", jsonPlayers, jsonMap); }