StoreScreen.cs 12 KB

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