2
0

CharacterLevelDescription.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //-----------------------------------------------------------------------------
  2. // CharacterLevelDescription.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.ComponentModel.DataAnnotations;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Xml.Linq;
  13. using Microsoft.Xna.Framework.Content;
  14. namespace RolePlaying.Data
  15. {
  16. /// <summary>
  17. /// The requirements and rewards for each level for a character class.
  18. /// </summary>
  19. public class CharacterLevelDescription
  20. {
  21. /// <summary>
  22. /// The amount of additional experience necessary to achieve this level.
  23. /// </summary>
  24. private int experiencePoints;
  25. /// <summary>
  26. /// The amount of additional experience necessary to achieve this level.
  27. /// </summary>
  28. public int ExperiencePoints
  29. {
  30. get { return experiencePoints; }
  31. set { experiencePoints = value; }
  32. }
  33. /// <summary>
  34. /// The content names of the spells given to the character
  35. /// when it reaches this level.
  36. /// </summary>
  37. private List<string> spellContentNames = new List<string>();
  38. /// <summary>
  39. /// The content names of the spells given to the character
  40. /// when it reaches this level.
  41. /// </summary>
  42. public List<string> SpellContentNames
  43. {
  44. get { return spellContentNames; }
  45. set { spellContentNames = value; }
  46. }
  47. /// <summary>
  48. /// Spells given to the character when it reaches this level.
  49. /// </summary>
  50. private List<Spell> spells = new List<Spell>();
  51. /// <summary>
  52. /// Spells given to the character when it reaches this level.
  53. /// </summary>
  54. [ContentSerializerIgnore]
  55. public List<Spell> Spells
  56. {
  57. get { return spells; }
  58. set { spells = value; }
  59. }
  60. /// <summary>
  61. /// Read a CharacterLevelDescription object from the content pipeline.
  62. /// </summary>
  63. public class CharacterLevelDescriptionReader :
  64. ContentTypeReader<CharacterLevelDescription>
  65. {
  66. /// <summary>
  67. /// Read a CharacterLevelDescription object from the content pipeline.
  68. /// </summary>
  69. protected override CharacterLevelDescription Read(ContentReader input,
  70. CharacterLevelDescription existingInstance)
  71. {
  72. CharacterLevelDescription desc = existingInstance;
  73. if (desc == null)
  74. {
  75. desc = new CharacterLevelDescription();
  76. }
  77. desc.ExperiencePoints = input.ReadInt32();
  78. desc.SpellContentNames.AddRange(input.ReadObject<List<string>>());
  79. // load all of the spells immediately
  80. foreach (string spellContentName in desc.SpellContentNames)
  81. {
  82. desc.spells.Add(input.ContentManager.Load<Spell>(Path.Combine("Spells", spellContentName)));
  83. }
  84. return desc;
  85. }
  86. }
  87. internal static CharacterLevelDescription Load(XElement item, ContentManager contentManager)
  88. {
  89. var characterLevelDescription = new CharacterLevelDescription
  90. {
  91. ExperiencePoints = (int?)item.Element("ExperiencePoints") ?? 0,
  92. SpellContentNames = item.Element("SpellContentNames")?
  93. .Elements("Item")
  94. .Select(x =>
  95. {
  96. return (string)x;
  97. })
  98. .ToList() ?? new List<string>(),
  99. };
  100. if (characterLevelDescription.SpellContentNames.Count > 0)
  101. {
  102. // Load the spells for this level
  103. characterLevelDescription.Spells = new List<Spell>();
  104. foreach (var spellContentName in characterLevelDescription.SpellContentNames)
  105. {
  106. var spell = Spell.Load(Path.Combine("Spells", spellContentName), contentManager);
  107. characterLevelDescription.Spells.Add(spell);
  108. }
  109. }
  110. return characterLevelDescription;
  111. }
  112. }
  113. }