EquipmentScreen.cs 8.7 KB

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