FixedCombat.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //-----------------------------------------------------------------------------
  2. // FixedCombat.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using Microsoft.Xna.Framework.Content;
  12. namespace RolePlaying.Data
  13. {
  14. /// <summary>
  15. /// The description of a fixed combat encounter in the world.
  16. /// </summary>
  17. public class FixedCombat : WorldObject
  18. {
  19. /// <summary>
  20. /// The content name and quantities of the monsters in this encounter.
  21. /// </summary>
  22. private List<ContentEntry<Monster>> entries = new List<ContentEntry<Monster>>();
  23. /// <summary>
  24. /// The content name and quantities of the monsters in this encounter.
  25. /// </summary>
  26. public List<ContentEntry<Monster>> Entries
  27. {
  28. get { return entries; }
  29. set { entries = value; }
  30. }
  31. internal static FixedCombat Load(string contentName, ContentManager contentManager)
  32. {
  33. var asset = XmlHelper.GetAssetElementFromXML(contentName);
  34. // Create a new FixedCombat instance and populate it with data from the XML asset
  35. var fixedCombat = new FixedCombat()
  36. {
  37. AssetName = contentName,
  38. Name = (string)asset.Element("Name"),
  39. Entries = asset.Element("Entries")?.Elements("Item")
  40. .Select(entry =>
  41. {
  42. var monsterContentName = (string)entry.Element("ContentName");
  43. var monsterCount = (int?)entry.Element("Count") ?? 1;
  44. var monster = Monster.Load(Path.Combine("Characters", "Monsters", monsterContentName), contentManager);
  45. return new ContentEntry<Monster>
  46. {
  47. ContentName = monsterContentName,
  48. Count = monsterCount,
  49. Content = monster
  50. };
  51. }).ToList(),
  52. };
  53. return fixedCombat;
  54. }
  55. /// <summary>
  56. /// Reads a FixedCombat object from the content pipeline.
  57. /// </summary>
  58. public class FixedCombatReader : ContentTypeReader<FixedCombat>
  59. {
  60. /// <summary>
  61. /// Reads a FixedCombat object from the content pipeline.
  62. /// </summary>
  63. protected override FixedCombat Read(ContentReader input,
  64. FixedCombat existingInstance)
  65. {
  66. FixedCombat fixedCombat = existingInstance;
  67. if (fixedCombat == null)
  68. {
  69. fixedCombat = new FixedCombat();
  70. }
  71. input.ReadRawObject<WorldObject>(fixedCombat as WorldObject);
  72. fixedCombat.Entries.AddRange(
  73. input.ReadObject<List<ContentEntry<Monster>>>());
  74. foreach (ContentEntry<Monster> fixedCombatEntry in fixedCombat.Entries)
  75. {
  76. fixedCombatEntry.Content = input.ContentManager.Load<Monster>(Path.Combine("Characters", "Monsters", fixedCombatEntry.ContentName));
  77. }
  78. return fixedCombat;
  79. }
  80. }
  81. }
  82. }