Пример #1
0
        public void Deserialize(string json)
        {
            var puzzle_entities = new List <PuzzleEntityModel>();

            var puzzle_info = JSONString.Parse(json);

            var puzzle_entity_infos = puzzle_info["puzzle_entities"].AsArray;
            var command_infos       = puzzle_info["commands"].AsArray;
            var command_group_infos = puzzle_info["command_groups"].AsArray;

            foreach (JSONObject puzzle_entity_info in puzzle_entity_infos)
            {
                var new_puzzle_entity = GameObject.Instantiate(Resources.Load <GameObject>("Prefabs/PuzzleEntities/" + puzzle_entity_info["name"].Value));
                new_puzzle_entity.AddComponent <PuzzleEntityModel>().FromJson(puzzle_entity_info);

                puzzle_entities.Add(new_puzzle_entity.GetComponent <PuzzleEntityModel>());
            }

            List <CommandModel> single_commands = new List <CommandModel>();

            foreach (JSONObject command_info in command_infos)
            {
                var new_command = GameObject.Instantiate <GameObject>(Resources.Load <GameObject>("Prefabs/Commands/" + command_info["name"].Value));

                var single_command = new_command.AddComponent <SingleCommandModel>();
                single_command.Playable = puzzle_entities[command_info["playable_entity_id"].AsInt].GetComponent <PlayableModel>();
                single_command.FromJson(command_info);

                single_commands.Add(single_command);
            }

            List <CommandModel> command_groups = new List <CommandModel>();

            foreach (JSONObject command_group_info in command_group_infos)
            {
                var new_command_group      = new GameObject();
                var command_group          = new_command_group.AddComponent <CommandGroupModel>();
                int playable_id            = command_group_info["playable_entity_id"].AsInt;
                int conditional_command_id = command_group_info["command_ids"][0].AsInt;

                command_group.Playable           = puzzle_entities[playable_id].GetComponent <PlayableModel>();
                command_group.ConditionalCommand = conditional_command_id == -1 ?
                                                   null :
                                                   single_commands[conditional_command_id] as SingleCommandModel;

                command_group.FromJson(command_group_info);
                command_groups.Add(command_group);
            }

            for (int i = 0; i < command_groups.Count; ++i)
            {
                var command_ids = command_group_infos[i]["command_ids"].AsArray;

                for (int j = 1; j < command_ids.Count; ++j)
                {
                    int id = command_ids[j].AsInt;

                    var commands = id < 0 ? command_groups : single_commands;

                    id = id < 0 ? (id + 1) * -1 : id;

                    var command_group = command_groups[i] as CommandGroupModel;
                    command_group.CommandsSequence.Add(commands[id]);
                }
            }

            var tile_entities = from puzzle_entity in puzzle_entities
                                where puzzle_entity.GetComponent <TileModel>()
                                select puzzle_entity;

            tiles.Clear();
            foreach (var tile_entity in tile_entities)
            {
                tiles.Add(convertVector3ToCellNo(tile_entity.transform.position), tile_entity.GetComponent <PuzzleEntityModel>());

                var upper_entity = from entity in puzzle_entities
                                   where !entity.GetComponent <TileModel>() &&
                                   entity.transform.position == tile_entity.transform.position + new Vector3(0, 1, 0)
                                   select entity;

                if (upper_entity.Count() == 0)
                {
                    continue;
                }

                tile_entity.GetComponent <TileModel>().PuzzleEntity = upper_entity.ToArray <PuzzleEntityModel>()[0];
            }
        }