Gear.cs 11 KB

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