Пример #1
0
    /// <summary>
    /// Loads in the data from the a file who's name matches the passed in
    /// name. If the file fails to load false is returned otherwise the
    /// map editor data is populated. If the level editor is not null its
    /// UI data is populated. If the passed in bool is true then the real
    /// prefabs are instanciated within the scene instead of the map objects.
    /// </summary>
    /// <param name="t_fileName">The name of the file that is to be loaded</param>
    /// <param name="t_loadRealObjects">Bool for it the original prefabs should be placed or the fake ones</param>
    /// <returns>Bool for if the file was loaded succesfuly or not</returns>
    public bool LoadLevel(string t_fileName, bool t_loadRealObjects)
    {
        if (t_fileName != "")
        {
            LevelSaveData saveData = LevelSave.LoadLevel(t_fileName);

            if (saveData != null)
            {
                ClearAllData();
                CreateMap(saveData.m_mapWidth, saveData.m_mapHeight);

                foreach (string path in saveData.m_tileSpritePaths)
                {
                    LoadtTileSprite(path);

                    if (m_levelEditor != null)
                    {
                        m_levelEditor.CreateSpriteButton(path);
                    }
                }

                for (int x = 0; x < saveData.m_spriteNames.Count; x++)
                {
                    ColumnWrapper wrapperClass = saveData.m_spriteNames[x];

                    for (int y = 0; y < wrapperClass.m_spriteNames.Count; y++)
                    {
                        Sprite sprite = m_map.GetTile(new MapIndex(x, y)).GetSprite();

                        foreach (string path in m_tileSprites.Keys)
                        {
                            if (path.Contains(wrapperClass.m_spriteNames[y]))
                            {
                                sprite = m_tileSprites[path];
                                break;
                            }
                        }

                        m_map.GetTile(new MapIndex(x, y)).SetSprite(sprite);
                    }
                }

                for (int i = 0; i < saveData.m_objectsData.Count; i++)
                {
                    LoadMapObject(saveData.m_objectsData[i].m_name,
                                  saveData.m_objectsData[i].m_width,
                                  saveData.m_objectsData[i].m_height);

                    if (m_levelEditor != null)
                    {
                        m_levelEditor.CreateObjectButton(m_mapObjects[saveData.m_objectsData[i].m_name]);
                    }
                }

                for (int i = 0; i < saveData.m_placedObjects.Count; i++)
                {
                    if (m_mapObjects.ContainsKey(saveData.m_placedObjects[i].m_name))
                    {
                        MapObject mapObject = m_mapObjects[saveData.m_placedObjects[i].m_name];

                        Vector2 position = saveData.m_placedObjects[i].m_position;

                        position.x = position.x - ((mapObject.m_width - 1) * m_map.m_tileSize) / 2;
                        position.y = position.y - ((mapObject.m_height - 1) * m_map.m_tileSize) / 2;

                        MapIndex mapIndex = m_map.WorldPositionToMapIndex(position);

                        if (t_loadRealObjects)
                        {
                            InstantiateRealObject(mapIndex, mapObject);
                        }

                        else
                        {
                            InstantiateMapObject(mapIndex, mapObject);
                        }
                    }
                }

                return(true);
            }
        }

        return(false);
    }