public static HeroSave FromJson(HeroSaveJson source) { HeroClass heroClass = null; switch (source.HeroClass.Name) { case "Warrior": { heroClass = LoadWarrior(source.HeroClass); break; } default: { throw new ApplicationException($"Unknown hero class '{source.HeroClass.Name}'."); } } var equipmentItemList = ConvertFromJsonList(source.Equipment); var equipment = new Equipment(); for (var i = 0; i < equipmentItemList.Count; i++) { equipment[i] = equipmentItemList[i]; } var inventoryItemList = ConvertFromJsonList(source.Inventory); var inventory = new Inventory(Option.InventoryCapacity); inventory.AddRange(inventoryItemList); var backPackItemList = ConvertFromJsonList(source.BackPack); var backPack = new Inventory(Option.HomeCapacity); backPack.AddRange(backPackItemList); var crucibleItemList = ConvertFromJsonList(source.Crucible); var crucible = new Inventory(Option.CrucibleCapacity); crucible.AddRange(crucibleItemList); var stages = ConvertFromJsonList(source.Stages); var heroSave = new HeroSave( source.Name, heroClass, inventory, equipment, backPack, crucible, stages, source.Properties); return(heroSave); }
public static HeroSaveJson ToJson(HeroSave source) { // Has this hero been altered or played? if (!source.IsDirty) { // This path exists as there are no stages yet and therefore there is not // Current Hero var heroSaveJson = new HeroSaveJson { Name = source.Name, // Lists that need to be preserved Equipment = ConvertToJsonList(source.Equipment.Items), Inventory = ConvertToJsonList(source.Inventory), BackPack = ConvertToJsonList(source.BackPack), Crucible = ConvertToJsonList(source.Crucible), Stages = ConvertToJsonList(source.Stages), // Actor Properties Properties = source.Properties }; heroSaveJson.Properties["Health"] = source.Health; heroSaveJson.Properties["Charge"] = source.Charge; heroSaveJson.Properties["Position"] = source.Position; heroSaveJson.Properties["ExperienceCents"] = source.ExperienceCents; heroSaveJson.Properties["Gold"] = source.Gold; heroSaveJson.Properties["Food"] = source.Food; var warrior = source.HeroClass as Warrior; if (warrior != null) { SaveWarrior(warrior, heroSaveJson); } return(heroSaveJson); } else { throw new NotImplementedException(); } }