GameplayScreen.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameplayScreen.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 System.Linq;
  13. using System.Text;
  14. using Microsoft.Xna.Framework;
  15. using Microsoft.Xna.Framework.Graphics;
  16. using Microsoft.Xna.Framework.Audio;
  17. using System.IO.IsolatedStorage;
  18. using System.IO;
  19. using Microsoft.Xna.Framework.Input;
  20. using Microsoft.Xna.Framework.Input.Touch;
  21. //using Microsoft.Devices.Sensors;
  22. using GameStateManagement;
  23. using Microsoft.Xna.Framework.Net;
  24. #endregion
  25. namespace CatapultGame
  26. {
  27. class GameplayScreen : GameScreen
  28. {
  29. #region Fields
  30. enum MessageType : byte
  31. {
  32. NewGame = 1,
  33. CatapultFiring = 2,
  34. CatapultAiming = 3,
  35. UpdateEnvironment = 4,
  36. }
  37. internal NetworkSession NetworkSession { get; private set; }
  38. PacketReader packetReader = new PacketReader ();
  39. PacketWriter packetWriter = new PacketWriter ();
  40. internal bool IsNetworking { get; private set; }
  41. // Texture Members
  42. Texture2D foregroundTexture;
  43. Texture2D cloud1Texture;
  44. Texture2D cloud2Texture;
  45. Texture2D mountainTexture;
  46. Texture2D skyTexture;
  47. Texture2D hudBackgroundTexture;
  48. Texture2D ammoTypeTexture;
  49. Texture2D windArrowTexture;
  50. Texture2D defeatTexture;
  51. Texture2D victoryTexture;
  52. SpriteFont hudFont;
  53. // Rendering members
  54. Vector2 cloud1Position;
  55. Vector2 cloud2Position;
  56. Vector2 playerOneHUDPosition;
  57. Vector2 computerHUDPosition;
  58. Vector2 windArrowPosition;
  59. // Gameplay members
  60. Human playerOne;
  61. Human playerTwo;
  62. Vector2 wind;
  63. bool changeTurn;
  64. bool isFirstPlayerTurn;
  65. bool gameOver;
  66. Random random;
  67. const int minWind = 0;
  68. const int maxWind = 10;
  69. // Helper members
  70. bool isDragging;
  71. #endregion
  72. #region Initialization
  73. public GameplayScreen ()
  74. {
  75. EnabledGestures = GestureType.FreeDrag |
  76. GestureType.DragComplete |
  77. GestureType.Tap;
  78. random = new Random ();
  79. }
  80. #endregion
  81. #region Content Loading/Unloading
  82. /// <summary>
  83. /// Loads the game assets and initializes "players"
  84. /// </summary>
  85. public override void LoadContent ()
  86. {
  87. base.LoadContent ();
  88. #if ANDROID || IPHONE || LINUX || WINDOWS
  89. LoadAssets();
  90. #endif
  91. // Start the game
  92. Start ();
  93. }
  94. public void LoadAssets ()
  95. {
  96. NetworkSession = ScreenManager.Game.Services.GetService (typeof(NetworkSession)) as NetworkSession;
  97. IsNetworking = NetworkSession != null;
  98. // Load textures
  99. foregroundTexture = Load<Texture2D> ("Textures/Backgrounds/gameplay_screen");
  100. cloud1Texture = Load<Texture2D> ("Textures/Backgrounds/cloud1");
  101. cloud2Texture = Load<Texture2D> ("Textures/Backgrounds/cloud2");
  102. mountainTexture = Load<Texture2D> ("Textures/Backgrounds/mountain");
  103. skyTexture = Load<Texture2D> ("Textures/Backgrounds/sky");
  104. defeatTexture = Load<Texture2D> ("Textures/Backgrounds/defeat");
  105. victoryTexture = Load<Texture2D> ("Textures/Backgrounds/victory");
  106. hudBackgroundTexture = Load<Texture2D> ("Textures/HUD/hudBackground");
  107. windArrowTexture = Load<Texture2D> ("Textures/HUD/windArrow");
  108. ammoTypeTexture = Load<Texture2D> ("Textures/HUD/ammoType");
  109. // Load font
  110. hudFont = Load<SpriteFont> ("Fonts/HUDFont");
  111. // Define initial cloud position
  112. cloud1Position = new Vector2 (224 - cloud1Texture.Width, 32);
  113. cloud2Position = new Vector2 (64, 90);
  114. // Define initial HUD positions
  115. playerOneHUDPosition = new Vector2 (7, 7);
  116. computerHUDPosition = new Vector2 (613, 7);
  117. windArrowPosition = new Vector2 (345, 46);
  118. // Initialize human & other players
  119. if (IsNetworking) {
  120. if (NetworkSession.RemoteGamers.Count > 0) {
  121. if (NetworkSession.IsHost) {
  122. playerOne = new Human (ScreenManager.Game, ScreenManager.SpriteBatch, PlayerSide.Left);
  123. playerOne.Initialize ();
  124. playerOne.Name = NetworkSession.LocalGamers [0].Gamertag + " (host)";
  125. playerTwo = new Human (ScreenManager.Game, ScreenManager.SpriteBatch, PlayerSide.Right);
  126. playerTwo.Initialize ();
  127. playerTwo.Name = NetworkSession.RemoteGamers [0].Gamertag;
  128. } else {
  129. playerOne = new Human (ScreenManager.Game, ScreenManager.SpriteBatch, PlayerSide.Left);
  130. playerOne.Initialize ();
  131. playerOne.Name = NetworkSession.RemoteGamers [0].Gamertag + " (host)";
  132. playerTwo = new Human (ScreenManager.Game, ScreenManager.SpriteBatch, PlayerSide.Right);
  133. playerTwo.Initialize ();
  134. playerTwo.Name = NetworkSession.LocalGamers [0].Gamertag;
  135. }
  136. }
  137. else {
  138. playerOne = new Human (ScreenManager.Game, ScreenManager.SpriteBatch, PlayerSide.Left);
  139. playerOne.Initialize ();
  140. playerOne.Name = NetworkSession.LocalGamers [0].Gamertag + " (host)";
  141. playerTwo = new Human (ScreenManager.Game, ScreenManager.SpriteBatch, PlayerSide.Right);
  142. playerTwo.Initialize ();
  143. playerTwo.Name = "Player 2";
  144. IsNetworking = false;
  145. NetworkSession.Dispose();
  146. NetworkSession = null;
  147. }
  148. } else {
  149. playerOne = new Human (ScreenManager.Game, ScreenManager.SpriteBatch, PlayerSide.Left);
  150. playerOne.Initialize ();
  151. playerOne.Name = "Player 1";
  152. playerTwo = new AI (ScreenManager.Game, ScreenManager.SpriteBatch);
  153. playerTwo.Initialize ();
  154. playerTwo.Name = "Player 2";
  155. }
  156. // Identify enemies
  157. playerOne.Enemy = playerTwo;
  158. playerTwo.Enemy = playerOne;
  159. }
  160. #endregion
  161. #region Update
  162. /// <summary>
  163. /// Runs one frame of update for the game.
  164. /// </summary>
  165. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  166. public override void Update (GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
  167. {
  168. float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  169. // Check it one of the players reached 5 and stop the game
  170. if ((playerOne.Catapult.GameOver || playerTwo.Catapult.GameOver) &&
  171. (gameOver == false)) {
  172. gameOver = true;
  173. if (IsNetworking) {
  174. if (NetworkSession.IsHost) {
  175. if (playerOne.Score > playerTwo.Score) {
  176. AudioManager.PlaySound ("gameOver_Win");
  177. } else {
  178. AudioManager.PlaySound ("gameOver_Lose");
  179. }
  180. }
  181. else {
  182. if (playerOne.Score > playerTwo.Score) {
  183. AudioManager.PlaySound ("gameOver_Lose");
  184. } else {
  185. AudioManager.PlaySound ("gameOver_Win");
  186. }
  187. }
  188. }
  189. else {
  190. if (playerOne.Score > playerTwo.Score) {
  191. AudioManager.PlaySound ("gameOver_Win");
  192. } else {
  193. AudioManager.PlaySound ("gameOver_Lose");
  194. }
  195. }
  196. return;
  197. }
  198. // If Reset flag raised and both catapults are not animating -
  199. // active catapult finished the cycle, new turn!
  200. if ((playerOne.Catapult.CurrentState == CatapultState.Reset ||
  201. playerTwo.Catapult.CurrentState == CatapultState.Reset) &&
  202. !(playerOne.Catapult.AnimationRunning ||
  203. playerTwo.Catapult.AnimationRunning)) {
  204. changeTurn = true;
  205. if (playerOne.IsActive == true) { //Last turn was a human turn?
  206. playerOne.IsActive = false;
  207. playerTwo.IsActive = true;
  208. isFirstPlayerTurn = false;
  209. playerOne.Catapult.CurrentState = CatapultState.Idle;
  210. if (playerTwo.IsAI)
  211. playerTwo.Catapult.CurrentState = CatapultState.Aiming;
  212. else
  213. playerTwo.Catapult.CurrentState = CatapultState.Idle;
  214. } else { //It was an AI turn
  215. playerOne.IsActive = true;
  216. playerTwo.IsActive = false;
  217. isFirstPlayerTurn = true;
  218. playerTwo.Catapult.CurrentState = CatapultState.Idle;
  219. playerOne.Catapult.CurrentState = CatapultState.Idle;
  220. }
  221. }
  222. if (changeTurn) {
  223. // Update wind
  224. wind = new Vector2 (random.Next (-1, 2), random.Next (minWind, maxWind + 1));
  225. // Set new wind value to the players and
  226. playerOne.Catapult.Wind = playerTwo.Catapult.Wind = wind.X > 0 ? wind.Y : -wind.Y;
  227. changeTurn = false;
  228. }
  229. // Update the players
  230. playerOne.Update (gameTime);
  231. playerTwo.Update (gameTime);
  232. // Updates the clouds position
  233. UpdateClouds (elapsed);
  234. UpdateNetworkSession ();
  235. base.Update (gameTime, otherScreenHasFocus, coveredByOtherScreen);
  236. }
  237. #endregion
  238. #region Draw
  239. /// <summary>
  240. /// Draw the game world, effects, and HUD
  241. /// </summary>
  242. /// <param name="gameTime">The elapsed time since last Draw</param>
  243. public override void Draw (GameTime gameTime)
  244. {
  245. ScreenManager.SpriteBatch.Begin ();
  246. // Render all parts of the screen
  247. DrawBackground ();
  248. DrawComputer (gameTime);
  249. DrawPlayer (gameTime);
  250. DrawHud ();
  251. ScreenManager.SpriteBatch.End ();
  252. }
  253. #endregion
  254. #region Input
  255. /// <summary>
  256. /// Input helper method provided by GameScreen. Packages up the various input
  257. /// values for ease of use.
  258. /// </summary>
  259. /// <param name="input">The state of the gamepads</param>
  260. public override void HandleInput (InputState input)
  261. {
  262. if (input == null)
  263. throw new ArgumentNullException ("input");
  264. if (gameOver) {
  265. if (input.IsPauseGame (null)) {
  266. FinishCurrentGame ();
  267. }
  268. if (input.MouseGesture.HasFlag(MouseGestureType.LeftClick))
  269. FinishCurrentGame();
  270. foreach (GestureSample gestureSample in input.Gestures) {
  271. if (gestureSample.GestureType == GestureType.Tap) {
  272. FinishCurrentGame ();
  273. }
  274. }
  275. return;
  276. }
  277. if (NetworkSession != null) {
  278. if ((isFirstPlayerTurn && !NetworkSession.IsHost)
  279. || (!isFirstPlayerTurn && NetworkSession.IsHost))
  280. return;
  281. }
  282. if (input.IsPauseGame (null)) {
  283. PauseCurrentGame ();
  284. } else if (isFirstPlayerTurn &&
  285. (playerOne.Catapult.CurrentState == CatapultState.Idle ||
  286. playerOne.Catapult.CurrentState == CatapultState.Aiming)) {
  287. // First we try with mouse input
  288. playerOne.HandleInput (input);
  289. isDragging = playerOne.isDragging;
  290. // Read all available gestures
  291. foreach (GestureSample gestureSample in input.Gestures) {
  292. if (gestureSample.GestureType == GestureType.FreeDrag)
  293. isDragging = true;
  294. else if (gestureSample.GestureType == GestureType.DragComplete)
  295. isDragging = false;
  296. playerOne.HandleInput (gestureSample);
  297. }
  298. } else if (!isFirstPlayerTurn &&
  299. (playerTwo.Catapult.CurrentState == CatapultState.Idle ||
  300. playerTwo.Catapult.CurrentState == CatapultState.Aiming)) {
  301. // First we try with mouse input
  302. playerTwo.HandleInput (input);
  303. isDragging = playerTwo.isDragging;
  304. // Read all available gestures
  305. foreach (GestureSample gestureSample in input.Gestures) {
  306. if (gestureSample.GestureType == GestureType.FreeDrag)
  307. isDragging = true;
  308. else if (gestureSample.GestureType == GestureType.DragComplete)
  309. isDragging = false;
  310. playerTwo.HandleInput (gestureSample);
  311. }
  312. }
  313. }
  314. Vector3 catapultInfoVector = Vector3.Zero;
  315. void UpdateNetworkSession ()
  316. {
  317. if (NetworkSession == null)
  318. return;
  319. if (NetworkSession.IsHost) {
  320. packetWriter.Write((int)MessageType.UpdateEnvironment);
  321. packetWriter.Write(wind);
  322. packetWriter.Write(cloud1Position);
  323. packetWriter.Write(cloud2Position);
  324. // Update our locally controlled tanks, and send their
  325. // latest position data to everyone in the session.
  326. foreach (LocalNetworkGamer gamer in NetworkSession.LocalGamers) {
  327. gamer.SendData(packetWriter, SendDataOptions.ReliableInOrder);
  328. }
  329. }
  330. if (playerOne.Catapult.CurrentState == CatapultState.Aiming) {
  331. SendCatapultInfo(MessageType.CatapultAiming, playerOne);
  332. }
  333. if (playerOne.Catapult.CurrentState == CatapultState.Firing) {
  334. SendCatapultInfo(MessageType.CatapultFiring, playerOne);
  335. }
  336. if (playerTwo.Catapult.CurrentState == CatapultState.Aiming) {
  337. SendCatapultInfo(MessageType.CatapultAiming, playerTwo);
  338. }
  339. if (playerTwo.Catapult.CurrentState == CatapultState.Firing) {
  340. SendCatapultInfo(MessageType.CatapultFiring, playerTwo);
  341. }
  342. // Pump the underlying session object.
  343. NetworkSession.Update ();
  344. // Make sure the session has not ended.
  345. if (NetworkSession == null)
  346. return;
  347. // Read any packets
  348. foreach (LocalNetworkGamer gamer in NetworkSession.LocalGamers) {
  349. ReadIncomingPackets (gamer);
  350. }
  351. }
  352. void SendCatapultInfo (MessageType messageType, Human player)
  353. {
  354. packetWriter.Write((int)messageType);
  355. catapultInfoVector.X = player.Catapult.ShotStrength;
  356. catapultInfoVector.Y = player.Catapult.ShotVelocity;
  357. catapultInfoVector.Z = player.ArrowScale;
  358. packetWriter.Write(catapultInfoVector);
  359. // Update our locally controlled catapults, and send their
  360. // latest position data to everyone in the session.
  361. foreach (LocalNetworkGamer gamer in NetworkSession.LocalGamers) {
  362. gamer.SendData(packetWriter, SendDataOptions.ReliableInOrder);
  363. }
  364. }
  365. /// <summary>
  366. /// Helper for reading incoming network packets.
  367. /// </summary>
  368. void ReadIncomingPackets (LocalNetworkGamer gamer)
  369. {
  370. // Keep reading as long as incoming packets are available.
  371. while (gamer.IsDataAvailable) {
  372. NetworkGamer sender;
  373. // Read a single packet from the network.
  374. gamer.ReceiveData (packetReader, out sender);
  375. // Discard packets sent by local gamers: we already know their state!
  376. if (sender.IsLocal)
  377. continue;
  378. MessageType msgType = (MessageType)packetReader.ReadInt32 ();
  379. switch (msgType) {
  380. case MessageType.NewGame:
  381. //ReceiveNewNetworkedGame();
  382. break;
  383. case MessageType.CatapultAiming:
  384. if (isFirstPlayerTurn && !NetworkSession.IsHost) {
  385. playerOne.Catapult.CurrentState = CatapultState.Aiming;
  386. playerOne.isDragging = true;
  387. catapultInfoVector = packetReader.ReadVector3();
  388. playerOne.Catapult.ShotStrength = catapultInfoVector.X;
  389. playerOne.Catapult.ShotVelocity = catapultInfoVector.Y;
  390. playerOne.ArrowScale = catapultInfoVector.Z;
  391. }
  392. if (!isFirstPlayerTurn && NetworkSession.IsHost) {
  393. playerTwo.Catapult.CurrentState = CatapultState.Aiming;
  394. playerTwo.isDragging = true;
  395. catapultInfoVector = packetReader.ReadVector3();
  396. playerTwo.Catapult.ShotStrength = catapultInfoVector.X;
  397. playerTwo.Catapult.ShotVelocity = catapultInfoVector.Y;
  398. playerTwo.ArrowScale = catapultInfoVector.Z;
  399. }
  400. break;
  401. case MessageType.CatapultFiring:
  402. if (isFirstPlayerTurn && !NetworkSession.IsHost) {
  403. catapultInfoVector = packetReader.ReadVector3();
  404. playerOne.Catapult.Fire (catapultInfoVector.Y);
  405. playerOne.Catapult.CurrentState = CatapultState.Firing;
  406. playerOne.ResetDragState();
  407. }
  408. if (!isFirstPlayerTurn && NetworkSession.IsHost) {
  409. catapultInfoVector = packetReader.ReadVector3();
  410. playerTwo.Catapult.Fire (catapultInfoVector.Y);
  411. playerTwo.Catapult.CurrentState = CatapultState.Firing;
  412. playerTwo.ResetDragState();
  413. }
  414. break;
  415. case MessageType.UpdateEnvironment:
  416. wind = packetReader.ReadVector2();
  417. cloud1Position = packetReader.ReadVector2();
  418. cloud2Position = packetReader.ReadVector2();
  419. // Set new wind value to the players and
  420. playerOne.Catapult.Wind = playerTwo.Catapult.Wind = wind.X > 0 ? wind.Y : -wind.Y;
  421. break;
  422. }
  423. }
  424. }
  425. #endregion
  426. #region Update Helpers
  427. private void UpdateClouds (float elapsedTime)
  428. {
  429. // Move the clouds according to the wind
  430. int windDirection = wind.X > 0 ? 1 : -1;
  431. cloud1Position += new Vector2 (24.0f, 0.0f) * elapsedTime *
  432. windDirection * wind.Y;
  433. if (cloud1Position.X > ScreenManager.GraphicsDevice.Viewport.Width)
  434. cloud1Position.X = -cloud1Texture.Width * 2.0f;
  435. else if (cloud1Position.X < -cloud1Texture.Width * 2.0f)
  436. cloud1Position.X = ScreenManager.GraphicsDevice.Viewport.Width;
  437. cloud2Position += new Vector2 (16.0f, 0.0f) * elapsedTime *
  438. windDirection * wind.Y;
  439. if (cloud2Position.X > ScreenManager.GraphicsDevice.Viewport.Width)
  440. cloud2Position.X = -cloud2Texture.Width * 2.0f;
  441. else if (cloud2Position.X < -cloud2Texture.Width * 2.0f)
  442. cloud2Position.X = ScreenManager.GraphicsDevice.Viewport.Width;
  443. }
  444. #endregion
  445. #region Draw Helpers
  446. /// <summary>
  447. /// Draws the player's catapult
  448. /// </summary>
  449. void DrawPlayer (GameTime gameTime)
  450. {
  451. if (!gameOver)
  452. playerOne.Draw (gameTime);
  453. }
  454. /// <summary>
  455. /// Draws the AI's catapult
  456. /// </summary>
  457. void DrawComputer (GameTime gameTime)
  458. {
  459. if (!gameOver)
  460. playerTwo.Draw (gameTime);
  461. }
  462. /// <summary>
  463. /// Draw the sky, clouds, mountains, etc.
  464. /// </summary>
  465. private void DrawBackground ()
  466. {
  467. // Clear the background
  468. ScreenManager.Game.GraphicsDevice.Clear (Color.White);
  469. // Draw the Sky
  470. ScreenManager.SpriteBatch.Draw (skyTexture, Vector2.Zero, Color.White);
  471. // Draw Cloud #1
  472. ScreenManager.SpriteBatch.Draw (cloud1Texture,
  473. cloud1Position, Color.White);
  474. // Draw the Mountain
  475. ScreenManager.SpriteBatch.Draw (mountainTexture,
  476. Vector2.Zero, Color.White);
  477. // Draw Cloud #2
  478. ScreenManager.SpriteBatch.Draw (cloud2Texture,
  479. cloud2Position, Color.White);
  480. // Draw the Castle, trees, and foreground
  481. ScreenManager.SpriteBatch.Draw (foregroundTexture,
  482. Vector2.Zero, Color.White);
  483. }
  484. /// <summary>
  485. /// Draw the HUD, which consists of the score elements and the GAME OVER tag.
  486. /// </summary>
  487. void DrawHud ()
  488. {
  489. if (gameOver) {
  490. Texture2D texture;
  491. if (IsNetworking) {
  492. if (NetworkSession.IsHost) {
  493. if (playerOne.Score > playerTwo.Score) {
  494. texture = victoryTexture;
  495. } else {
  496. texture = defeatTexture;
  497. }
  498. }
  499. else {
  500. if (playerOne.Score > playerTwo.Score) {
  501. texture = defeatTexture;
  502. } else {
  503. texture = victoryTexture;
  504. }
  505. }
  506. }
  507. else {
  508. if (playerOne.Score > playerTwo.Score) {
  509. texture = victoryTexture;
  510. } else {
  511. texture = defeatTexture;
  512. }
  513. }
  514. ScreenManager.SpriteBatch.Draw (texture,
  515. new Vector2 (ScreenManager.Game.GraphicsDevice.Viewport.Width / 2 - texture.Width / 2,
  516. ScreenManager.Game.GraphicsDevice.Viewport.Height / 2 - texture.Height / 2),
  517. Color.White);
  518. } else {
  519. // Draw Player Hud
  520. ScreenManager.SpriteBatch.Draw (hudBackgroundTexture, playerOneHUDPosition, Color.White);
  521. ScreenManager.SpriteBatch.Draw (ammoTypeTexture, playerOneHUDPosition + new Vector2 (33, 35), Color.White);
  522. DrawString (hudFont, playerOne.Score.ToString (), playerOneHUDPosition + new Vector2 (123, 35), Color.White);
  523. DrawString (hudFont, playerOne.Name, playerOneHUDPosition + new Vector2 (40, 1), Color.Blue);
  524. // Draw Computer Hud
  525. ScreenManager.SpriteBatch.Draw (hudBackgroundTexture, computerHUDPosition, Color.White);
  526. ScreenManager.SpriteBatch.Draw (ammoTypeTexture, computerHUDPosition + new Vector2 (33, 35), Color.White);
  527. DrawString (hudFont, playerTwo.Score.ToString (), computerHUDPosition + new Vector2 (123, 35), Color.White);
  528. DrawString (hudFont, playerTwo.Name, computerHUDPosition + new Vector2 (40, 1), Color.Red);
  529. // Draw Wind direction
  530. string text = "WIND";
  531. Vector2 size = hudFont.MeasureString (text);
  532. Vector2 windarrowScale = new Vector2 (wind.Y / 10, 1);
  533. ScreenManager.SpriteBatch.Draw (windArrowTexture,
  534. windArrowPosition, null, Color.White, 0, Vector2.Zero,
  535. windarrowScale, wind.X > 0 ? SpriteEffects.None : SpriteEffects.FlipHorizontally, 0);
  536. DrawString (hudFont, text, windArrowPosition - new Vector2 (0, size.Y), Color.Black);
  537. if (wind.Y == 0) {
  538. text = "NONE";
  539. DrawString (hudFont, text, windArrowPosition, Color.Black);
  540. }
  541. if (IsNetworking) {
  542. if (isFirstPlayerTurn) {
  543. if (NetworkSession.IsHost) {
  544. // Prepare player one prompt message
  545. text = !isDragging ? "Drag Anywhere to Fire" : "Release to Fire!";
  546. }
  547. else {
  548. text = "Waiting for " + playerOne.Name;
  549. }
  550. } else {
  551. if (NetworkSession.IsHost) {
  552. // Prepare player one prompt message
  553. text = "Waiting for " + playerTwo.Name;
  554. }
  555. else {
  556. text = !isDragging ? "Drag Anywhere to Fire" : "Release to Fire!";
  557. }
  558. }
  559. }
  560. else {
  561. if (isFirstPlayerTurn) {
  562. // Prepare player one prompt message
  563. text = !isDragging ? "Drag Anywhere to Fire" : "Release to Fire!";
  564. } else {
  565. // Prepare other player message
  566. text = "I'll get you yet!";
  567. }
  568. }
  569. size = hudFont.MeasureString (text);
  570. DrawString (hudFont, text,
  571. new Vector2 (ScreenManager.GraphicsDevice.Viewport.Width / 2 - size.X / 2,
  572. ScreenManager.GraphicsDevice.Viewport.Height - size.Y),
  573. Color.Green);
  574. }
  575. }
  576. /// <summary>
  577. /// A simple helper to draw shadowed text.
  578. /// </summary>
  579. void DrawString (SpriteFont font, string text, Vector2 position, Color color)
  580. {
  581. ScreenManager.SpriteBatch.DrawString (font, text,
  582. new Vector2 (position.X + 1, position.Y + 1), Color.Black);
  583. ScreenManager.SpriteBatch.DrawString (font, text, position, color);
  584. }
  585. /// <summary>
  586. /// A simple helper to draw shadowed text.
  587. /// </summary>
  588. void DrawString (SpriteFont font, string text, Vector2 position, Color color, float fontScale)
  589. {
  590. ScreenManager.SpriteBatch.DrawString (font, text, new Vector2 (position.X + 1,
  591. position.Y + 1), Color.Black, 0, new Vector2 (0, font.LineSpacing / 2),
  592. fontScale, SpriteEffects.None, 0);
  593. ScreenManager.SpriteBatch.DrawString (font, text, position, color, 0,
  594. new Vector2 (0, font.LineSpacing / 2), fontScale, SpriteEffects.None, 0);
  595. }
  596. #endregion
  597. #region Input Helpers
  598. /// <summary>
  599. /// Finish the current game
  600. /// </summary>
  601. private void FinishCurrentGame ()
  602. {
  603. ExitScreen ();
  604. }
  605. /// <summary>
  606. /// Pause the current game
  607. /// </summary>
  608. private void PauseCurrentGame ()
  609. {
  610. var pauseMenuBackground = new BackgroundScreen ();
  611. if (isDragging) {
  612. isDragging = false;
  613. playerOne.Catapult.CurrentState = CatapultState.Idle;
  614. }
  615. ScreenManager.AddScreen (pauseMenuBackground, null);
  616. ScreenManager.AddScreen (new PauseScreen (pauseMenuBackground, playerOne, playerTwo), null);
  617. }
  618. #endregion
  619. #region Gameplay Helpers
  620. /// <summary>
  621. /// Starts a new game session, setting all game states to initial values.
  622. /// </summary>
  623. void Start ()
  624. {
  625. // Set initial wind direction
  626. wind = Vector2.Zero;
  627. isFirstPlayerTurn = false;
  628. changeTurn = true;
  629. playerTwo.Catapult.CurrentState = CatapultState.Reset;
  630. }
  631. #endregion
  632. }
  633. }