Hud.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. //-----------------------------------------------------------------------------
  2. // Hud.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 each player's basic statistics and the combat action menu.
  18. /// </summary>
  19. class Hud
  20. {
  21. private ScreenManager screenManager;
  22. public const int HudHeight = 183;
  23. private Texture2D backgroundHudTexture;
  24. private Texture2D topHudTexture;
  25. private Texture2D combatPopupTexture;
  26. private Texture2D activeCharInfoTexture;
  27. private Texture2D inActiveCharInfoTexture;
  28. private Texture2D cantUseCharInfoTexture;
  29. private Texture2D selectionBracketTexture;
  30. private Texture2D menuTexture;
  31. private Texture2D statsTexture;
  32. private Texture2D deadPortraitTexture;
  33. private Texture2D charSelFadeLeftTexture;
  34. private Texture2D charSelFadeRightTexture;
  35. private Texture2D charSelArrowLeftTexture;
  36. private Texture2D charSelArrowRightTexture;
  37. private Texture2D actionTexture;
  38. private Texture2D yButtonTexture;
  39. private Texture2D startButtonTexture;
  40. private Vector2 topHudPosition = new Vector2(353f, 30f);
  41. private Vector2 charSelLeftPosition = new Vector2(70f, 600f);
  42. private Vector2 charSelRightPosition = new Vector2(1170f, 600f);
  43. private Vector2 yButtonPosition = new Vector2(0f, 560f + 20f);
  44. private Vector2 startButtonPosition = new Vector2(0f, 560f + 35f);
  45. private Vector2 yTextPosition = new Vector2(0f, 560f + 70f);
  46. private Vector2 startTextPosition = new Vector2(0f, 560f + 70f);
  47. private Vector2 actionTextPosition = new Vector2(640f, 55f);
  48. private Vector2 backgroundHudPosition = new Vector2(0f, 525f);
  49. private Vector2 portraitPosition = new Vector2(640f, 55f);
  50. private Vector2 startingInfoPosition = new Vector2(0f, 550f);
  51. private Vector2 namePosition;
  52. private Vector2 levelPosition;
  53. private Vector2 detailPosition;
  54. private readonly Color activeNameColor = new Color(200, 200, 200);
  55. private readonly Color inActiveNameColor = new Color(100, 100, 100);
  56. private readonly Color nonSelColor = new Color(86, 26, 5);
  57. private readonly Color selColor = new Color(229, 206, 144);
  58. /// <summary>
  59. /// The text that is shown in the action bar at the top of the combat screen.
  60. /// </summary>
  61. private string actionText = String.Empty;
  62. /// <summary>
  63. /// The text that is shown in the action bar at the top of the combat screen.
  64. /// </summary>
  65. public string ActionText
  66. {
  67. get { return actionText; }
  68. set { actionText = value; }
  69. }
  70. /// <summary>
  71. /// Creates a new Hud object using the given ScreenManager.
  72. /// </summary>
  73. public Hud(ScreenManager screenManager)
  74. {
  75. // check the parameter
  76. if (screenManager == null)
  77. {
  78. throw new ArgumentNullException("screenManager");
  79. }
  80. this.screenManager = screenManager;
  81. }
  82. /// <summary>
  83. /// Load the graphics content from the content manager.
  84. /// </summary>
  85. public void LoadContent()
  86. {
  87. ContentManager content = screenManager.Game.Content;
  88. backgroundHudTexture =
  89. content.Load<Texture2D>(Path.Combine("Textures", "HUD", "HudBkgd"));
  90. topHudTexture =
  91. content.Load<Texture2D>(Path.Combine("Textures", "HUD", "CombatStateInfoStrip"));
  92. activeCharInfoTexture =
  93. content.Load<Texture2D>(Path.Combine("Textures", "HUD", "PlankActive"));
  94. inActiveCharInfoTexture =
  95. content.Load<Texture2D>(Path.Combine("Textures", "HUD", "PlankInactive"));
  96. cantUseCharInfoTexture =
  97. content.Load<Texture2D>(Path.Combine("Textures", "HUD", "PlankCantUse"));
  98. selectionBracketTexture =
  99. content.Load<Texture2D>(Path.Combine("Textures", "HUD", "SelectionBrackets"));
  100. deadPortraitTexture =
  101. content.Load<Texture2D>(Path.Combine("Textures", "Characters", "Portraits", "Tombstone"));
  102. combatPopupTexture =
  103. content.Load<Texture2D>(Path.Combine("Textures", "HUD", "CombatPopup"));
  104. charSelFadeLeftTexture =
  105. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "CharSelectFadeLeft"));
  106. charSelFadeRightTexture =
  107. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "CharSelectFadeRight"));
  108. charSelArrowLeftTexture =
  109. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "CharSelectHlLeft"));
  110. charSelArrowRightTexture =
  111. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "CharSelectHlRight"));
  112. actionTexture =
  113. content.Load<Texture2D>(Path.Combine("Textures", "HUD", "HudSelectButton"));
  114. yButtonTexture =
  115. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "YButton"));
  116. startButtonTexture =
  117. content.Load<Texture2D>(Path.Combine("Textures", "Buttons", "StartButton"));
  118. menuTexture =
  119. content.Load<Texture2D>(Path.Combine("Textures", "HUD", "Menu"));
  120. statsTexture =
  121. content.Load<Texture2D>(Path.Combine("Textures", "HUD", "Stats"));
  122. }
  123. /// <summary>
  124. /// Draw the screen.
  125. /// </summary>
  126. public void Draw()
  127. {
  128. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  129. spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, Session.ScreenManager.GlobalTransformation);
  130. startingInfoPosition.X = 640f;
  131. startingInfoPosition.X -= Session.Party.Players.Count / 2 * 200f;
  132. if (Session.Party.Players.Count % 2 != 0)
  133. {
  134. startingInfoPosition.X -= 100f;
  135. }
  136. spriteBatch.Draw(backgroundHudTexture, backgroundHudPosition, Color.White);
  137. if (CombatEngine.IsActive)
  138. {
  139. DrawForCombat();
  140. }
  141. else
  142. {
  143. DrawForNonCombat();
  144. }
  145. spriteBatch.End();
  146. }
  147. /// <summary>
  148. /// Draws HUD for Combat Mode
  149. /// </summary>
  150. private void DrawForCombat()
  151. {
  152. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  153. Vector2 position = startingInfoPosition;
  154. foreach (CombatantPlayer combatantPlayer in CombatEngine.Players)
  155. {
  156. DrawCombatPlayerDetails(combatantPlayer, position);
  157. position.X += activeCharInfoTexture.Width - 6f;
  158. }
  159. charSelLeftPosition.X = startingInfoPosition.X - 5f -
  160. charSelArrowLeftTexture.Width;
  161. charSelRightPosition.X = position.X + 5f;
  162. // Draw character Selection Arrows
  163. if (CombatEngine.IsPlayersTurn)
  164. {
  165. spriteBatch.Draw(charSelArrowLeftTexture, charSelLeftPosition,
  166. Color.White);
  167. spriteBatch.Draw(charSelArrowRightTexture, charSelRightPosition,
  168. Color.White);
  169. }
  170. else
  171. {
  172. spriteBatch.Draw(charSelFadeLeftTexture, charSelLeftPosition,
  173. Color.White);
  174. spriteBatch.Draw(charSelFadeRightTexture, charSelRightPosition,
  175. Color.White);
  176. }
  177. if (actionText.Length > 0)
  178. {
  179. spriteBatch.Draw(topHudTexture, topHudPosition, Color.White);
  180. // Draw Action Text
  181. Fonts.DrawCenteredText(spriteBatch, Fonts.PlayerStatisticsFont,
  182. actionText, actionTextPosition, Color.Black);
  183. }
  184. }
  185. /// <summary>
  186. /// Draws HUD for non Combat Mode
  187. /// </summary>
  188. private void DrawForNonCombat()
  189. {
  190. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  191. Vector2 position = startingInfoPosition;
  192. foreach (Player player in Session.Party.Players)
  193. {
  194. DrawNonCombatPlayerDetails(player, position);
  195. position.X += inActiveCharInfoTexture.Width - 6f;
  196. }
  197. yTextPosition.X = position.X + 5f;
  198. yButtonPosition.X = position.X + 9f;
  199. // Draw Select Button
  200. spriteBatch.Draw(statsTexture, yTextPosition, Color.White);
  201. spriteBatch.Draw(yButtonTexture, yButtonPosition, Color.White);
  202. startTextPosition.X = startingInfoPosition.X -
  203. startButtonTexture.Width - 25f;
  204. startButtonPosition.X = startingInfoPosition.X -
  205. startButtonTexture.Width - 10f;
  206. // Draw Back Button
  207. spriteBatch.Draw(menuTexture, startTextPosition, Color.White);
  208. spriteBatch.Draw(startButtonTexture, startButtonPosition, Color.White);
  209. }
  210. enum PlankState
  211. {
  212. Active,
  213. Inactive,
  214. CantUse,
  215. }
  216. /// <summary>
  217. /// Draws Player Details
  218. /// </summary>
  219. /// <param name="playerIndex">Index of player details to draw</param>
  220. /// <param name="position">Position where to draw</param>
  221. private void DrawCombatPlayerDetails(CombatantPlayer player, Vector2 position)
  222. {
  223. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  224. PlankState plankState;
  225. bool isPortraitActive = false;
  226. bool isCharDead = false;
  227. Color color;
  228. portraitPosition.X = position.X + 7f;
  229. portraitPosition.Y = position.Y + 7f;
  230. namePosition.X = position.X + 84f;
  231. namePosition.Y = position.Y + 12f;
  232. levelPosition.X = position.X + 84f;
  233. levelPosition.Y = position.Y + 39f;
  234. detailPosition.X = position.X + 25f;
  235. detailPosition.Y = position.Y + 66f;
  236. position.X -= 2;
  237. position.Y -= 4;
  238. if (player.IsTurnTaken)
  239. {
  240. plankState = PlankState.CantUse;
  241. isPortraitActive = false;
  242. }
  243. else
  244. {
  245. plankState = PlankState.Inactive;
  246. isPortraitActive = true;
  247. }
  248. if (((CombatEngine.HighlightedCombatant == player) && !player.IsTurnTaken) ||
  249. (CombatEngine.PrimaryTargetedCombatant == player) ||
  250. (CombatEngine.SecondaryTargetedCombatants.Contains(player)))
  251. {
  252. plankState = PlankState.Active;
  253. }
  254. if (player.IsDeadOrDying)
  255. {
  256. isCharDead = true;
  257. isPortraitActive = false;
  258. plankState = PlankState.CantUse;
  259. }
  260. // Draw Info Slab
  261. if (plankState == PlankState.Active)
  262. {
  263. color = activeNameColor;
  264. spriteBatch.Draw(activeCharInfoTexture, position, Color.White);
  265. // Draw Brackets
  266. if ((CombatEngine.HighlightedCombatant == player) && !player.IsTurnTaken)
  267. {
  268. spriteBatch.Draw(selectionBracketTexture, position, Color.White);
  269. }
  270. if (isPortraitActive &&
  271. (CombatEngine.HighlightedCombatant == player) &&
  272. (CombatEngine.HighlightedCombatant.CombatAction == null) &&
  273. !CombatEngine.IsDelaying)
  274. {
  275. position.X += activeCharInfoTexture.Width / 2;
  276. position.X -= combatPopupTexture.Width / 2;
  277. position.Y -= combatPopupTexture.Height;
  278. // Draw Action
  279. DrawActionsMenu(position);
  280. }
  281. }
  282. else if (plankState == PlankState.Inactive)
  283. {
  284. color = inActiveNameColor;
  285. spriteBatch.Draw(inActiveCharInfoTexture, position, Color.White);
  286. }
  287. else
  288. {
  289. color = Color.Black;
  290. spriteBatch.Draw(cantUseCharInfoTexture, position, Color.White);
  291. }
  292. if (isCharDead)
  293. {
  294. spriteBatch.Draw(deadPortraitTexture, portraitPosition, Color.White);
  295. }
  296. else
  297. {
  298. // Draw Player Portrait
  299. DrawPortrait(player.Player, portraitPosition, plankState);
  300. }
  301. // Draw Player Name
  302. spriteBatch.DrawString(Fonts.PlayerStatisticsFont,
  303. player.Player.Name,
  304. namePosition, color);
  305. color = Color.Black;
  306. // Draw Player Details
  307. spriteBatch.DrawString(Fonts.HudDetailFont,
  308. "Lvl: " + player.Player.CharacterLevel,
  309. levelPosition, color);
  310. spriteBatch.DrawString(Fonts.HudDetailFont,
  311. "HP: " + player.Statistics.HealthPoints +
  312. "/" + player.Player.CharacterStatistics.HealthPoints,
  313. detailPosition, color);
  314. detailPosition.Y += 30f;
  315. spriteBatch.DrawString(Fonts.HudDetailFont,
  316. "MP: " + player.Statistics.MagicPoints +
  317. "/" + player.Player.CharacterStatistics.MagicPoints,
  318. detailPosition, color);
  319. }
  320. /// <summary>
  321. /// Draws Player Details
  322. /// </summary>
  323. /// <param name="playerIndex">Index of player details to draw</param>
  324. /// <param name="position">Position where to draw</param>
  325. private void DrawNonCombatPlayerDetails(Player player, Vector2 position)
  326. {
  327. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  328. PlankState plankState;
  329. bool isCharDead = false;
  330. Color color;
  331. portraitPosition.X = position.X + 7f;
  332. portraitPosition.Y = position.Y + 7f;
  333. namePosition.X = position.X + 84f;
  334. namePosition.Y = position.Y + 12f;
  335. levelPosition.X = position.X + 84f;
  336. levelPosition.Y = position.Y + 39f;
  337. detailPosition.X = position.X + 25f;
  338. detailPosition.Y = position.Y + 66f;
  339. position.X -= 2;
  340. position.Y -= 4;
  341. plankState = PlankState.Active;
  342. // Draw Info Slab
  343. if (plankState == PlankState.Active)
  344. {
  345. color = activeNameColor;
  346. spriteBatch.Draw(activeCharInfoTexture, position, Color.White);
  347. }
  348. else if (plankState == PlankState.Inactive)
  349. {
  350. color = inActiveNameColor;
  351. spriteBatch.Draw(inActiveCharInfoTexture, position, Color.White);
  352. }
  353. else
  354. {
  355. color = Color.Black;
  356. spriteBatch.Draw(cantUseCharInfoTexture, position, Color.White);
  357. }
  358. if (isCharDead)
  359. {
  360. spriteBatch.Draw(deadPortraitTexture, portraitPosition, Color.White);
  361. }
  362. else
  363. {
  364. // Draw Player Portrait
  365. DrawPortrait(player, portraitPosition, plankState);
  366. }
  367. // Draw Player Name
  368. spriteBatch.DrawString(Fonts.PlayerStatisticsFont,
  369. player.Name,
  370. namePosition, color);
  371. color = Color.Black;
  372. // Draw Player Details
  373. spriteBatch.DrawString(Fonts.HudDetailFont,
  374. "Lvl: " + player.CharacterLevel,
  375. levelPosition, color);
  376. spriteBatch.DrawString(Fonts.HudDetailFont,
  377. "HP: " + player.CurrentStatistics.HealthPoints +
  378. "/" + player.CharacterStatistics.HealthPoints,
  379. detailPosition, color);
  380. detailPosition.Y += 30f;
  381. spriteBatch.DrawString(Fonts.HudDetailFont,
  382. "MP: " + player.CurrentStatistics.MagicPoints +
  383. "/" + player.CharacterStatistics.MagicPoints,
  384. detailPosition, color);
  385. }
  386. /// <summary>
  387. /// Draw the portrait of the given player at the given position.
  388. /// </summary>
  389. private void DrawPortrait(Player player, Vector2 position,
  390. PlankState plankState)
  391. {
  392. switch (plankState)
  393. {
  394. case PlankState.Active:
  395. screenManager.SpriteBatch.Draw(player.ActivePortraitTexture,
  396. position, Color.White);
  397. break;
  398. case PlankState.Inactive:
  399. screenManager.SpriteBatch.Draw(player.InactivePortraitTexture,
  400. position, Color.White);
  401. break;
  402. case PlankState.CantUse:
  403. screenManager.SpriteBatch.Draw(player.UnselectablePortraitTexture,
  404. position, Color.White);
  405. break;
  406. }
  407. }
  408. /// <summary>
  409. /// The list of entries in the combat action menu.
  410. /// </summary>
  411. private string[] actionList = new string[5]
  412. {
  413. "Attack",
  414. "Spell",
  415. "Item",
  416. "Defend",
  417. "Flee",
  418. };
  419. /// <summary>
  420. /// The currently highlighted item.
  421. /// </summary>
  422. private int highlightedAction = 0;
  423. /// <summary>
  424. /// Handle user input to the actions menu.
  425. /// </summary>
  426. public void UpdateActionsMenu()
  427. {
  428. // cursor up
  429. if (InputManager.IsActionTriggered(InputManager.InputAction.CursorUp))
  430. {
  431. if (highlightedAction > 0)
  432. {
  433. highlightedAction--;
  434. }
  435. return;
  436. }
  437. // cursor down
  438. if (InputManager.IsActionTriggered(InputManager.InputAction.CursorDown))
  439. {
  440. if (highlightedAction < actionList.Length - 1)
  441. {
  442. highlightedAction++;
  443. }
  444. return;
  445. }
  446. // select an action
  447. if (InputManager.IsActionTriggered(InputManager.InputAction.Ok))
  448. {
  449. switch (actionList[highlightedAction])
  450. {
  451. case "Attack":
  452. {
  453. ActionText = "Performing a Melee Attack";
  454. CombatEngine.HighlightedCombatant.CombatAction =
  455. new MeleeCombatAction(CombatEngine.HighlightedCombatant);
  456. CombatEngine.HighlightedCombatant.CombatAction.Target =
  457. CombatEngine.FirstEnemyTarget;
  458. }
  459. break;
  460. case "Spell":
  461. {
  462. SpellbookScreen spellbookScreen = new SpellbookScreen(
  463. CombatEngine.HighlightedCombatant.Character,
  464. CombatEngine.HighlightedCombatant.Statistics);
  465. spellbookScreen.SpellSelected +=
  466. new SpellbookScreen.SpellSelectedHandler(
  467. spellbookScreen_SpellSelected);
  468. Session.ScreenManager.AddScreen(spellbookScreen);
  469. }
  470. break;
  471. case "Item":
  472. {
  473. InventoryScreen inventoryScreen = new InventoryScreen(true);
  474. inventoryScreen.GearSelected +=
  475. new InventoryScreen.GearSelectedHandler(
  476. inventoryScreen_GearSelected);
  477. Session.ScreenManager.AddScreen(inventoryScreen);
  478. }
  479. break;
  480. case "Defend":
  481. {
  482. ActionText = "Defending";
  483. CombatEngine.HighlightedCombatant.CombatAction =
  484. new DefendCombatAction(
  485. CombatEngine.HighlightedCombatant);
  486. CombatEngine.HighlightedCombatant.CombatAction.Start();
  487. }
  488. break;
  489. case "Flee":
  490. CombatEngine.AttemptFlee();
  491. break;
  492. }
  493. return;
  494. }
  495. }
  496. /// <summary>
  497. /// Recieves the spell from the Spellbook screen and casts it.
  498. /// </summary>
  499. void spellbookScreen_SpellSelected(Spell spell)
  500. {
  501. if (spell != null)
  502. {
  503. ActionText = "Casting " + spell.Name;
  504. CombatEngine.HighlightedCombatant.CombatAction =
  505. new SpellCombatAction(CombatEngine.HighlightedCombatant, spell);
  506. if (spell.IsOffensive)
  507. {
  508. CombatEngine.HighlightedCombatant.CombatAction.Target =
  509. CombatEngine.FirstEnemyTarget;
  510. }
  511. else
  512. {
  513. CombatEngine.HighlightedCombatant.CombatAction.Target =
  514. CombatEngine.HighlightedCombatant;
  515. }
  516. }
  517. }
  518. /// <summary>
  519. /// Receives the item back from the Inventory screen and uses it.
  520. /// </summary>
  521. void inventoryScreen_GearSelected(Gear gear)
  522. {
  523. Item item = gear as Item;
  524. if (item != null)
  525. {
  526. ActionText = "Using " + item.Name;
  527. CombatEngine.HighlightedCombatant.CombatAction =
  528. new ItemCombatAction(CombatEngine.HighlightedCombatant, item);
  529. if (item.IsOffensive)
  530. {
  531. CombatEngine.HighlightedCombatant.CombatAction.Target =
  532. CombatEngine.FirstEnemyTarget;
  533. }
  534. else
  535. {
  536. CombatEngine.HighlightedCombatant.CombatAction.Target =
  537. CombatEngine.HighlightedCombatant;
  538. }
  539. }
  540. }
  541. /// <summary>
  542. /// Draws the combat action menu.
  543. /// </summary>
  544. /// <param name="position">The position of the menu.</param>
  545. private void DrawActionsMenu(Vector2 position)
  546. {
  547. ActionText = "Choose an Action";
  548. SpriteBatch spriteBatch = screenManager.SpriteBatch;
  549. Vector2 arrowPosition;
  550. float height = 25f;
  551. spriteBatch.Draw(combatPopupTexture, position, Color.White);
  552. position.Y += 21f;
  553. arrowPosition = position;
  554. arrowPosition.X += 10f;
  555. arrowPosition.Y += 2f;
  556. arrowPosition.Y += height * (int)highlightedAction;
  557. spriteBatch.Draw(actionTexture, arrowPosition, Color.White);
  558. position.Y += 4f;
  559. position.X += 50f;
  560. // Draw Action Text
  561. for (int i = 0; i < actionList.Length; i++)
  562. {
  563. spriteBatch.DrawString(Fonts.GearInfoFont, actionList[i], position,
  564. i == highlightedAction ? selColor : nonSelColor);
  565. position.Y += height;
  566. }
  567. }
  568. }
  569. }