StoreScreen.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. //-----------------------------------------------------------------------------
  2. // StoreScreen.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 Microsoft.Xna.Framework;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using Microsoft.Xna.Framework.Content;
  12. using RolePlaying.Data;
  13. namespace RolePlaying
  14. {
  15. /// <summary>
  16. /// Draws the options available in a store - typically to buy or sell gear.
  17. /// </summary>
  18. class StoreScreen : GameScreen
  19. {
  20. private Store store = null;
  21. private Texture2D shopDrawScreen;
  22. private Texture2D selectButton;
  23. private Texture2D backButton;
  24. private Texture2D highlightItem;
  25. private Texture2D selectionArrow;
  26. private Texture2D conversationStrip;
  27. private Texture2D plankTexture;
  28. private Texture2D fadeTexture;
  29. private Texture2D goldIcon;
  30. private readonly Vector2 textPosition = new Vector2(620, 250);
  31. private readonly Vector2 backButtonPosition = new Vector2(80, 640);
  32. private readonly Vector2 selectButtonPosition = new Vector2(1150, 640);
  33. private readonly Vector2 partyGoldPosition = new Vector2(565, 648);
  34. private readonly Vector2 shopKeeperPosition = new Vector2(290, 370);
  35. private readonly Vector2 welcomeMessagePosition = new Vector2(470, 460);
  36. private readonly Vector2 conversationStripPosition = new Vector2(240, 405);
  37. private readonly Vector2 goldIconPosition = new Vector2(490, 640);
  38. private readonly Vector2 highlightItemOffset = new Vector2(400, 20);
  39. private readonly Vector2 selectionArrowOffset = new Vector2(100, 16);
  40. private Vector2 shopNamePosition;
  41. private Vector2 plankPosition;
  42. private Vector2 titleBarMidPosition;
  43. private Vector2 placeTextMid;
  44. private Rectangle screenRect;
  45. private int currentCursor;
  46. private const int interval = 50;
  47. /// <summary>
  48. /// Constructs a new StoreScreen object for the given store.
  49. /// </summary>
  50. public StoreScreen(Store store)
  51. {
  52. // check the parameter
  53. if (store == null)
  54. {
  55. throw new ArgumentNullException("store");
  56. }
  57. this.IsPopup = true;
  58. this.store = store;
  59. titleBarMidPosition = new Vector2(
  60. -Fonts.HeaderFont.MeasureString(store.Name).X / 2, 0f);
  61. placeTextMid = Fonts.ButtonNamesFont.MeasureString("Select");
  62. }
  63. /// <summary>
  64. /// Loads the graphics content from the content manager.
  65. /// </summary>
  66. public override void LoadContent()
  67. {
  68. ContentManager content = ScreenManager.Game.Content;
  69. shopDrawScreen =
  70. content.Load<Texture2D>(@"Textures\GameScreens\GameScreenBkgd");
  71. backButton =
  72. content.Load<Texture2D>(@"Textures\Buttons\BButton");
  73. selectButton =
  74. content.Load<Texture2D>(@"Textures\Buttons\AButton");
  75. highlightItem =
  76. content.Load<Texture2D>(@"Textures\GameScreens\HighlightLarge");
  77. selectionArrow =
  78. content.Load<Texture2D>(@"Textures\GameScreens\SelectionArrow");
  79. fadeTexture =
  80. content.Load<Texture2D>(@"Textures\GameScreens\FadeScreen");
  81. conversationStrip =
  82. content.Load<Texture2D>(@"Textures\GameScreens\ConversationStrip");
  83. goldIcon =
  84. content.Load<Texture2D>(@"Textures\GameScreens\GoldIcon");
  85. plankTexture =
  86. content.Load<Texture2D>(@"Textures\MainMenu\MainMenuPlank03");
  87. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  88. screenRect = new Rectangle(viewport.X, viewport.Y,
  89. viewport.Width, viewport.Height);
  90. plankPosition = new Vector2(
  91. (viewport.Width - plankTexture.Width) / 2, 66f);
  92. shopNamePosition = new Vector2(
  93. (viewport.Width - Fonts.HeaderFont.MeasureString(store.Name).X) / 2,
  94. 90f);
  95. }
  96. /// <summary>
  97. /// Handles user input.
  98. /// </summary>
  99. public override void HandleInput()
  100. {
  101. // exits the screen
  102. if (InputManager.IsActionTriggered(InputManager.Action.Back))
  103. {
  104. ExitScreen();
  105. return;
  106. }
  107. // select one of the buttons
  108. else if (InputManager.IsActionTriggered(InputManager.Action.Ok))
  109. {
  110. if (currentCursor == 0)
  111. {
  112. ScreenManager.AddScreen(new StoreBuyScreen(store));
  113. }
  114. else if (currentCursor == 1)
  115. {
  116. ScreenManager.AddScreen(new StoreSellScreen(store));
  117. }
  118. else
  119. {
  120. ExitScreen();
  121. }
  122. return;
  123. }
  124. // move the cursor up
  125. else if (InputManager.IsActionTriggered(
  126. InputManager.Action.MoveCharacterUp))
  127. {
  128. currentCursor--;
  129. if (currentCursor < 0)
  130. {
  131. currentCursor = 0;
  132. }
  133. }
  134. // move the cursor down
  135. else if (InputManager.IsActionTriggered(
  136. InputManager.Action.MoveCharacterDown))
  137. {
  138. currentCursor++;
  139. if (currentCursor > 2)
  140. {
  141. currentCursor = 2;
  142. }
  143. }
  144. }
  145. /// <summary>
  146. /// Draw the screen.
  147. /// </summary>
  148. public override void Draw(GameTime gameTime)
  149. {
  150. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  151. // Draw Shop Main Menu
  152. spriteBatch.Begin();
  153. // Draw Shop Main Menu Screen
  154. DrawMainMenu();
  155. // Draw Buttons
  156. if (IsActive)
  157. {
  158. DrawButtons();
  159. }
  160. // Measure Title of the Screen
  161. spriteBatch.Draw(plankTexture, plankPosition, Color.White);
  162. // Draw the Title of the Screen
  163. spriteBatch.DrawString(Fonts.HeaderFont, store.Name,
  164. shopNamePosition, Fonts.TitleColor);
  165. // Draw Conversation Strip
  166. spriteBatch.Draw(conversationStrip, conversationStripPosition,
  167. Color.White);
  168. // Draw Shop Keeper
  169. spriteBatch.Draw(store.ShopkeeperTexture, shopKeeperPosition, Color.White);
  170. // Draw Shop Info
  171. spriteBatch.DrawString(Fonts.DescriptionFont,
  172. Fonts.BreakTextIntoLines(store.WelcomeMessage, 55, 3),
  173. welcomeMessagePosition, Fonts.DescriptionColor);
  174. spriteBatch.End();
  175. }
  176. /// <summary>
  177. /// Draws the main menu for the store.
  178. /// </summary>
  179. private void DrawMainMenu()
  180. {
  181. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  182. Vector2 arrowPosition = Vector2.Zero;
  183. Vector2 highlightPosition = Vector2.Zero;
  184. Vector2 position = textPosition;
  185. // Draw faded screen
  186. spriteBatch.Draw(fadeTexture, screenRect, Color.White);
  187. spriteBatch.Draw(shopDrawScreen, screenRect, Color.White);
  188. arrowPosition.X = textPosition.X - selectionArrowOffset.X;
  189. arrowPosition.Y = textPosition.Y - selectionArrowOffset.Y;
  190. highlightPosition.X = textPosition.X - highlightItemOffset.X;
  191. highlightPosition.Y = textPosition.Y - highlightItemOffset.Y;
  192. // "Buy" is highlighted
  193. if (currentCursor == 0)
  194. {
  195. spriteBatch.Draw(highlightItem, highlightPosition, Color.White);
  196. spriteBatch.Draw(selectionArrow, arrowPosition, Color.White);
  197. spriteBatch.DrawString(Fonts.GearInfoFont, "Buy", position,
  198. Fonts.HighlightColor);
  199. position.Y += interval;
  200. spriteBatch.DrawString(Fonts.GearInfoFont, "Sell", position,
  201. Fonts.DisplayColor);
  202. position.Y += interval;
  203. spriteBatch.DrawString(Fonts.GearInfoFont, "Leave", position,
  204. Fonts.DisplayColor);
  205. }
  206. // "Sell" is highlighted
  207. else if (currentCursor == 1)
  208. {
  209. position = textPosition;
  210. spriteBatch.DrawString(Fonts.GearInfoFont, "Buy", position,
  211. Fonts.DisplayColor);
  212. highlightPosition.Y += interval;
  213. arrowPosition.Y += interval;
  214. position.Y += interval;
  215. spriteBatch.Draw(highlightItem, highlightPosition, Color.White);
  216. spriteBatch.Draw(selectionArrow, arrowPosition, Color.White);
  217. spriteBatch.DrawString(Fonts.GearInfoFont, "Sell", position,
  218. Fonts.HighlightColor);
  219. position.Y += interval;
  220. spriteBatch.DrawString(Fonts.GearInfoFont, "Leave", position,
  221. Fonts.DisplayColor);
  222. }
  223. // "Leave" is highlighted
  224. else if (currentCursor == 2)
  225. {
  226. position = textPosition;
  227. spriteBatch.DrawString(Fonts.GearInfoFont, "Buy", position,
  228. Fonts.DisplayColor);
  229. position.Y += interval;
  230. spriteBatch.DrawString(Fonts.GearInfoFont, "Sell", position,
  231. Fonts.DisplayColor);
  232. highlightPosition.Y += interval + interval;
  233. arrowPosition.Y += interval + interval;
  234. position.Y += interval;
  235. spriteBatch.Draw(highlightItem, highlightPosition, Color.White);
  236. spriteBatch.Draw(selectionArrow, arrowPosition, Color.White);
  237. spriteBatch.DrawString(Fonts.GearInfoFont, "Leave", position,
  238. Fonts.HighlightColor);
  239. }
  240. }
  241. /// <summary>
  242. /// Draws the buttons.
  243. /// </summary>
  244. private void DrawButtons()
  245. {
  246. if (!IsActive)
  247. {
  248. return;
  249. }
  250. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  251. Vector2 position = new Vector2();
  252. // Draw Back Button
  253. spriteBatch.Draw(backButton, backButtonPosition, Color.White);
  254. // Draw Back Text
  255. position = backButtonPosition;
  256. position.X += backButton.Width + 10;
  257. position.Y += 5;
  258. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Back", position, Color.White);
  259. // Draw Select Button
  260. spriteBatch.Draw(selectButton, selectButtonPosition, Color.White);
  261. // Draw Select Text
  262. position = selectButtonPosition;
  263. position.X -= placeTextMid.X + 10;
  264. position.Y += 5;
  265. spriteBatch.DrawString(Fonts.ButtonNamesFont, "Select", position,
  266. Color.White);
  267. // Draw Gold Text
  268. spriteBatch.DrawString(Fonts.ButtonNamesFont,
  269. Fonts.GetGoldString(Session.Party.PartyGold), partyGoldPosition,
  270. Color.White);
  271. // Draw Gold Icon
  272. spriteBatch.Draw(goldIcon, goldIconPosition, Color.White);
  273. }
  274. }
  275. }