//----------------------------------------------------------------------------- // FixedCombat.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.IO; using System.Linq; using Microsoft.Xna.Framework.Content; namespace RolePlaying.Data { /// /// The description of a fixed combat encounter in the world. /// public class FixedCombat : WorldObject { /// /// The content name and quantities of the monsters in this encounter. /// private List> entries = new List>(); /// /// The content name and quantities of the monsters in this encounter. /// public List> Entries { get { return entries; } set { entries = value; } } internal static FixedCombat Load(string contentName, ContentManager contentManager) { var asset = XmlHelper.GetAssetElementFromXML(contentName); // Create a new FixedCombat instance and populate it with data from the XML asset var fixedCombat = new FixedCombat() { AssetName = contentName, Name = (string)asset.Element("Name"), Entries = asset.Element("Entries")?.Elements("Item") .Select(entry => { var monsterContentName = (string)entry.Element("ContentName"); var monsterCount = (int?)entry.Element("Count") ?? 1; var monster = Monster.Load(Path.Combine("Characters", "Monsters", monsterContentName), contentManager); return new ContentEntry { ContentName = monsterContentName, Count = monsterCount, Content = monster }; }).ToList(), }; return fixedCombat; } /// /// Reads a FixedCombat object from the content pipeline. /// public class FixedCombatReader : ContentTypeReader { /// /// Reads a FixedCombat object from the content pipeline. /// protected override FixedCombat Read(ContentReader input, FixedCombat existingInstance) { FixedCombat fixedCombat = existingInstance; if (fixedCombat == null) { fixedCombat = new FixedCombat(); } input.ReadRawObject(fixedCombat as WorldObject); fixedCombat.Entries.AddRange( input.ReadObject>>()); foreach (ContentEntry fixedCombatEntry in fixedCombat.Entries) { fixedCombatEntry.Content = input.ContentManager.Load(Path.Combine("Characters", "Monsters", fixedCombatEntry.ContentName)); } return fixedCombat; } } } }