public Player GeneratePlayer(LoggedClient loggedClient, Character character, OperationContext operationContext) { try { var freeInd = int.MaxValue; for (var i = 0; i < GameSessionClients.Length; i++) { if (GameSessionClients[i] != null) { continue; } freeInd = i; break; } lock (SyncRootToPlayers) { var player = new Player(freeInd, _random.Next(_maxGlobalX), _random.Next(_maxGlobalY), 6, 4.5, character); Players[freeInd] = player; GameSessionClients[freeInd] = new GameSessionClient() { Player = player, OperationContext = operationContext, LoggedClient = loggedClient }; GameSessionManager.GameSessions[_currentSession.Id].NumberPlayers++; return(player); } } catch { return(null); } }
public GameMap(GameSession gameSession) { GameSessionClients = new GameSessionClient[gameSession.MaxPlayers]; Players = new Player[gameSession.MaxPlayers]; _currentSession = gameSession; switch (gameSession.Map) { case GameSession.MapSize.Small: _tickTimeout = 30; Foods = new Food[80]; _maxGlobalX = 2000; _maxGlobalY = 2000; for (var i = 0; i < 80; i++) { Foods[i] = GenerateFood(i); } break; case GameSession.MapSize.Medium: _tickTimeout = 25; Foods = new Food[200]; _maxGlobalX = 3000; _maxGlobalY = 3000; for (var i = 0; i < 200; i++) { Foods[i] = GenerateFood(i); } break; case GameSession.MapSize.High: _tickTimeout = 20; Foods = new Food[300]; _maxGlobalX = 4000; _maxGlobalY = 4000; for (var i = 0; i < 300; i++) { Foods[i] = GenerateFood(i); } break; case GameSession.MapSize.VeryHigh: _tickTimeout = 15; Foods = new Food[500]; _maxGlobalX = 5000; _maxGlobalY = 5000; for (var i = 0; i < 500; i++) { Foods[i] = GenerateFood(i); } break; default: throw new ArgumentOutOfRangeException(nameof(gameSession), gameSession, null); } _cancellationMapTick = new CancellationTokenSource(); var mapRender = new Task(() => MapTick(_cancellationMapTick.Token), _cancellationMapTick.Token); mapRender.Start(); }