2
0

EquipmentScreen.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // EquipmentScreen.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.Collections.ObjectModel;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Content;
  16. using RolePlayingGameData;
  17. #endregion
  18. namespace RolePlaying
  19. {
  20. /// <summary>
  21. /// Lists the player's equipped gear, and allows the user to unequip them.
  22. /// </summary>
  23. class EquipmentScreen : ListScreen<Equipment>
  24. {
  25. #region Columns
  26. protected string nameColumnText = "Name";
  27. private const int nameColumnInterval = 80;
  28. protected string powerColumnText = "Power (min, max)";
  29. private const int powerColumnInterval = 270;
  30. protected string slotColumnText = "Slot";
  31. private const int slotColumnInterval = 400;
  32. #endregion
  33. #region Data Access
  34. /// <summary>
  35. /// The FightingCharacter object whose equipment is displayed.
  36. /// </summary>
  37. private FightingCharacter fightingCharacter;
  38. /// <summary>
  39. /// Get the list that this screen displays.
  40. /// </summary>
  41. public override ReadOnlyCollection<Equipment> GetDataList()
  42. {
  43. return fightingCharacter.EquippedEquipment.AsReadOnly();
  44. }
  45. #endregion
  46. #region Initialization
  47. /// <summary>
  48. /// Creates a new EquipmentScreen object for the given player.
  49. /// </summary>
  50. public EquipmentScreen(FightingCharacter fightingCharacter)
  51. : base()
  52. {
  53. // check the parameter
  54. if (fightingCharacter == null)
  55. {
  56. throw new ArgumentNullException("fightingCharacter");
  57. }
  58. this.fightingCharacter = fightingCharacter;
  59. // sort the player's equipment
  60. this.fightingCharacter.EquippedEquipment.Sort(
  61. delegate(Equipment equipment1, Equipment equipment2)
  62. {
  63. // handle null values
  64. if (equipment1 == null)
  65. {
  66. return (equipment2 == null ? 0 : 1);
  67. }
  68. else if (equipment2 == null)
  69. {
  70. return -1;
  71. }
  72. // handle weapons - they're always first in the list
  73. if (equipment1 is Weapon)
  74. {
  75. return (equipment2 is Weapon ?
  76. equipment1.Name.CompareTo(equipment2.Name) : -1);
  77. }
  78. else if (equipment2 is Weapon)
  79. {
  80. return 1;
  81. }
  82. // compare armor slots
  83. Armor armor1 = equipment1 as Armor;
  84. Armor armor2 = equipment2 as Armor;
  85. if ((armor1 != null) && (armor2 != null))
  86. {
  87. return armor1.Slot.CompareTo(armor2.Slot);
  88. }
  89. return 0;
  90. });
  91. // configure the menu text
  92. titleText = "Equipped Gear";
  93. selectButtonText = String.Empty;
  94. backButtonText = "Back";
  95. xButtonText = "Unequip";
  96. yButtonText = String.Empty;
  97. leftTriggerText = String.Empty;
  98. rightTriggerText = String.Empty;
  99. }
  100. #endregion
  101. #region Input Handling
  102. /// <summary>
  103. /// Respond to the triggering of the X button (and related key).
  104. /// </summary>
  105. protected override void ButtonXPressed(Equipment entry)
  106. {
  107. // remove the equipment from the player's equipped list
  108. fightingCharacter.Unequip(entry);
  109. // add the equipment back to the party's inventory
  110. Session.Party.AddToInventory(entry, 1);
  111. }
  112. #endregion
  113. #region Drawing
  114. /// <summary>
  115. /// Draw the equipment at the given position in the list.
  116. /// </summary>
  117. /// <param name="contentEntry">The equipment to draw.</param>
  118. /// <param name="position">The position to draw the entry at.</param>
  119. /// <param name="isSelected">If true, this item is selected.</param>
  120. protected override void DrawEntry(Equipment entry, Vector2 position,
  121. bool isSelected)
  122. {
  123. // check the parameter
  124. if (entry == null)
  125. {
  126. throw new ArgumentNullException("entry");
  127. }
  128. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  129. Vector2 drawPosition = position;
  130. // draw the icon
  131. spriteBatch.Draw(entry.IconTexture, drawPosition + iconOffset, Color.White);
  132. // draw the name
  133. Color color = isSelected ? Fonts.HighlightColor : Fonts.DisplayColor;
  134. drawPosition.Y += listLineSpacing / 4;
  135. drawPosition.X += nameColumnInterval;
  136. spriteBatch.DrawString(Fonts.GearInfoFont, entry.Name, drawPosition, color);
  137. // draw the power
  138. drawPosition.X += powerColumnInterval;
  139. string powerText = entry.GetPowerText();
  140. Vector2 powerTextSize = Fonts.GearInfoFont.MeasureString(powerText);
  141. Vector2 powerPosition = drawPosition;
  142. powerPosition.Y -= (float)Math.Ceiling((powerTextSize.Y - 30f) / 2);
  143. spriteBatch.DrawString(Fonts.GearInfoFont, powerText,
  144. powerPosition, color);
  145. // draw the slot
  146. drawPosition.X += slotColumnInterval;
  147. if (entry is Weapon)
  148. {
  149. spriteBatch.DrawString(Fonts.GearInfoFont, "Weapon",
  150. drawPosition, color);
  151. }
  152. else if (entry is Armor)
  153. {
  154. Armor armor = entry as Armor;
  155. spriteBatch.DrawString(Fonts.GearInfoFont, armor.Slot.ToString(),
  156. drawPosition, color);
  157. }
  158. // turn on or off the unequip button
  159. if (isSelected)
  160. {
  161. xButtonText = "Unequip";
  162. }
  163. }
  164. /// <summary>
  165. /// Draw the description of the selected item.
  166. /// </summary>
  167. protected override void DrawSelectedDescription(Equipment entry)
  168. {
  169. // check the parameter
  170. if (entry == null)
  171. {
  172. throw new ArgumentNullException("entry");
  173. }
  174. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  175. Vector2 position = descriptionTextPosition;
  176. // draw the description
  177. // -- it's up to the content owner to fit the description
  178. string text = entry.Description;
  179. if (!String.IsNullOrEmpty(text))
  180. {
  181. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  182. Fonts.DescriptionColor);
  183. position.Y += Fonts.DescriptionFont.LineSpacing;
  184. }
  185. // draw the modifiers
  186. text = entry.OwnerBuffStatistics.GetModifierString();
  187. if (!String.IsNullOrEmpty(text))
  188. {
  189. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  190. Fonts.DescriptionColor);
  191. position.Y += Fonts.DescriptionFont.LineSpacing;
  192. }
  193. // draw the restrictions
  194. text = entry.GetRestrictionsText();
  195. if (!String.IsNullOrEmpty(text))
  196. {
  197. spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
  198. Fonts.DescriptionColor);
  199. position.Y += Fonts.DescriptionFont.LineSpacing;
  200. }
  201. }
  202. /// <summary>
  203. /// Draw the column headers above the list.
  204. /// </summary>
  205. protected override void DrawColumnHeaders()
  206. {
  207. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  208. Vector2 position = listEntryStartPosition;
  209. position.X += nameColumnInterval;
  210. if (!String.IsNullOrEmpty(nameColumnText))
  211. {
  212. spriteBatch.DrawString(Fonts.CaptionFont, nameColumnText, position,
  213. Fonts.CaptionColor);
  214. }
  215. position.X += powerColumnInterval;
  216. if (!String.IsNullOrEmpty(powerColumnText))
  217. {
  218. spriteBatch.DrawString(Fonts.CaptionFont, powerColumnText, position,
  219. Fonts.CaptionColor);
  220. }
  221. position.X += slotColumnInterval;
  222. if (!String.IsNullOrEmpty(slotColumnText))
  223. {
  224. spriteBatch.DrawString(Fonts.CaptionFont, slotColumnText, position,
  225. Fonts.CaptionColor);
  226. }
  227. }
  228. #endregion
  229. }
  230. }