void saveToJson(Planet planet) { PlanetToJson ptj = new PlanetToJson(); ptj = planet.formatPlanetToJson(); string dataAsJson = JsonUtility.ToJson(ptj); string filePath = Application.persistentDataPath + "/planetDataG" + planet.galaxy + "S" + planet.stellarSystem + "P" + planet.planetID + ".json"; File.WriteAllText(filePath, dataAsJson); }
public void formatPlanetFromJson(PlanetToJson planetFromJson) { if (savedMapHeight.GetLength(0) == 0 || savedMapMatrix == null) { int sizeInterpolation = Mathf.RoundToInt(horizontalSize * 0.05f); savedMapHeight = new int[horizontalSize + sizeInterpolation]; savedMapMatrix = new int[horizontalSize + sizeInterpolation, (verticalSize + heightMultiplier * 2)]; } // Création de mapDecrypted 1D int tailleMatrix = 0; for (int i = 0; i < savedMapMatrix.GetLength(0); i++) { tailleMatrix += (planetFromJson.savedMapHeight [i]); } int[] mapDecrypted = new int[tailleMatrix]; // Décryptage de savedMapMatrix avec le tableau du nombre d'occurence List <int> mapToDecrypt = new List <int> (); for (int i = 0; i < planetFromJson.savedMapMatrix.GetLength(0); i++) { for (int j = 0; j < planetFromJson.nbOccurence [i]; j++) { mapToDecrypt.Add(planetFromJson.savedMapMatrix [i]); } } // Affectation du tableau décrypté à mapMatrix mapDecrypted = mapToDecrypt.ToArray(); // Reconstruction du tableau complet à deux dimensions int depth = 0; for (int j = 0; j <= savedMapHeight [0]; j++) { if (j == 0) { savedMapMatrix [0, j] = 2; } else if (j <= planetFromJson.savedMapHeight[0]) { savedMapMatrix [0, j] = mapDecrypted [j]; } else { savedMapMatrix [0, j] = 0; } } for (int i = 1; i < savedMapMatrix.GetLength(0); i++) { depth += planetFromJson.savedMapHeight [i - 1]; for (int j = 0; j < savedMapMatrix.GetLength(1); j++) { if (j == 0) { savedMapMatrix [i, j] = 2; } else if (j <= planetFromJson.savedMapHeight[i]) { savedMapMatrix [i, j] = mapDecrypted [j + (depth - 1)]; } else { savedMapMatrix [i, j] = 0; } } } savedMapHeight = planetFromJson.savedMapHeight; tilesType = planetFromJson.tilesType; playerLastPosition = planetFromJson.playerLastPosition; }