Finished load and save mechanic, v1.3.0
This commit is contained in:
@@ -117,6 +117,7 @@ namespace Assets.Scripts
|
||||
public void saveGame()
|
||||
{
|
||||
audioHandler.playButtonClick();
|
||||
FileHandler.generateDirectory();
|
||||
string saveString = "{\r\n\"player\": {\r\n" + player.saveGame();
|
||||
saveString = saveString + "\r\n},\r\n\"world\": {\r\n" + worldGenerator.saveGame() + "\r\n}\r\n}";
|
||||
FileHandler.saveGame(saveString, "./save.json");
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
@@ -154,4 +155,80 @@ public class ContentGenerator : MonoBehaviour
|
||||
return boss;
|
||||
}
|
||||
}
|
||||
|
||||
public GameObject loadObject(JToken json)
|
||||
{
|
||||
GameObject result = gameObject;
|
||||
string name = json["objectname"].ToString().Replace("(Clone)", "");
|
||||
if (name.ToLower().Contains("stone"))
|
||||
{
|
||||
foreach (GameObject stone in stones)
|
||||
{
|
||||
if (stone.name == name)
|
||||
{
|
||||
result = stone;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (GameObject tree in trees)
|
||||
{
|
||||
if (tree.name == name)
|
||||
{
|
||||
result = tree;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public GameObject loadEnemy(JToken json)
|
||||
{
|
||||
GameObject result = gameObject;
|
||||
string name = json["enemyname"].ToString().Replace("(Clone)", "");
|
||||
if (name.Split(' ').Length > 1)
|
||||
{
|
||||
name = name.Split(' ')[1];
|
||||
}
|
||||
if (name == "(Boss)")
|
||||
{
|
||||
result = boss;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (name)
|
||||
{
|
||||
case "(Metal)":
|
||||
name = "SlimeMetalIdle";
|
||||
break;
|
||||
case "(MiniBoss)":
|
||||
name = "SlimeMiniBossIdle";
|
||||
break;
|
||||
case "(Water)":
|
||||
name = "SlimeWaterIdle";
|
||||
break;
|
||||
case "(Mage)":
|
||||
name = "SlimeMageIdle";
|
||||
break;
|
||||
case "(Warrior)":
|
||||
name = "SlimeWarriorIdle";
|
||||
break;
|
||||
default:
|
||||
name = "SlimeBaseIdle";
|
||||
break;
|
||||
}
|
||||
foreach (GameObject enemy in enemies)
|
||||
{
|
||||
if (enemy.name == name)
|
||||
{
|
||||
result = enemy;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Assets.Scripts;
|
||||
using Assets.Scripts.Slimes;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
@@ -130,5 +131,10 @@ namespace Assets.Scripts
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public void loadEnemy(JToken json)
|
||||
{
|
||||
slime = new BasicSlime(json);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,14 +16,7 @@ namespace Assets.Scripts
|
||||
static StreamWriter sw;
|
||||
public static void saveGame(string data, string path)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
sw = File.CreateText(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
sw = new StreamWriter(path);
|
||||
}
|
||||
sw = new StreamWriter(path);
|
||||
sw.Write(data);
|
||||
sw.Flush();
|
||||
sw.Close();
|
||||
@@ -39,7 +32,7 @@ namespace Assets.Scripts
|
||||
{
|
||||
jsonString = jsonString + line.Replace("\r\n", "");
|
||||
}
|
||||
JArray json = JsonConvert.DeserializeObject<JArray>(jsonString);
|
||||
JObject json = JsonConvert.DeserializeObject<JObject>(jsonString);
|
||||
player.loadPlayer(json["player"]);
|
||||
worldGenerator.loadWorld(json["world"]);
|
||||
}
|
||||
@@ -47,14 +40,7 @@ namespace Assets.Scripts
|
||||
|
||||
public static void saveAudio(string path, float music, float effects)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
sw = File.CreateText(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
sw = new StreamWriter(path);
|
||||
}
|
||||
sw = new StreamWriter(path);
|
||||
sw.WriteLine("Music:" + music);
|
||||
sw.WriteLine("Effects:" + effects);
|
||||
sw.Flush();
|
||||
@@ -86,5 +72,44 @@ namespace Assets.Scripts
|
||||
{
|
||||
return File.Exists("./save.json");
|
||||
}
|
||||
|
||||
public static void saveTile(string content, string path)
|
||||
{
|
||||
sw = new StreamWriter(path);
|
||||
sw.Write(content);
|
||||
sw.Flush();
|
||||
sw.Close();
|
||||
}
|
||||
|
||||
public static void saveNoise(string content, string path)
|
||||
{
|
||||
sw = new StreamWriter(path, true);
|
||||
sw.Write(content);
|
||||
sw.Flush();
|
||||
sw.Close();
|
||||
}
|
||||
|
||||
public static void generateDirectory()
|
||||
{
|
||||
if (!Directory.Exists("./save/"))
|
||||
{
|
||||
Directory.CreateDirectory("./save/");
|
||||
}
|
||||
else
|
||||
{
|
||||
Directory.Delete("./save/");
|
||||
}
|
||||
}
|
||||
|
||||
public static string loadTile(string path)
|
||||
{
|
||||
string result = "";
|
||||
string[] lines = File.ReadAllLines(path);
|
||||
foreach (string line in lines)
|
||||
{
|
||||
result = result + line.Replace("\r\n", "");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Linq;
|
||||
|
||||
public class NoiseGenerator
|
||||
{
|
||||
@@ -230,7 +232,7 @@ public class NoiseGenerator
|
||||
tile.GetComponent<MeshCollider>().sharedMesh = mesh;
|
||||
}
|
||||
|
||||
public string saveTile(GameObject tile)
|
||||
public void saveTile(GameObject tile, string path)
|
||||
{
|
||||
string result = "";
|
||||
Vector3[] vertices = tile.GetComponent<MeshFilter>().mesh.vertices;
|
||||
@@ -247,13 +249,38 @@ public class NoiseGenerator
|
||||
result = result + "\r\n},\"colors\": {\r\n";
|
||||
for (int i = 0; i < colors.Length; i++)
|
||||
{
|
||||
result = result + FileHandler.generateJSON("color" + i, "\"" + colors[i].r + "/" + colors[i].g + "/" + colors[i].b + "\"");
|
||||
result = result + FileHandler.generateJSON("color" + i, "\"" + colors[i].r + "/" + colors[i].g + "/" + colors[i].b + "/" + colors[i].a + "\"");
|
||||
if (i < colors.Length - 1)
|
||||
{
|
||||
result = result + ",\r\n";
|
||||
}
|
||||
}
|
||||
result = result + "\r\n}";
|
||||
return result;
|
||||
result = result + "\r\n}\r\n}";
|
||||
FileHandler.saveNoise(result, path);
|
||||
}
|
||||
|
||||
public void loadTile(GameObject tile, JToken jsonVertices, JToken jsonColors)
|
||||
{
|
||||
var jsonData = JObject.Parse(jsonColors.ToString()).Children();
|
||||
List<JToken> colorTokens = jsonData.Children().ToList();
|
||||
jsonData = JObject.Parse(jsonVertices.ToString()).Children();
|
||||
List<JToken> verticeTokens = jsonData.Children().ToList();
|
||||
Color32[] colors = new Color32[colorTokens.Count];
|
||||
Vector3[] vertices = new Vector3[verticeTokens.Count];
|
||||
JToken current;
|
||||
string[] parts;
|
||||
for(int i = 0; i < colorTokens.Count;i++)
|
||||
{
|
||||
current = colorTokens[i];
|
||||
parts = current.Value<string>().Split('/');
|
||||
colors[i] = new Color32(byte.Parse(parts[0]), byte.Parse(parts[1]), byte.Parse(parts[2]), byte.Parse(parts[3]));
|
||||
}
|
||||
for (int i = 0; i < verticeTokens.Count; i++)
|
||||
{
|
||||
current = verticeTokens[i];
|
||||
parts = current.Value<string>().Split('/');
|
||||
vertices[i] = new Vector3(float.Parse(parts[0]), float.Parse(parts[1]), float.Parse(parts[2]));
|
||||
}
|
||||
applyMesh(tile, vertices, tile.GetComponent<MeshFilter>().mesh, colors);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -601,6 +601,7 @@ namespace Assets.Scripts
|
||||
isDodging = bool.Parse(json["isDodging"].ToString());
|
||||
killcount = (int)json["killcount"];
|
||||
difficulty = (int)json["difficulty"];
|
||||
generateSkills();
|
||||
}
|
||||
|
||||
private void loadRole(string name)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -33,6 +34,19 @@ namespace Assets.Scripts.Slimes
|
||||
level = playerStats[7];
|
||||
}
|
||||
|
||||
public BasicSlime(JToken json)
|
||||
{
|
||||
maxHealth = (int)json["maxHealth"];
|
||||
maxSecondary = (int)json["maxSecondary"];
|
||||
secondary = (int)json["secondary"];
|
||||
health = (int)json["health"];
|
||||
strength = (int)json["strength"];
|
||||
dexterity = (int)json["dexterity"];
|
||||
intelligence = (int)json["intelligence"];
|
||||
level = (int)json["level"];
|
||||
experience = (int)json["experience"];
|
||||
}
|
||||
|
||||
public int[] getStats()
|
||||
{
|
||||
int[] result = { health, maxHealth, secondary, maxSecondary, strength, dexterity, intelligence };
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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