Item.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //-----------------------------------------------------------------------------
  2. // Item.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.Graphics;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. namespace RolePlaying.Data
  14. {
  15. /// <summary>
  16. /// A usable piece of gear that has a spell-like effect.
  17. /// </summary>
  18. public class Item : Gear
  19. {
  20. /// <summary>
  21. /// Flags that specify when an item may be used.
  22. /// </summary>
  23. public enum ItemUsage
  24. {
  25. Combat = 1,
  26. NonCombat = 2,
  27. };
  28. /// <summary>
  29. /// Description of when the item may be used.
  30. /// </summary>
  31. /// <remarks>Defaults to "either", with both values.</remarks>
  32. private ItemUsage usage = ItemUsage.Combat | ItemUsage.NonCombat;
  33. /// <summary>
  34. /// Description of when the item may be used.
  35. /// </summary>
  36. /// <remarks>Defaults to "either", with both values.</remarks>
  37. [ContentSerializer(Optional = true)]
  38. public ItemUsage Usage
  39. {
  40. get { return usage; }
  41. set { usage = value; }
  42. }
  43. /// <summary>
  44. /// Builds and returns a string describing the power of this item.
  45. /// </summary>
  46. public override string GetPowerText()
  47. {
  48. return TargetEffectRange.GetModifierString();
  49. }
  50. /// <summary>
  51. /// If true, the statistics change are used as a debuff (subtracted).
  52. /// Otherwise, the statistics change is used as a buff (added).
  53. /// </summary>
  54. private bool isOffensive;
  55. /// <summary>
  56. /// If true, the statistics change are used as a debuff (subtracted).
  57. /// Otherwise, the statistics change is used as a buff (added).
  58. /// </summary>
  59. public bool IsOffensive
  60. {
  61. get { return isOffensive; }
  62. set { isOffensive = value; }
  63. }
  64. /// <summary>
  65. /// The duration of the effect of this item on its target, in rounds.
  66. /// </summary>
  67. /// <remarks>
  68. /// If the duration is zero, then the effects last for the rest of the battle.
  69. /// </remarks>
  70. private int targetDuration;
  71. /// <summary>
  72. /// The duration of the effect of this item on its target, in rounds.
  73. /// </summary>
  74. /// <remarks>
  75. /// If the duration is zero, then the effects last for the rest of the battle.
  76. /// </remarks>
  77. public int TargetDuration
  78. {
  79. get { return targetDuration; }
  80. set { targetDuration = value; }
  81. }
  82. /// <summary>
  83. /// The range of statistics effects of this item on its target.
  84. /// </summary>
  85. /// <remarks>
  86. /// This is a debuff if IsOffensive is true, otherwise it's a buff.
  87. /// </remarks>
  88. private StatisticsRange targetEffectRange = new StatisticsRange();
  89. /// <summary>
  90. /// The range of statistics effects of this item on its target.
  91. /// </summary>
  92. /// <remarks>
  93. /// This is a debuff if IsOffensive is true, otherwise it's a buff.
  94. /// </remarks>
  95. public StatisticsRange TargetEffectRange
  96. {
  97. get { return targetEffectRange; }
  98. set { targetEffectRange = value; }
  99. }
  100. /// <summary>
  101. /// The number of simultaneous, adjacent targets affected by this item.
  102. /// </summary>
  103. private int adjacentTargets;
  104. /// <summary>
  105. /// The number of simultaneous, adjacent targets affected by this item.
  106. /// </summary>
  107. public int AdjacentTargets
  108. {
  109. get { return adjacentTargets; }
  110. set { adjacentTargets = value; }
  111. }
  112. /// <summary>
  113. /// The name of the sound effect cue played when the item is used.
  114. /// </summary>
  115. private string usingCueName;
  116. /// <summary>
  117. /// The name of the sound effect cue played when the item is used.
  118. /// </summary>
  119. public string UsingCueName
  120. {
  121. get { return usingCueName; }
  122. set { usingCueName = value; }
  123. }
  124. /// <summary>
  125. /// The name of the sound effect cue played when the item effect is traveling.
  126. /// </summary>
  127. private string travelingCueName;
  128. /// <summary>
  129. /// The name of the sound effect cue played when the item effect is traveling.
  130. /// </summary>
  131. public string TravelingCueName
  132. {
  133. get { return travelingCueName; }
  134. set { travelingCueName = value; }
  135. }
  136. /// <summary>
  137. /// The name of the sound effect cue played when the item affects its target.
  138. /// </summary>
  139. private string impactCueName;
  140. /// <summary>
  141. /// The name of the sound effect cue played when the item affects its target.
  142. /// </summary>
  143. public string ImpactCueName
  144. {
  145. get { return impactCueName; }
  146. set { impactCueName = value; }
  147. }
  148. /// <summary>
  149. /// The name of the sound effect cue played when the item effect is blocked.
  150. /// </summary>
  151. private string blockCueName;
  152. /// <summary>
  153. /// The name of the sound effect cue played when the item effect is blocked.
  154. /// </summary>
  155. public string BlockCueName
  156. {
  157. get { return blockCueName; }
  158. set { blockCueName = value; }
  159. }
  160. /// <summary>
  161. /// An animating sprite used when this item is used.
  162. /// </summary>
  163. /// <remarks>
  164. /// This is optional. If it is null, then a Using or Creating animation
  165. /// in SpellSprite is used.
  166. /// </remarks>
  167. private AnimatingSprite creationSprite;
  168. /// <summary>
  169. /// An animating sprite used when this item is used.
  170. /// </summary>
  171. /// <remarks>
  172. /// This is optional. If it is null, then a Using or Creating animation
  173. /// in SpellSprite is used.
  174. /// </remarks>
  175. [ContentSerializer(Optional = true)]
  176. public AnimatingSprite CreationSprite
  177. {
  178. get { return creationSprite; }
  179. set { creationSprite = value; }
  180. }
  181. /// <summary>
  182. /// The animating sprite used when this item is in action.
  183. /// </summary>
  184. private AnimatingSprite spellSprite;
  185. /// <summary>
  186. /// The animating sprite used when this item is in action.
  187. /// </summary>
  188. public AnimatingSprite SpellSprite
  189. {
  190. get { return spellSprite; }
  191. set { spellSprite = value; }
  192. }
  193. /// <summary>
  194. /// The overlay sprite for this item.
  195. /// </summary>
  196. private AnimatingSprite overlay;
  197. /// <summary>
  198. /// The overlay sprite for this item.
  199. /// </summary>
  200. public AnimatingSprite Overlay
  201. {
  202. get { return overlay; }
  203. set { overlay = value; }
  204. }
  205. internal static Item Load(string itemAssetName, ContentManager contentManager)
  206. {
  207. var itemAsset = XmlHelper.GetAssetElementFromXML(itemAssetName);
  208. var item = new Item()
  209. {
  210. AssetName = itemAssetName,
  211. Name = (string)itemAsset.Element("Name"),
  212. Description = (string)itemAsset.Element("Description"),
  213. GoldValue = (int)itemAsset.Element("GoldValue"),
  214. IconTextureName = (string)itemAsset.Element("IconTextureName"),
  215. IconTexture = contentManager.Load<Texture2D>(Path.Combine("Textures", "Gear", (string)itemAsset.Element("IconTextureName"))),
  216. MinimumCharacterLevel = int.Parse(itemAsset.Element("MinimumCharacterLevel").Value),
  217. Usage = itemAsset.Element("Usage") != null ? (ItemUsage)Enum.Parse(typeof(ItemUsage), (string)itemAsset.Element("Usage")) : default,
  218. IsOffensive = bool.Parse(itemAsset.Element("IsOffensive").Value),
  219. TargetDuration = int.Parse(itemAsset.Element("TargetDuration").Value),
  220. AdjacentTargets = int.Parse(itemAsset.Element("AdjacentTargets").Value),
  221. UsingCueName = (string)itemAsset.Element("UsingCueName"),
  222. TravelingCueName = (string)itemAsset.Element("TravelingCueName"),
  223. ImpactCueName = (string)itemAsset.Element("ImpactCueName"),
  224. BlockCueName = (string)itemAsset.Element("BlockCueName"),
  225. CreationSprite = itemAsset.Element("CreatingSprite") != null ? AnimatingSprite.Load(itemAsset.Element("CreatingSprite"), contentManager) : null,
  226. SpellSprite = itemAsset.Element("SpellSprite") != null ? AnimatingSprite.Load(itemAsset.Element("SpellSprite"), contentManager) : null,
  227. Overlay = itemAsset.Element("Overlay") != null ? AnimatingSprite.Load(itemAsset.Element("Overlay"), contentManager) : null,
  228. };
  229. return item;
  230. }
  231. /// <summary>
  232. /// Read an Item object from the content pipeline
  233. /// </summary>
  234. public class ItemReader : ContentTypeReader<Item>
  235. {
  236. protected override Item Read(ContentReader input, Item existingInstance)
  237. {
  238. Item item = existingInstance;
  239. if (item == null)
  240. {
  241. item = new Item();
  242. }
  243. // read gear settings
  244. input.ReadRawObject<Gear>(item as Gear);
  245. // read item settings
  246. item.Usage = (ItemUsage)input.ReadInt32();
  247. item.IsOffensive = input.ReadBoolean();
  248. item.TargetDuration = input.ReadInt32();
  249. item.TargetEffectRange = input.ReadObject<StatisticsRange>();
  250. item.AdjacentTargets = input.ReadInt32();
  251. item.UsingCueName = input.ReadString();
  252. item.TravelingCueName = input.ReadString();
  253. item.ImpactCueName = input.ReadString();
  254. item.BlockCueName = input.ReadString();
  255. item.CreationSprite = input.ReadObject<AnimatingSprite>();
  256. item.CreationSprite.SourceOffset = new Vector2(
  257. item.CreationSprite.FrameDimensions.X / 2,
  258. item.CreationSprite.FrameDimensions.Y);
  259. item.SpellSprite = input.ReadObject<AnimatingSprite>();
  260. item.SpellSprite.SourceOffset = new Vector2(
  261. item.SpellSprite.FrameDimensions.X / 2,
  262. item.SpellSprite.FrameDimensions.Y);
  263. item.Overlay = input.ReadObject<AnimatingSprite>();
  264. item.Overlay.SourceOffset = new Vector2(
  265. item.Overlay.FrameDimensions.X / 2,
  266. item.Overlay.FrameDimensions.Y);
  267. return item;
  268. }
  269. }
  270. }
  271. }