// Load a monster manifest from a file of definitions. public static MonsterManifest LoadFromFile(string filepath) { using (StreamReader sr = new StreamReader(filepath)) { try { // Load contents into an array of serialized monster data. string contents = sr.ReadToEnd(); var serializedMonsterDataObject = JsonConvert.DeserializeObject <SerializableMonsterDefinitionList>(contents); var serializedMonsterData = serializedMonsterDataObject.monster_definitions; // Copy serialized data into dictionary of monster definitions. var monsterDefinitions = new MonsterDefinition[serializedMonsterData.Length]; for (int i = 0; i < monsterDefinitions.Length; ++i) { var monster = serializedMonsterData[i]; monsterDefinitions[i] = new MonsterDefinition(i) { Name = monster.name, ChallengeRating = monster.challenge_rating, }; } // Return manifest object with definitions assigned. return(new MonsterManifest { MonsterDefinitions = monsterDefinitions, }); } catch { return(null); } } }
// Called when the monsters add button is clicked. private void MonstersAddButton_Click(object sender, RoutedEventArgs e) { Button button = sender as Button; MonsterDefinition monster = button.DataContext as MonsterDefinition; encounter.Monsters.Add(monster); }