Store.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //-----------------------------------------------------------------------------
  2. // Store.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. using Microsoft.Xna.Framework.Graphics;
  13. namespace RolePlaying.Data
  14. {
  15. /// <summary>
  16. /// A gear store, where the party can buy and sell gear, organized into categories.
  17. /// </summary>
  18. public class Store : WorldObject
  19. {
  20. /// <summary>
  21. /// A purchasing multiplier applied to the price of all gear.
  22. /// </summary>
  23. private float buyMultiplier;
  24. /// <summary>
  25. /// A purchasing multiplier applied to the price of all gear.
  26. /// </summary>
  27. public float BuyMultiplier
  28. {
  29. get { return buyMultiplier; }
  30. set { buyMultiplier = value; }
  31. }
  32. /// <summary>
  33. /// A sell-back multiplier applied to the price of all gear.
  34. /// </summary>
  35. private float sellMultiplier;
  36. /// <summary>
  37. /// A sell-back multiplier applied to the price of all gear.
  38. /// </summary>
  39. public float SellMultiplier
  40. {
  41. get { return sellMultiplier; }
  42. set { sellMultiplier = value; }
  43. }
  44. /// <summary>
  45. /// The categories of gear in this store.
  46. /// </summary>
  47. private List<StoreCategory> storeCategories = new List<StoreCategory>();
  48. /// <summary>
  49. /// The categories of gear in this store.
  50. /// </summary>
  51. public List<StoreCategory> StoreCategories
  52. {
  53. get { return storeCategories; }
  54. set { storeCategories = value; }
  55. }
  56. /// <summary>
  57. /// The message shown when the party enters the store.
  58. /// </summary>
  59. private string welcomeMessage;
  60. /// <summary>
  61. /// The message shown when the party enters the store.
  62. /// </summary>
  63. public string WelcomeMessage
  64. {
  65. get { return welcomeMessage; }
  66. set { welcomeMessage = value; }
  67. }
  68. /// <summary>
  69. /// The content path and name of the texture for the shopkeeper.
  70. /// </summary>
  71. private string shopkeeperTextureName;
  72. /// <summary>
  73. /// The content path and name of the texture for the shopkeeper.
  74. /// </summary>
  75. public string ShopkeeperTextureName
  76. {
  77. get { return shopkeeperTextureName; }
  78. set { shopkeeperTextureName = value; }
  79. }
  80. /// <summary>
  81. /// The texture for the shopkeeper.
  82. /// </summary>
  83. private Texture2D shopkeeperTexture;
  84. /// <summary>
  85. /// The texture for the shopkeeper.
  86. /// </summary>
  87. [ContentSerializerIgnore]
  88. public Texture2D ShopkeeperTexture
  89. {
  90. get { return shopkeeperTexture; }
  91. set { shopkeeperTexture = value; }
  92. }
  93. internal static Store Load(string contentName, ContentManager contentManager)
  94. {
  95. var asset = XmlHelper.GetAssetElementFromXML(contentName);
  96. var store = new Store
  97. {
  98. AssetName = contentName,
  99. Name = asset.Element("Name").Value,
  100. BuyMultiplier = float.Parse(asset.Element("BuyMultiplier").Value),
  101. SellMultiplier = float.Parse(asset.Element("SellMultiplier").Value),
  102. WelcomeMessage = asset.Element("WelcomeMessage").Value,
  103. ShopkeeperTextureName = asset.Element("ShopkeeperTextureName").Value,
  104. ShopkeeperTexture = contentManager.Load<Texture2D>(Path.Combine("Textures", "Characters", "Portraits", asset.Element("ShopkeeperTextureName").Value)),
  105. StoreCategories = asset.Element("StoreCategories")
  106. .Elements("Item")
  107. .Select(storeCategory => StoreCategory.Load(storeCategory, contentManager)).ToList(),
  108. };
  109. return store;
  110. }
  111. /// <summary>
  112. /// Reads an Store object from the content pipeline.
  113. /// </summary>
  114. public class StoreReader : ContentTypeReader<Store>
  115. {
  116. protected override Store Read(ContentReader input, Store existingInstance)
  117. {
  118. Store store = existingInstance;
  119. if (store == null)
  120. {
  121. store = new Store();
  122. }
  123. input.ReadRawObject<WorldObject>(store as WorldObject);
  124. store.BuyMultiplier = input.ReadSingle();
  125. store.SellMultiplier = input.ReadSingle();
  126. store.StoreCategories.AddRange(input.ReadObject<List<StoreCategory>>());
  127. store.WelcomeMessage = input.ReadString();
  128. store.ShopkeeperTextureName = input.ReadString();
  129. store.shopkeeperTexture = input.ContentManager.Load<Texture2D>(Path.Combine("Textures", "Characters", "Portraits", store.ShopkeeperTextureName));
  130. return store;
  131. }
  132. }
  133. }
  134. }