Gear.cs 11 KB

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