diff --git a/Assets/Scripts/FileHandler.cs b/Assets/Scripts/FileHandler.cs index 354c95d..0882c07 100644 --- a/Assets/Scripts/FileHandler.cs +++ b/Assets/Scripts/FileHandler.cs @@ -6,6 +6,8 @@ using System.Text; using System.Threading.Tasks; using UnityEngine; using UnityEngine.UI; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; namespace Assets.Scripts { @@ -37,6 +39,9 @@ namespace Assets.Scripts { jsonString = jsonString + line.Replace("\r\n", ""); } + JArray json = JsonConvert.DeserializeObject(jsonString); + player.loadPlayer(json["player"]); + worldGenerator.loadWorld(json["world"]); } } diff --git a/Assets/Scripts/Player.cs b/Assets/Scripts/Player.cs index 88b8d89..dd917eb 100644 --- a/Assets/Scripts/Player.cs +++ b/Assets/Scripts/Player.cs @@ -7,6 +7,7 @@ using UnityEngine; using UnityEngine.UI; using Assets.Scripts.Classes; using Assets.Scripts.Races; +using Newtonsoft.Json.Linq; namespace Assets.Scripts { @@ -580,5 +581,64 @@ namespace Assets.Scripts result = result + FileHandler.generateJSON("difficulty", difficulty); return result; } + + public void loadPlayer(JToken json) + { + playername = json["playername"].ToString(); + 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"]; + maxExperience = (int)json["maxExperience"]; + loadRole(json["role"].ToString()); + loadRace(json["race"].ToString()); + points = (int)json["points"]; + isDodging = bool.Parse(json["isDodging"].ToString()); + killcount = (int)json["killcount"]; + difficulty = (int)json["difficulty"]; + } + + private void loadRole(string name) + { + switch (name) + { + case "Warrior": + role = new WarriorClass(); + break; + case "Mage": + role = new MageClass(); + break; + case "Thief": + role = new ThiefClass(); + break; + } + } + + private void loadRace(string name) + { + switch (name) + { + case "Human": + race = new HumanRace(); + break; + case "Dwarf": + race = new DwarvenRace(); + break; + case "Elf": + race = new ElvenRace(); + break; + case "Giant": + race = new GiantRace(); + break; + case "Goblin": + race = new GoblinRace(); + break; + } + } } }