Gear.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Gear.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using System.Diagnostics;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Content;
  16. using Microsoft.Xna.Framework.Graphics;
  17. #endregion
  18. namespace RolePlayingGameData
  19. {
  20. /// <summary>
  21. /// An inventory element - items, equipment, etc.
  22. /// </summary>
  23. #if !XBOX
  24. [DebuggerDisplay("Name = {name}")]
  25. #endif
  26. public abstract class Gear : ContentObject
  27. {
  28. #region Description Data
  29. /// <summary>
  30. /// The name of this gear.
  31. /// </summary>
  32. private string name;
  33. /// <summary>
  34. /// The name of this gear.
  35. /// </summary>
  36. public string Name
  37. {
  38. get { return name; }
  39. set { name = value; }
  40. }
  41. /// <summary>
  42. /// The long description of this gear.
  43. /// </summary>
  44. private string description;
  45. /// <summary>
  46. /// The long description of this gear.
  47. /// </summary>
  48. public string Description
  49. {
  50. get { return description; }
  51. set { description = value; }
  52. }
  53. /// <summary>
  54. /// Builds and returns a string describing the power of this gear.
  55. /// </summary>
  56. public virtual string GetPowerText()
  57. {
  58. return String.Empty;
  59. }
  60. #endregion
  61. #region Value Data
  62. /// <summary>
  63. /// The value of this gear.
  64. /// </summary>
  65. /// <remarks>If the value is less than zero, it cannot be sold.</remarks>
  66. private int goldValue;
  67. /// <summary>
  68. /// The value of this gear.
  69. /// </summary>
  70. /// <remarks>If the value is less than zero, it cannot be sold.</remarks>
  71. public int GoldValue
  72. {
  73. get { return goldValue; }
  74. set { goldValue = value; }
  75. }
  76. /// <summary>
  77. /// If true, the gear can be dropped. If false, it cannot ever be dropped.
  78. /// </summary>
  79. private bool isDroppable;
  80. /// <summary>
  81. /// If true, the gear can be dropped. If false, it cannot ever be dropped.
  82. /// </summary>
  83. public bool IsDroppable
  84. {
  85. get { return isDroppable; }
  86. set { isDroppable = value; }
  87. }
  88. #endregion
  89. #region Restrictions
  90. /// <summary>
  91. /// The minimum character level required to equip or use this gear.
  92. /// </summary>
  93. private int minimumCharacterLevel;
  94. /// <summary>
  95. /// The minimum character level required to equip or use this gear.
  96. /// </summary>
  97. public int MinimumCharacterLevel
  98. {
  99. get { return minimumCharacterLevel; }
  100. set { minimumCharacterLevel = value; }
  101. }
  102. /// <summary>
  103. /// The list of the names of all supported classes.
  104. /// </summary>
  105. /// <remarks>Class names are compared case-insensitive.</remarks>
  106. private List<string> supportedClasses = new List<string>();
  107. /// <summary>
  108. /// The list of the names of all supported classes.
  109. /// </summary>
  110. /// <remarks>Class names are compared case-insensitive.</remarks>
  111. public List<string> SupportedClasses
  112. {
  113. get { return supportedClasses; }
  114. }
  115. /// <summary>
  116. /// Check the restrictions on this object against the provided character.
  117. /// </summary>
  118. /// <returns>True if the gear could be used, false otherwise.</returns>
  119. public virtual bool CheckRestrictions(FightingCharacter fightingCharacter)
  120. {
  121. if (fightingCharacter == null)
  122. {
  123. throw new ArgumentNullException("fightingCharacter");
  124. }
  125. return ((fightingCharacter.CharacterLevel >= MinimumCharacterLevel) &&
  126. ((SupportedClasses.Count <= 0) ||
  127. SupportedClasses.Contains(fightingCharacter.CharacterClass.Name)));
  128. }
  129. /// <summary>
  130. /// Builds a string describing the restrictions on this piece of gear.
  131. /// </summary>
  132. public virtual string GetRestrictionsText()
  133. {
  134. StringBuilder sb = new StringBuilder();
  135. // add the minimum character level, if any
  136. if (MinimumCharacterLevel > 0)
  137. {
  138. sb.Append("Level - ");
  139. sb.Append(MinimumCharacterLevel.ToString());
  140. sb.Append("; ");
  141. }
  142. // add the classes
  143. if (SupportedClasses.Count > 0)
  144. {
  145. sb.Append("Class - ");
  146. bool firstClass = true;
  147. foreach (string className in SupportedClasses)
  148. {
  149. if (firstClass)
  150. {
  151. firstClass = false;
  152. }
  153. else
  154. {
  155. sb.Append(",");
  156. }
  157. sb.Append(className);
  158. }
  159. }
  160. return sb.ToString();
  161. }
  162. #endregion
  163. #region Graphics Data
  164. /// <summary>
  165. /// The content path and name of the icon for this gear.
  166. /// </summary>
  167. private string iconTextureName;
  168. /// <summary>
  169. /// The content path and name of the icon for this gear.
  170. /// </summary>
  171. public string IconTextureName
  172. {
  173. get { return iconTextureName; }
  174. set { iconTextureName = value; }
  175. }
  176. /// <summary>
  177. /// The icon texture for this gear.
  178. /// </summary>
  179. private Texture2D iconTexture;
  180. /// <summary>
  181. /// The icon texture for this gear.
  182. /// </summary>
  183. [ContentSerializerIgnore]
  184. public Texture2D IconTexture
  185. {
  186. get { return iconTexture; }
  187. }
  188. #endregion
  189. #region Drawing Methods
  190. /// <summary>
  191. /// Draw the icon for this gear.
  192. /// </summary>
  193. /// <param name="spriteBatch">The SpriteBatch object to use when drawing.</param>
  194. /// <param name="position">The position of the icon on the screen.</param>
  195. public virtual void DrawIcon(SpriteBatch spriteBatch, Vector2 position)
  196. {
  197. // check the parameters
  198. if (spriteBatch == null)
  199. {
  200. throw new ArgumentNullException("spriteBatch");
  201. }
  202. // draw the icon, if we there is a texture for it
  203. if (iconTexture != null)
  204. {
  205. spriteBatch.Draw(iconTexture, position, Color.White);
  206. }
  207. }
  208. /// <summary>
  209. /// Draw the description for this gear in the space provided.
  210. /// </summary>
  211. /// <param name="spriteBatch">The SpriteBatch object to use when drawing.</param>
  212. /// <param name="spriteFont">The font that the text is drawn with.</param>
  213. /// <param name="color">The color of the text.</param>
  214. /// <param name="position">The position of the text on the screen.</param>
  215. /// <param name="maximumCharactersPerLine">
  216. /// The maximum length of a single line of text.
  217. /// </param>
  218. /// <param name="maximumLines">The maximum number of lines to draw.</param>
  219. public virtual void DrawDescription(SpriteBatch spriteBatch,
  220. SpriteFont spriteFont, Color color, Vector2 position,
  221. int maximumCharactersPerLine, int maximumLines)
  222. {
  223. // check the parameters
  224. if (spriteBatch == null)
  225. {
  226. throw new ArgumentNullException("spriteBatch");
  227. }
  228. if (spriteFont == null)
  229. {
  230. throw new ArgumentNullException("spriteFont");
  231. }
  232. if (maximumLines <= 0)
  233. {
  234. throw new ArgumentOutOfRangeException("maximumLines");
  235. }
  236. if (maximumCharactersPerLine <= 0)
  237. {
  238. throw new ArgumentOutOfRangeException("maximumCharactersPerLine");
  239. }
  240. // if the string is trivial, then this is really easy
  241. if (String.IsNullOrEmpty(description))
  242. {
  243. return;
  244. }
  245. // if the text is short enough to fit on one line, then this is still easy
  246. if (description.Length < maximumCharactersPerLine)
  247. {
  248. spriteBatch.DrawString(spriteFont, description, position, color);
  249. return;
  250. }
  251. // construct a new string with carriage returns
  252. StringBuilder stringBuilder = new StringBuilder(description);
  253. int currentLine = 0;
  254. int newLineIndex = 0;
  255. while (((description.Length - newLineIndex) > maximumCharactersPerLine) &&
  256. (currentLine < maximumLines))
  257. {
  258. description.IndexOf(' ', 0);
  259. int nextIndex = newLineIndex;
  260. while (nextIndex < maximumCharactersPerLine)
  261. {
  262. newLineIndex = nextIndex;
  263. nextIndex = description.IndexOf(' ', newLineIndex + 1);
  264. }
  265. stringBuilder.Replace(' ', '\n', newLineIndex, 1);
  266. currentLine++;
  267. }
  268. // draw the string
  269. spriteBatch.DrawString(spriteFont, stringBuilder.ToString(),
  270. position, color);
  271. }
  272. #endregion
  273. #region Content Type Reader
  274. /// <summary>
  275. /// Reads a Gear object from the content pipeline.
  276. /// </summary>
  277. public class GearReader : ContentTypeReader<Gear>
  278. {
  279. /// <summary>
  280. /// Reads a Gear object from the content pipeline.
  281. /// </summary>
  282. protected override Gear Read(ContentReader input, Gear existingInstance)
  283. {
  284. Gear gear = existingInstance;
  285. if (gear == null)
  286. {
  287. throw new ArgumentException("Unable to create new Gear objects.");
  288. }
  289. gear.AssetName = input.AssetName;
  290. // read gear settings
  291. gear.Name = input.ReadString();
  292. gear.Description = input.ReadString();
  293. gear.GoldValue = input.ReadInt32();
  294. gear.IsDroppable = input.ReadBoolean();
  295. gear.MinimumCharacterLevel = input.ReadInt32();
  296. gear.SupportedClasses.AddRange(input.ReadObject<List<string>>());
  297. gear.IconTextureName = input.ReadString();
  298. gear.iconTexture = input.ContentManager.Load<Texture2D>(
  299. System.IO.Path.Combine(@"Textures\Gear", gear.IconTextureName));
  300. return gear;
  301. }
  302. }
  303. #endregion
  304. }
  305. }