Пример #1
0
        /// <summary>
        /// Deserializes an object from an inputted string. This is handled differently depending on which
        /// kind of object it is.
        /// </summary>
        /// <param name="serializedObject">Serialized object string</param>
        private void UpdateObject(string serializedObject)
        {
            JObject obj = JObject.Parse(serializedObject);

            //Tank
            JToken token = obj["tank"];

            if (token != null)
            {
                Tank tank = JsonConvert.DeserializeObject <Tank>(serializedObject);
                TheWorld.Tanks[tank.ID] = tank;

                //Assigns a color to the tank and increments the seen players
                if (!TankColorRecord.ContainsKey(tank.ID))
                {
                    TankColorRecord.Add(tank.ID, SeenPlayers % 8);
                    SeenPlayers++;
                }

                //If it disconnected the tank is removed
                if (tank.Disconnected)
                {
                    TheWorld.Tanks.Remove(tank.ID);
                }

                //It also notes that the walls must be done if it's importing a tank
                wallsDone = true;
                return;
            }

            //Projectile
            token = obj["proj"];
            if (token != null)
            {
                Projectile proj = JsonConvert.DeserializeObject <Projectile>(serializedObject);
                TheWorld.Projectiles[proj.ID] = proj;
                //Removes projectiles when they die
                if (proj.Died)
                {
                    TheWorld.Projectiles.Remove(proj.ID);
                }
                return;
            }

            //Powerup
            token = obj["power"];
            if (token != null)
            {
                PowerUp power = JsonConvert.DeserializeObject <PowerUp>(serializedObject);
                TheWorld.PowerUps[power.ID] = power;
                //Removes powerups when they die
                if (power.Died)
                {
                    TheWorld.PowerUps.Remove(power.ID);
                }
                return;
            }

            //Beam
            token = obj["beam"];
            if (token != null)
            {
                Beam beam = JsonConvert.DeserializeObject <Beam>(serializedObject);
                TheWorld.Beams[beam.ID] = beam;
                return;
            }

            //Wall
            token = obj["wall"];
            if (token != null)
            {
                Wall wall = JsonConvert.DeserializeObject <Wall>(serializedObject);
                TheWorld.Walls[wall.ID] = wall;
                return;
            }
        }