Beispiel #1
0
        /// <summary>
        /// We will receive the player's name and assign an ID number
        /// At this time we will also send to the client world size, the ID number
        /// and the walls
        /// </summary>
        /// <param name="client"></param>
        private void GetPlayerInfo(SocketState client)
        {
            //Check if error occured and write message to console
            if (client.ErrorOccured)
            {
                Console.WriteLine(client.ErrorMessage);
            }

            //Set the new callback action
            client.OnNetworkAction = GetActionDataFromClient;

            //Get the player name
            string playerName = client.GetData().Trim('\n');

            //Create a new tank representing the player at a random location
            Tank newPlayer = new Tank(playerNumber, playerName, RandomLocationGenerator())
            {
                //Allows the tank to fire upon spawn
                FrameCount = framesBetweenShot + 1
            };

            //We don't spawn on walls or powerups
            while (CheckForCollision(newPlayer, 1))
            {
                newPlayer.Location = new Vector2D(RandomLocationGenerator());
            }

            //Add player to our connections
            lock (connections)
            {
                //Send ID and worldsize info
                Networking.Send(client.TheSocket, playerNumber.ToString() + "\n" + serverWorld.Size.ToString() + "\n");
                //Add socket state to the collection of players with their ID number
                connections.Add(client, playerNumber);
            }

            Console.WriteLine("Player " + playerNumber.ToString() + ": " + playerName + " has connected.");

            //Add player to server world
            lock (serverWorld.Tanks)
            {
                serverWorld.Tanks.Add(newPlayer.GetID(), newPlayer);
                //Increase player ID number
                playerNumber++;
            }

            //Create a string builder info to serialize and send all the walls
            StringBuilder wallinfo = new StringBuilder();

            foreach (Wall wall in serverWorld.Walls.Values)
            {
                wallinfo.Append(JsonConvert.SerializeObject(wall) + "\n");
            }

            //Send walls to the client
            Networking.Send(client.TheSocket, wallinfo.ToString());

            //Empty the socket state of data
            client.ClearData();

            //Begin receive loop
            Networking.GetData(client);
        }