Finished load and save mechanic, v1.3.0
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using Assets.Scripts;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public class Tile : MonoBehaviour
|
||||
@@ -215,9 +217,9 @@ public class Tile : MonoBehaviour
|
||||
return tiletype;
|
||||
}
|
||||
|
||||
public string saveTile()
|
||||
public void saveTile(string path)
|
||||
{
|
||||
string result = "";
|
||||
string result = "{\r\n";
|
||||
GameObject obj;
|
||||
result = result + FileHandler.generateJSON("tiletype", "\"" + tiletype + "\"") + ",\r\n";
|
||||
result = result + FileHandler.generateJSON("position", "\"" + position.x + "/" + position.y + "/" + position.z + "\"") + ",\r\n";
|
||||
@@ -240,8 +242,8 @@ public class Tile : MonoBehaviour
|
||||
result = result + ",\r\n";
|
||||
}
|
||||
}
|
||||
result = result + "\r\n}";
|
||||
return result;
|
||||
result = result + "\r\n},";
|
||||
FileHandler.saveTile(result, path);
|
||||
}
|
||||
|
||||
public string saveCurrent()
|
||||
@@ -250,4 +252,48 @@ public class Tile : MonoBehaviour
|
||||
result = result + "\"" + position.x + "/" + position.y + "/" + position.z + "\"";
|
||||
return result;
|
||||
}
|
||||
|
||||
public void loadTile(JToken json, Vector3 pos)
|
||||
{
|
||||
var jsonData = JObject.Parse(json["objects"].ToString()).Children();
|
||||
List<JToken> tokens = jsonData.Children().ToList();
|
||||
contentGenerator = GameObject.Find("ContentGenerator");
|
||||
GameObject spawnedObject;
|
||||
Vector3 position;
|
||||
foreach (JToken obj in tokens)
|
||||
{
|
||||
if (obj["objectname"] != null)
|
||||
{
|
||||
if (obj["objectname"].ToString() != "pnlWater")
|
||||
{
|
||||
spawnedObject = contentGenerator.GetComponent<ContentGenerator>().loadObject(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
spawnedObject = null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
spawnedObject = contentGenerator.GetComponent<ContentGenerator>().loadEnemy(obj);
|
||||
}
|
||||
if (spawnedObject != null)
|
||||
{
|
||||
position = new Vector3(float.Parse(obj["position"].ToString().Split('/')[0]), float.Parse(obj["position"].ToString().Split('/')[1]), float.Parse(obj["position"].ToString().Split('/')[2]));
|
||||
spawnedObject = Instantiate(spawnedObject, position, Quaternion.identity);
|
||||
spawnedObject.transform.parent = gameObject.transform;
|
||||
if (spawnedObject.tag.Contains("Enemy"))
|
||||
{
|
||||
aliveEnemies.Add(spawnedObject);
|
||||
if (obj["health"] != null)
|
||||
{
|
||||
spawnedObject.GetComponent<Enemy>().loadEnemy(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
tiletype = json["tiletype"].ToString();
|
||||
setPosition(pos);
|
||||
setBorders();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user