StoreCategory.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //-----------------------------------------------------------------------------
  2. // StoreCategory.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Xml.Linq;
  14. namespace RolePlaying.Data
  15. {
  16. /// <summary>
  17. /// A category of gear for sale in a store.
  18. /// </summary>
  19. public class StoreCategory
  20. {
  21. /// <summary>
  22. /// The display name of this store category.
  23. /// </summary>
  24. private string name;
  25. /// <summary>
  26. /// The display name of this store category.
  27. /// </summary>
  28. public string Name
  29. {
  30. get { return name; }
  31. set { name = value; }
  32. }
  33. /// <summary>
  34. /// The content names for the gear available in this category.
  35. /// </summary>
  36. private List<string> availableContentNames = new List<string>();
  37. /// <summary>
  38. /// The content names for the gear available in this category.
  39. /// </summary>
  40. public List<string> AvailableContentNames
  41. {
  42. get { return availableContentNames; }
  43. set { availableContentNames = value; }
  44. }
  45. /// <summary>
  46. /// The gear available in this category.
  47. /// </summary>
  48. private List<Gear> availableGear = new List<Gear>();
  49. /// <summary>
  50. /// The gear available in this category.
  51. /// </summary>
  52. [ContentSerializerIgnore]
  53. public List<Gear> AvailableGear
  54. {
  55. get { return availableGear; }
  56. set { availableGear = value; }
  57. }
  58. /// <summary>
  59. /// Reads a StoreCategory object from the content pipeline.
  60. /// </summary>
  61. public class StoreCategoryReader : ContentTypeReader<StoreCategory>
  62. {
  63. /// <summary>
  64. /// Reads a StoreCategory object from the content pipeline.
  65. /// </summary>
  66. protected override StoreCategory Read(ContentReader input,
  67. StoreCategory existingInstance)
  68. {
  69. StoreCategory storeCategory = existingInstance;
  70. if (storeCategory == null)
  71. {
  72. storeCategory = new StoreCategory();
  73. }
  74. storeCategory.Name = input.ReadString();
  75. storeCategory.AvailableContentNames.AddRange(
  76. input.ReadObject<List<string>>());
  77. // populate the gear list based on the content names
  78. foreach (string gearName in storeCategory.AvailableContentNames)
  79. {
  80. storeCategory.AvailableGear.Add(input.ContentManager.Load<Gear>(Path.Combine("Gear", gearName)));
  81. }
  82. return storeCategory;
  83. }
  84. }
  85. internal static StoreCategory Load(XElement storeCategoryElement, ContentManager contentManager)
  86. {
  87. var storeCategory = new StoreCategory
  88. {
  89. Name = storeCategoryElement.Element("Name").Value,
  90. AvailableContentNames = storeCategoryElement.Element("AvailableContentNames")
  91. .Elements("Item")
  92. .Select(contentNameElement => contentNameElement.Value)
  93. .ToList(),
  94. };
  95. foreach (string gearName in storeCategory.AvailableContentNames)
  96. {
  97. var gearAsset = XmlHelper.GetAssetElementFromXML(System.IO.Path.Combine("Gear", gearName));
  98. var gear = new Item
  99. {
  100. AssetName = gearName,
  101. Name = gearAsset.Element("Name").Value,
  102. Description = gearAsset.Element("Description").Value,
  103. GoldValue = int.Parse(gearAsset.Element("GoldValue").Value),
  104. IsDroppable = bool.Parse(gearAsset.Element("IsDroppable").Value),
  105. IsOffensive = bool.Parse(gearAsset.Element("IsOffensive").Value),
  106. MinimumCharacterLevel = int.Parse(gearAsset.Element("MinimumCharacterLevel").Value),
  107. IconTextureName = gearAsset.Element("IconTextureName").Value,
  108. IconTexture = contentManager.Load<Texture2D>(Path.Combine("Textures", "Gear", gearAsset.Element("IconTextureName").Value)),
  109. TargetDuration = int.Parse(gearAsset.Element("TargetDuration").Value),
  110. AdjacentTargets = int.Parse(gearAsset.Element("AdjacentTargets").Value),
  111. UsingCueName = gearAsset.Element("UsingCueName").Value,
  112. ImpactCueName = gearAsset.Element("ImpactCueName").Value,
  113. BlockCueName = gearAsset.Element("BlockCueName").Value,
  114. };
  115. // Load other properties of Gear as needed
  116. storeCategory.AvailableGear.Add(gear);
  117. }
  118. return storeCategory;
  119. }
  120. }
  121. }