InnScreen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //-----------------------------------------------------------------------------
  2. // InnScreen.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. /// Displays the options for an inn that the party can stay at.
  17. /// </summary>
  18. class InnScreen : GameScreen
  19. {
  20. private Inn inn;
  21. private Texture2D backgroundTexture;
  22. private Texture2D plankTexture;
  23. private Texture2D selectIconTexture;
  24. private Texture2D backIconTexture;
  25. private Texture2D highlightTexture;
  26. private Texture2D arrowTexture;
  27. private Texture2D conversationTexture;
  28. private Texture2D fadeTexture;
  29. private Texture2D goldIcon;
  30. private readonly Vector2 stayPosition = new Vector2(620f, 250f);
  31. private readonly Vector2 leavePosition = new Vector2(620f, 300f);
  32. private readonly Vector2 costPosition = new Vector2(470, 450);
  33. private readonly Vector2 informationPosition = new Vector2(470, 490);
  34. private readonly Vector2 selectIconPosition = new Vector2(1150, 640);
  35. private readonly Vector2 backIconPosition = new Vector2(80, 640);
  36. private readonly Vector2 goldStringPosition = new Vector2(565, 648);
  37. private readonly Vector2 stayArrowPosition = new Vector2(520f, 234f);
  38. private readonly Vector2 leaveArrowPosition = new Vector2(520f, 284f);
  39. private readonly Vector2 stayHighlightPosition = new Vector2(180f, 230f);
  40. private readonly Vector2 leaveHighlightPosition = new Vector2(180f, 280f);
  41. private readonly Vector2 innKeeperPosition = new Vector2(290, 370);
  42. private readonly Vector2 conversationStripPosition = new Vector2(210f, 405f);
  43. private readonly Vector2 goldIconPosition = new Vector2(490, 640);
  44. private Vector2 plankPosition;
  45. private Vector2 backgroundPosition;
  46. private Vector2 namePosition;
  47. private Vector2 selectTextPosition;
  48. private Vector2 backTextPosition;
  49. private Rectangle screenRectangle;
  50. private List<string> welcomeMessage;
  51. private List<string> serviceRenderedMessage;
  52. private List<string> noGoldMessage;
  53. private List<string> currentDialogue;
  54. private const int maxWidth = 570;
  55. private const int maxLines = 3;
  56. private string costString;
  57. private readonly string stayString = "Stay";
  58. private readonly string leaveString = "Leave";
  59. private readonly string selectString = "Select";
  60. private readonly string backString = "Leave";
  61. private int selectionMark;
  62. private int endIndex;
  63. /// <summary>
  64. /// Creates a new InnScreen object.
  65. /// </summary>
  66. public InnScreen(Inn inn)
  67. : base()
  68. {
  69. // check the parameter
  70. if (inn == null)
  71. {
  72. throw new ArgumentNullException("inn");
  73. }
  74. this.IsPopup = true;
  75. this.inn = inn;
  76. welcomeMessage = Fonts.BreakTextIntoList(inn.WelcomeMessage,
  77. Fonts.DescriptionFont, maxWidth);
  78. serviceRenderedMessage = Fonts.BreakTextIntoList(inn.PaidMessage,
  79. Fonts.DescriptionFont, maxWidth);
  80. noGoldMessage = Fonts.BreakTextIntoList(inn.NotEnoughGoldMessage,
  81. Fonts.DescriptionFont, maxWidth);
  82. selectionMark = 1;
  83. ChangeDialogue(welcomeMessage);
  84. }
  85. /// <summary>
  86. /// Load the graphics content
  87. /// </summary>
  88. /// <param name="batch">SpriteBatch object</param>
  89. /// <param name="screenWidth">Width of screen</param>
  90. /// <param name="screenHeight">Height of the screen</param>
  91. public override void LoadContent()
  92. {
  93. Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
  94. ContentManager content = ScreenManager.Game.Content;
  95. backgroundTexture =
  96. content.Load<Texture2D>(@"Textures\GameScreens\GameScreenBkgd");
  97. plankTexture =
  98. content.Load<Texture2D>(@"Textures\MainMenu\MainMenuPlank03");
  99. selectIconTexture =
  100. content.Load<Texture2D>(@"Textures\Buttons\AButton");
  101. backIconTexture =
  102. content.Load<Texture2D>(@"Textures\Buttons\BButton");
  103. highlightTexture =
  104. content.Load<Texture2D>(@"Textures\GameScreens\HighlightLarge");
  105. arrowTexture =
  106. content.Load<Texture2D>(@"Textures\GameScreens\SelectionArrow");
  107. conversationTexture =
  108. content.Load<Texture2D>(@"Textures\GameScreens\ConversationStrip");
  109. goldIcon = content.Load<Texture2D>(@"Textures\GameScreens\GoldIcon");
  110. fadeTexture = content.Load<Texture2D>(@"Textures\GameScreens\FadeScreen");
  111. screenRectangle = new Rectangle(viewport.X, viewport.Y,
  112. viewport.Width, viewport.Height);
  113. plankPosition = new Vector2((viewport.Width - plankTexture.Width) / 2, 67f);
  114. backgroundPosition = new Vector2(
  115. (viewport.Width - backgroundTexture.Width) / 2,
  116. (viewport.Height - backgroundTexture.Height) / 2);
  117. namePosition = new Vector2(
  118. (viewport.Width - Fonts.HeaderFont.MeasureString(inn.Name).X) / 2, 90f);
  119. selectTextPosition = selectIconPosition;
  120. selectTextPosition.X -=
  121. Fonts.ButtonNamesFont.MeasureString(selectString).X + 10;
  122. selectTextPosition.Y += 5;
  123. backTextPosition = backIconPosition;
  124. backTextPosition.X += backIconTexture.Width + 10;
  125. backTextPosition.Y += 5;
  126. }
  127. /// <summary>
  128. /// Handle user input.
  129. /// </summary>
  130. public override void HandleInput()
  131. {
  132. // exit the screen
  133. if (InputManager.IsActionTriggered(InputManager.Action.Back))
  134. {
  135. ExitScreen();
  136. return;
  137. }
  138. // move the cursor up
  139. else if (InputManager.IsActionTriggered(InputManager.Action.CursorUp))
  140. {
  141. if (selectionMark == 2)
  142. {
  143. selectionMark = 1;
  144. }
  145. }
  146. // move the cursor down
  147. else if (InputManager.IsActionTriggered(InputManager.Action.CursorDown))
  148. {
  149. if (selectionMark == 1)
  150. {
  151. selectionMark = 2;
  152. }
  153. }
  154. // select an option
  155. else if (InputManager.IsActionTriggered(InputManager.Action.Ok))
  156. {
  157. if (selectionMark == 1)
  158. {
  159. int partyCharge = GetChargeForParty(Session.Party);
  160. if (Session.Party.PartyGold >= partyCharge)
  161. {
  162. AudioManager.PlayCue("Money");
  163. Session.Party.PartyGold -= partyCharge;
  164. selectionMark = 2;
  165. ChangeDialogue(serviceRenderedMessage);
  166. HealParty(Session.Party);
  167. }
  168. else
  169. {
  170. selectionMark = 2;
  171. ChangeDialogue(noGoldMessage);
  172. }
  173. }
  174. else
  175. {
  176. ExitScreen();
  177. return;
  178. }
  179. }
  180. }
  181. /// <summary>
  182. /// Change the current dialogue.
  183. /// </summary>
  184. private void ChangeDialogue(List<string> newDialogue)
  185. {
  186. currentDialogue = newDialogue;
  187. endIndex = maxLines;
  188. if (endIndex > currentDialogue.Count)
  189. {
  190. endIndex = currentDialogue.Count;
  191. }
  192. }
  193. /// <summary>
  194. /// Calculate the charge for the party's stay at the inn.
  195. /// </summary>
  196. private int GetChargeForParty(Party party)
  197. {
  198. // check the parameter
  199. if (party == null)
  200. {
  201. throw new ArgumentNullException("party");
  202. }
  203. return inn.ChargePerPlayer * party.Players.Count;
  204. }
  205. /// <summary>
  206. /// Heal the party back to their correct values for level + gear.
  207. /// </summary>
  208. private void HealParty(Party party)
  209. {
  210. // check the parameter
  211. if (party == null)
  212. {
  213. throw new ArgumentNullException("party");
  214. }
  215. // reset the statistics for each player
  216. foreach (Player player in party.Players)
  217. {
  218. player.StatisticsModifiers = new StatisticsValue();
  219. }
  220. }
  221. /// <summary>
  222. /// Draw the screen.
  223. /// </summary>
  224. public override void Draw(GameTime gameTime)
  225. {
  226. SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  227. Vector2 dialogPosition = informationPosition;
  228. spriteBatch.Begin();
  229. // Draw fade screen
  230. spriteBatch.Draw(fadeTexture, screenRectangle, Color.White);
  231. // Draw the background
  232. spriteBatch.Draw(backgroundTexture, backgroundPosition, Color.White);
  233. // Draw the wooden plank
  234. spriteBatch.Draw(plankTexture, plankPosition, Color.White);
  235. // Draw the select icon
  236. spriteBatch.Draw(selectIconTexture, selectIconPosition, Color.White);
  237. // Draw the back icon
  238. spriteBatch.Draw(backIconTexture, backIconPosition, Color.White);
  239. // Draw the inn name on the wooden plank
  240. spriteBatch.DrawString(Fonts.HeaderFont, inn.Name, namePosition,
  241. Fonts.DisplayColor);
  242. // Draw the stay and leave option texts based on the current selection
  243. if (selectionMark == 1)
  244. {
  245. spriteBatch.Draw(highlightTexture, stayHighlightPosition, Color.White);
  246. spriteBatch.Draw(arrowTexture, stayArrowPosition, Color.White);
  247. spriteBatch.DrawString(
  248. Fonts.GearInfoFont, stayString, stayPosition, Fonts.HighlightColor);
  249. spriteBatch.DrawString(
  250. Fonts.GearInfoFont, leaveString, leavePosition, Fonts.DisplayColor);
  251. }
  252. else
  253. {
  254. spriteBatch.Draw(highlightTexture, leaveHighlightPosition, Color.White);
  255. spriteBatch.Draw(arrowTexture, leaveArrowPosition, Color.White);
  256. spriteBatch.DrawString(Fonts.GearInfoFont, stayString, stayPosition,
  257. Fonts.DisplayColor);
  258. spriteBatch.DrawString(Fonts.GearInfoFont, leaveString, leavePosition,
  259. Fonts.HighlightColor);
  260. }
  261. // Draw the amount of gold
  262. spriteBatch.DrawString(Fonts.ButtonNamesFont,
  263. Fonts.GetGoldString(Session.Party.PartyGold), goldStringPosition,
  264. Color.White);
  265. // Draw the select button text
  266. spriteBatch.DrawString(
  267. Fonts.ButtonNamesFont, selectString, selectTextPosition, Color.White);
  268. // Draw the back button text
  269. spriteBatch.DrawString(Fonts.ButtonNamesFont, backString, backTextPosition,
  270. Color.White);
  271. // Draw Conversation Strip
  272. spriteBatch.Draw(conversationTexture, conversationStripPosition,
  273. Color.White);
  274. // Draw Shop Keeper
  275. spriteBatch.Draw(inn.ShopkeeperTexture, innKeeperPosition, Color.White);
  276. // Draw the cost to stay
  277. costString = "Cost: " + GetChargeForParty(Session.Party) + " Gold";
  278. spriteBatch.DrawString(Fonts.DescriptionFont, costString, costPosition,
  279. Color.DarkRed);
  280. // Draw the innkeeper dialog
  281. for (int i = 0; i < endIndex; i++)
  282. {
  283. spriteBatch.DrawString(Fonts.DescriptionFont, currentDialogue[i],
  284. dialogPosition, Color.Black);
  285. dialogPosition.Y += Fonts.DescriptionFont.LineSpacing;
  286. }
  287. // Draw Gold Icon
  288. spriteBatch.Draw(goldIcon, goldIconPosition, Color.White);
  289. spriteBatch.End();
  290. }
  291. }
  292. }