Ejemplo n.º 1
0
    public void LoadMap(string name = null, bool preset = true)
    {
        name = name ?? m_levelData.Level.SceneName;
        string fullPath = preset ? this.directory + name : Path.Combine(Application.persistentDataPath, name);

        SerializedLevel serializedLevel;

        try
        {
            var jsonTextFile = Resources.Load <TextAsset>(fullPath).ToString();
            serializedLevel = JsonUtility.FromJson <SerializedLevel>(jsonTextFile);
        }
        catch (System.Exception e)
        {
            Debug.LogWarning($"An error occurred when trying to load map '{name}':" +
                             $"\n\t{e}");
            Debug.Log("Creating Empty level");
            serializedLevel = new SerializedLevel();
            serializedLevel.SetPlot(new SerializedPlot(new SerializedMapObjects(),
                                                       JsonUtility.FromJson <SerializedGrid>(File.ReadAllText(this.directory + "_defaultGrid.json"))));
        }
        Debug.Log("Loading " + name);
        m_plotIO.LoadPlot(serializedLevel.serializedPlot);
        //Animals loaded after map to avoid path finding issues
        this.PresetMap = serializedLevel;
        Reload();
    }
Ejemplo n.º 2
0
    public void SaveMap(string name = null, bool preset = true)
    {
        name = name ?? LevelOnPlay;
        name = name + ".json";
        string fullPath = preset ? "Assets/Resources/" + this.directory + name : Path.Combine(Application.persistentDataPath, name);

        Debug.Log("Saving Grid to " + fullPath);
        if (File.Exists(fullPath))
        {
            Debug.Log("Overwriting file at " + fullPath);
        }

        SerializedLevel level;

        try
        {
            level = new SerializedLevel();
            level.SetPopulations(m_populationManager);
            level.SetPlot(m_plotIO.SavePlot());
        }
        catch
        {
            Debug.LogError("Serialization error, NOT saved to protect existing saves");
            return;
        }

        using (StreamWriter streamWriter = new StreamWriter(fullPath))
            streamWriter.Write(JsonUtility.ToJson(level));
        Debug.Log("Grid Saved to: " + fullPath);
    }
Ejemplo n.º 3
0
    private SerializedLevel SaveLevel()
    {
        SerializedLevel level = new SerializedLevel();

        // Serialize plot
        level.SetPopulations(this.populationManager);
        // Serialize Animals
        level.SetPlot(this.plotIO.SavePlot());
        return(level);
    }
Ejemplo n.º 4
0
    // no idea what these two do
    public void ClearAnimals()
    {
        SerializedLevel level = new SerializedLevel();

        level.SetPopulations(m_populationManager);
        level.SetPlot(m_plotIO.SavePlot());
        level.serializedPopulations = new SerializedPopulation[0];
        this.PresetMap = level;

        foreach (Population population in m_populationManager.Populations)
        {
            population.RemoveAll();
        }

        m_populationManager.Initialize();
    }
Ejemplo n.º 5
0
    private void LoadFromFile(string fullPath)
    {
        SerializedLevel serializedLevel;

        try
        {
            var jsonTextFile = Resources.Load <TextAsset>(fullPath).ToString();
            serializedLevel = JsonUtility.FromJson <SerializedLevel>(jsonTextFile);
        }
        catch
        {
            Debug.LogWarning("No map save found for this scene, create a map using map designer or check your spelling");
            Debug.Log("Creating Empty level");
            serializedLevel = new SerializedLevel();
            serializedLevel.SetPlot(new SerializedPlot(new SerializedMapObjects(), this.CreateDefaultGrid()));
        }
        this.plotIO.LoadPlot(serializedLevel.serializedPlot);
        //Animals loaded after map to avoid path finding issues
        this.presetMap = serializedLevel;
        Reload();
    }