Hud.cs 23 KB

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