Finished load and save mechanic, v1.3.0
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
using Assets.Scripts;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
@@ -21,9 +24,14 @@ public class WorldGenerator : MonoBehaviour
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
tiles = new Dictionary<Vector3, GameObject>();
|
||||
tiles.Add(new Vector3(0,0,0), GameObject.Find("Spawn"));
|
||||
tiles.Add(new Vector3(0, 0, 0), GameObject.Find("Spawn"));
|
||||
renderedTiles = new List<GameObject>();
|
||||
currentTile = GameObject.Find("Spawn");
|
||||
currentTile.GetComponent<Tile>().setPosition(new Vector3(0, 0, 0));
|
||||
@@ -53,7 +61,7 @@ public class WorldGenerator : MonoBehaviour
|
||||
}
|
||||
player.transform.position = new Vector3(0,1.5f,0);
|
||||
player.transform.rotation = Quaternion.identity;
|
||||
Start();
|
||||
OnEnable();
|
||||
currentTile.GetComponent<Tile>().resetSpawn();
|
||||
this.cityAmount = cityAmount;
|
||||
maxCityAmount = cityAmount;
|
||||
@@ -184,6 +192,7 @@ public class WorldGenerator : MonoBehaviour
|
||||
{
|
||||
string result = "";
|
||||
int counter = 0;
|
||||
string savePath = "";
|
||||
result = result + FileHandler.generateJSON("cityAmount", cityAmount) + ",\r\n";
|
||||
result = result + FileHandler.generateJSON("maxCityAmount", maxCityAmount) + ",\r\n";
|
||||
result = result + "\"currentTile\": " + currentTile.GetComponent<Tile>().saveCurrent() + ",\r\n";
|
||||
@@ -192,10 +201,18 @@ public class WorldGenerator : MonoBehaviour
|
||||
{
|
||||
if (tile.name != "Spawn")
|
||||
{
|
||||
result = result + "\"tile" + counter + "\": {\r\n";
|
||||
result = result + tile.GetComponent<Tile>().saveTile() + ",\r\n";
|
||||
result = result + noise.saveTile(tile) + "\r\n}";
|
||||
if (counter < tiles.Count - 1)
|
||||
savePath = "./save/tile" + counter + ".json";
|
||||
result = result + "\"tile" + counter + "\": \"" + savePath + "\"";
|
||||
tile.GetComponent<Tile>().saveTile(savePath);
|
||||
if (tile.GetComponent<Tile>().getTileType() == "CityTile")
|
||||
{
|
||||
FileHandler.saveNoise("\r\n}", savePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
noise.saveTile(tile, savePath);
|
||||
}
|
||||
if (counter < tiles.Count - 2)
|
||||
{
|
||||
result = result + ",\r\n";
|
||||
}
|
||||
@@ -205,4 +222,39 @@ public class WorldGenerator : MonoBehaviour
|
||||
result = result + "\r\n}";
|
||||
return result;
|
||||
}
|
||||
|
||||
public void loadWorld(JToken json)
|
||||
{
|
||||
resetGame(0);
|
||||
cityAmount = (int)json["cityAmount"];
|
||||
maxCityAmount = (int)json["maxCityAmount"];
|
||||
string[] vectorParts = json["currentTile"].ToString().Split('/');
|
||||
Vector3 current = new Vector3(float.Parse(vectorParts[0]), float.Parse(vectorParts[1]), float.Parse(vectorParts[2]));
|
||||
GameObject loadedTile;
|
||||
Vector3 mapPos;
|
||||
Vector3 pos;
|
||||
foreach (JProperty tilePath in json["map"])
|
||||
{
|
||||
JToken jsonData = JObject.Parse(FileHandler.loadTile(tilePath.Value.ToString()));
|
||||
vectorParts = jsonData["position"].ToString().Split('/');
|
||||
pos = new Vector3(float.Parse(vectorParts[0]), float.Parse(vectorParts[1]), float.Parse(vectorParts[2]));
|
||||
mapPos = new Vector3(pos.x * 100, 0, pos.z * 100);
|
||||
if (jsonData["tiletype"].ToString() == "CityTile")
|
||||
{
|
||||
loadedTile = Instantiate(city, mapPos, Quaternion.identity);
|
||||
}
|
||||
else
|
||||
{
|
||||
loadedTile = Instantiate(tile, mapPos, Quaternion.identity);
|
||||
loadedTile.GetComponent<Tile>().loadTile(jsonData, pos);
|
||||
noise.loadTile(loadedTile, jsonData["vertices"], jsonData["colors"]);
|
||||
}
|
||||
tiles.Add(pos, loadedTile);
|
||||
renderedTiles.Add(loadedTile);
|
||||
}
|
||||
currentTile = tiles[current];
|
||||
updateRenderedTiles();
|
||||
Vector3 position = new Vector3(currentTile.transform.position.x, 5, currentTile.transform.position.z);
|
||||
player.transform.SetPositionAndRotation(position, player.transform.rotation);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user