InnScreen.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. using System.IO;
  14. namespace RolePlaying
  15. {
  16. /// <summary>
  17. /// Displays the options for an inn that the party can stay at.
  18. /// </summary>
  19. class InnScreen : GameScreen
  20. {
  21. private Inn inn;
  22. private Texture2D backgroundTexture;
  23. private Texture2D plankTexture;
  24. private Texture2D selectIconTexture;
  25. private Texture2D backIconTexture;
  26. private Texture2D highlightTexture;
  27. private Texture2D arrowTexture;
  28. private Texture2D conversationTexture;
  29. private Texture2D fadeTexture;
  30. private Texture2D goldIcon;
  31. private readonly Vector2 stayPosition = new Vector2(620f, 250f);
  32. private readonly Vector2 leavePosition = new Vector2(620f, 300f);
  33. private readonly Vector2 costPosition = new Vector2(470, 450);
  34. private readonly Vector2 informationPosition = new Vector2(470, 490);
  35. private readonly Vector2 selectIconPosition = new Vector2(1150, 640);
  36. private readonly Vector2 backIconPosition = new Vector2(80, 640);
  37. private readonly Vector2 goldStringPosition = new Vector2(565, 648);
  38. private readonly Vector2 stayArrowPosition = new Vector2(520f, 234f);
  39. private readonly Vector2 leaveArrowPosition = new Vector2(520f, 284f);
  40. private readonly Vector2 stayHighlightPosition = new Vector2(180f, 230f);
  41. private readonly Vector2 leaveHighlightPosition = new Vector2(180f, 280f);
  42. private readonly Vector2 innKeeperPosition = new Vector2(290, 370);
  43. private readonly Vector2 conversationStripPosition = new Vector2(210f, 405f);
  44. private readonly Vector2 goldIconPosition = new Vector2(490, 640);
  45. private Vector2 plankPosition;
  46. private Vector2 backgroundPosition;
  47. private Vector2 namePosition;
  48. private Vector2 selectTextPosition;
  49. private Vector2 backTextPosition;
  50. private Rectangle screenRectangle;
  51. private List<string> welcomeMessage;
  52. private List<string> serviceRenderedMessage;
  53. private List<string> noGoldMessage;
  54. private List<string> currentDialogue;
  55. private const int maxWidth = 570;
  56. private const int maxLines = 3;
  57. private string costString;
  58. private readonly string stayString = "Stay";
  59. private readonly string leaveString = "Leave";
  60. private readonly string selectString = "Select";
  61. private readonly string backString = "Leave";
  62. private int selectionMark;
  63. private int endIndex;
  64. /// <summary>
  65. /// Creates a new InnScreen object.
  66. /// </summary>
  67. public InnScreen(Inn inn)
  68. : base()
  69. {
  70. // check the parameter
  71. if (inn == null)
  72. {
  73. throw new ArgumentNullException("inn");
  74. }
  75. this.IsPopup = true;
  76. this.inn = inn;
  77. welcomeMessage = Fonts.BreakTextIntoList(inn.WelcomeMessage,
  78. Fonts.DescriptionFont, maxWidth);
  79. serviceRenderedMessage = Fonts.BreakTextIntoList(inn.PaidMessage,
  80. Fonts.DescriptionFont, maxWidth);
  81. noGoldMessage = Fonts.BreakTextIntoList(inn.NotEnoughGoldMessage,
  82. Fonts.DescriptionFont, maxWidth);
  83. selectionMark = 1;
  84. ChangeDialogue(welcomeMessage);
  85. }
  86. /// <summary>
  87. /// Load the graphics content
  88. /// </summary>
  89. /// <param name="batch">SpriteBatch object</param>
  90. /// <param name="screenWidth">Width of screen</param>
  91. /// <param name="screenHeight">Height of the screen</param>
  92. public override void LoadContent()
  93. {
  94. ContentManager content = ScreenManager.Game.Content;
  95. backgroundTexture =
  96. content.Load<Texture2D>(Path.Combine("Textures", "GameScreens", "GameScreenBkgd"));
  97. plankTexture =
  98. content.Load<Texture2D>(Path.Combine("Textures", "MainMenu", "MainMenuPlank03"));
  99. selectIconTexture =
  100. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "AButton"));
  101. backIconTexture =
  102. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "BButton"));
  103. highlightTexture =
  104. content.Load<Texture2D>(Path.Combine("Textures", "GameScreens", "HighlightLarge"));
  105. arrowTexture =
  106. content.Load<Texture2D>(Path.Combine("Textures", "GameScreens", "SelectionArrow"));
  107. conversationTexture =
  108. content.Load<Texture2D>(Path.Combine("Textures", "GameScreens", "ConversationStrip"));
  109. goldIcon = content.Load<Texture2D>(Path.Combine("Textures", "GameScreens", "GoldIcon"));
  110. fadeTexture = content.Load<Texture2D>(Path.Combine("Textures", "GameScreens", "FadeScreen"));
  111. screenRectangle = new Rectangle(0, 0,
  112. Session.BACK_BUFFER_WIDTH, Session.BACK_BUFFER_HEIGHT);
  113. plankPosition = new Vector2((Session.BACK_BUFFER_WIDTH - plankTexture.Width) / 2, 67f);
  114. backgroundPosition = new Vector2(
  115. (Session.BACK_BUFFER_WIDTH - backgroundTexture.Width) / 2,
  116. (Session.BACK_BUFFER_HEIGHT - backgroundTexture.Height) / 2);
  117. namePosition = new Vector2(
  118. (Session.BACK_BUFFER_WIDTH - Fonts.HeaderFont.MeasureString(inn.Name).X) / 2, 95f);
  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.InputAction.Back))
  134. {
  135. ExitScreen();
  136. return;
  137. }
  138. // move the cursor up
  139. else if (InputManager.IsActionTriggered(InputManager.InputAction.CursorUp))
  140. {
  141. if (selectionMark == 2)
  142. {
  143. selectionMark = 1;
  144. }
  145. }
  146. // move the cursor down
  147. else if (InputManager.IsActionTriggered(InputManager.InputAction.CursorDown))
  148. {
  149. if (selectionMark == 1)
  150. {
  151. selectionMark = 2;
  152. }
  153. }
  154. // select an option
  155. else if (InputManager.IsActionTriggered(InputManager.InputAction.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(SpriteSortMode.Deferred, null, null, null, null, null, ScreenManager.GlobalTransformation);
  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, Fonts.DisplayColor, MathHelper.ToRadians(-3.0f), Vector2.Zero, 1.0f, SpriteEffects.None, 0f);
  241. // Draw the stay and leave option texts based on the current selection
  242. if (selectionMark == 1)
  243. {
  244. spriteBatch.Draw(highlightTexture, stayHighlightPosition, Color.White);
  245. spriteBatch.Draw(arrowTexture, stayArrowPosition, Color.White);
  246. spriteBatch.DrawString(
  247. Fonts.GearInfoFont, stayString, stayPosition, Fonts.HighlightColor);
  248. spriteBatch.DrawString(
  249. Fonts.GearInfoFont, leaveString, leavePosition, Fonts.DisplayColor);
  250. }
  251. else
  252. {
  253. spriteBatch.Draw(highlightTexture, leaveHighlightPosition, Color.White);
  254. spriteBatch.Draw(arrowTexture, leaveArrowPosition, Color.White);
  255. spriteBatch.DrawString(Fonts.GearInfoFont, stayString, stayPosition,
  256. Fonts.DisplayColor);
  257. spriteBatch.DrawString(Fonts.GearInfoFont, leaveString, leavePosition,
  258. Fonts.HighlightColor);
  259. }
  260. // Draw the amount of gold
  261. spriteBatch.DrawString(Fonts.ButtonNamesFont,
  262. Fonts.GetGoldString(Session.Party.PartyGold), goldStringPosition,
  263. Color.White);
  264. // Draw the select button text
  265. spriteBatch.DrawString(
  266. Fonts.ButtonNamesFont, selectString, selectTextPosition, Color.White);
  267. // Draw the back button text
  268. spriteBatch.DrawString(Fonts.ButtonNamesFont, backString, backTextPosition,
  269. Color.White);
  270. // Draw Conversation Strip
  271. spriteBatch.Draw(conversationTexture, conversationStripPosition,
  272. Color.White);
  273. // Draw Shop Keeper
  274. spriteBatch.Draw(inn.ShopkeeperTexture, innKeeperPosition, Color.White);
  275. // Draw the cost to stay
  276. costString = "Cost: " + GetChargeForParty(Session.Party) + " Gold";
  277. spriteBatch.DrawString(Fonts.DescriptionFont, costString, costPosition,
  278. Color.DarkRed);
  279. // Draw the innkeeper dialog
  280. for (int i = 0; i < endIndex; i++)
  281. {
  282. spriteBatch.DrawString(Fonts.DescriptionFont, currentDialogue[i],
  283. dialogPosition, Color.Black);
  284. dialogPosition.Y += Fonts.DescriptionFont.LineSpacing;
  285. }
  286. // Draw Gold Icon
  287. spriteBatch.Draw(goldIcon, goldIconPosition, Color.White);
  288. spriteBatch.End();
  289. }
  290. }
  291. }