ShipUpgradeScreen.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ShipUpgradeScreen.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.Text;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework;
  15. using System.Diagnostics;
  16. using Microsoft.Xna.Framework.Audio;
  17. #endregion
  18. namespace Spacewar
  19. {
  20. /// <summary>
  21. /// ShipUpgradeScreen handles the screen that allows either user to select weapon upgrades
  22. /// </summary>
  23. public class ShipUpgradeScreen : Screen
  24. {
  25. private static string upgradeTexture = @"textures\weapon_select_FINAL";
  26. private Vector4 white = new Vector4(1f, 1f, 1f, 1f);
  27. private Vector4 upgradeFontColor = new Vector4(.882f, .596f, .286f, 1f);
  28. private const int countSpeed = 3;
  29. private bool player1Ready = false;
  30. private bool player2Ready = false;
  31. private bool playingTallySound = false;
  32. private Cue tallySound;
  33. private Cue menuMusic;
  34. private static string[] scoreLookup = new string[] { "000", "001", "011", "111" };
  35. private int[] playerCashCount = new int[] { 0, 0 };
  36. private float[] flashPercent = new float[2]; //Amount of flash on a selected weapon
  37. private TimeSpan[] flashEndTime = new TimeSpan[2]; //How long left to flash
  38. private TimeSpan flashTime = new TimeSpan(0, 0, 3);
  39. private SceneItem[] weapons = new SceneItem[2];
  40. private ProjectileType[] purchasedWeapon = new ProjectileType[] { ProjectileType.Peashooter, ProjectileType.Peashooter };
  41. private Vector2[,] weaponPositions = new Vector2[,]
  42. {
  43. {
  44. new Vector2(248, 133),
  45. new Vector2(75, 314),
  46. new Vector2(444, 314),
  47. new Vector2(248, 501),
  48. },
  49. {
  50. new Vector2(864, 133),
  51. new Vector2(686, 314),
  52. new Vector2(1055, 314),
  53. new Vector2(864, 501),
  54. }
  55. };
  56. /// <summary>
  57. /// Creates the ShipUpgradeScreen
  58. /// </summary>
  59. public ShipUpgradeScreen(Game game)
  60. : base(game)
  61. {
  62. //Play the menu music
  63. menuMusic = Sound.Play(Sounds.MenuMusic);
  64. weapons[0] = new SceneItem(game, new EvolvedShape(game, EvolvedShapes.Weapon, PlayerIndex.One, (int)ProjectileType.Peashooter, LightingType.Menu), new Vector3(-170, -30, 0));
  65. weapons[0].Scale = new Vector3(.06f, .06f, .06f);
  66. scene.Add(weapons[0]);
  67. weapons[1] = new SceneItem(game, new EvolvedShape(game, EvolvedShapes.Weapon, PlayerIndex.Two, (int)ProjectileType.Peashooter, LightingType.Menu), new Vector3(170, -30, 0));
  68. weapons[1].Scale = new Vector3(.06f, .06f, .06f);
  69. scene.Add(weapons[1]);
  70. }
  71. /// <summary>
  72. /// ShipUpgrade update will change the gamestate and start the next level when either
  73. /// 1. The upgrade timer expires
  74. /// 2. Both users have pressed A for ready
  75. /// </summary>
  76. /// <param name="time">Total game time since it was started</param>
  77. /// <param name="elapsedTime">Elapsed game time since the last call to update</param>
  78. /// <returns></returns>
  79. public override GameState Update(TimeSpan time, TimeSpan elapsedTime)
  80. {
  81. if ((XInputHelper.GamePads[PlayerIndex.One].APressed) || (!XInputHelper.GamePads[PlayerIndex.One].State.IsConnected && SpacewarGame.CurrentPlatform != PlatformID.Win32NT))
  82. player1Ready = true;
  83. if ((XInputHelper.GamePads[PlayerIndex.Two].APressed) || (!XInputHelper.GamePads[PlayerIndex.Two].State.IsConnected && SpacewarGame.CurrentPlatform != PlatformID.Win32NT))
  84. player2Ready = true;
  85. if (XInputHelper.GamePads[PlayerIndex.One].BPressed)
  86. player1Ready = false;
  87. if (XInputHelper.GamePads[PlayerIndex.Two].BPressed)
  88. player2Ready = false;
  89. //DPAD purchases weapons you can afford
  90. for (int player = 0; player < 2; player++)
  91. {
  92. //Can only buy weapons if you are not ready
  93. if ((!player1Ready && player == 0) || (!player2Ready && player == 1))
  94. {
  95. for (int weapon = 1; weapon < 5; weapon++)
  96. {
  97. //Associate weapon selection with dpad direction
  98. bool dpadPressed = false;
  99. switch (weapon)
  100. {
  101. case 1:
  102. dpadPressed = XInputHelper.GamePads[(PlayerIndex)player].UpPressed;
  103. break;
  104. case 2:
  105. dpadPressed = XInputHelper.GamePads[(PlayerIndex)player].LeftPressed;
  106. break;
  107. case 3:
  108. dpadPressed = XInputHelper.GamePads[(PlayerIndex)player].RightPressed;
  109. break;
  110. case 4:
  111. dpadPressed = XInputHelper.GamePads[(PlayerIndex)player].DownPressed;
  112. break;
  113. }
  114. //If a dpad is selected and you have enough money then purchase that weapon
  115. if (dpadPressed && SpacewarGame.Players[player].Cash >= SpacewarGame.Settings.Weapons[weapon].Cost)
  116. {
  117. Sound.PlayCue(Sounds.MenuAdvance);
  118. purchasedWeapon[player] = (ProjectileType)weapon;
  119. SpacewarGame.Players[player].Cash -= SpacewarGame.Settings.Weapons[weapon].Cost;
  120. SpacewarGame.Players[player].ProjectileType = (ProjectileType)weapon;
  121. weapons[player].ShapeItem = new EvolvedShape(GameInstance, EvolvedShapes.Weapon, (PlayerIndex)player, weapon, LightingType.Menu);
  122. flashEndTime[player] = time + flashTime;
  123. }
  124. }
  125. }
  126. }
  127. //Make the totals 'count'
  128. bool AnyPlayersCounting = false;
  129. for (int player = 0; player < 2; player++)
  130. {
  131. //If the count doesn't match the total
  132. if (playerCashCount[player] != SpacewarGame.Players[player].Cash)
  133. {
  134. AnyPlayersCounting = true;
  135. if (!playingTallySound)
  136. {
  137. tallySound = Sound.Play(Sounds.PointsTally);
  138. playingTallySound = true;
  139. }
  140. //Then move the count towards it
  141. int countDirection = Math.Sign(SpacewarGame.Players[player].Cash - playerCashCount[player]);
  142. playerCashCount[player] += (int)(elapsedTime.TotalMilliseconds * (double)countSpeed * (double)countDirection);
  143. //Ensure we don't go past the actual count
  144. if (countDirection == 1)
  145. {
  146. playerCashCount[player] = Math.Min(playerCashCount[player], SpacewarGame.Players[player].Cash);
  147. }
  148. else
  149. {
  150. playerCashCount[player] = Math.Max(playerCashCount[player], SpacewarGame.Players[player].Cash);
  151. }
  152. }
  153. }
  154. if (!AnyPlayersCounting && playingTallySound)
  155. {
  156. Sound.Stop(tallySound);
  157. playingTallySound = false;
  158. }
  159. //Spin the weapon
  160. for (int i = 0; i < 2; i++)
  161. {
  162. //(i * 2 -1) makes player 2 spin the other way
  163. weapons[i].Rotation = new Vector3(-.3f, (float)time.TotalSeconds * (i * 2 - 1), 0);
  164. }
  165. //Flash the select icon
  166. for (int player = 0; player < 2; player++)
  167. {
  168. if (flashEndTime[player] > time)
  169. {
  170. flashPercent[player] = (float)(Math.Sin((time - flashEndTime[player]).TotalSeconds * 40) * .5 + 1);
  171. }
  172. else
  173. {
  174. //Fully on
  175. flashPercent[player] = 1.0f;
  176. }
  177. }
  178. //Don't forget to call the base class
  179. base.Update(time, elapsedTime);
  180. //Play the next level when both players are ready
  181. if (player1Ready && player2Ready)
  182. {
  183. //Copy over the purchased weapons
  184. for (int i = 0; i < 2; i++)
  185. {
  186. SpacewarGame.Players[i].ProjectileType = purchasedWeapon[i];
  187. }
  188. Shutdown();
  189. return GameState.PlayEvolved;
  190. }
  191. else
  192. {
  193. return GameState.None;
  194. }
  195. }
  196. /// <summary>
  197. /// Tidy up anything that needs to be tidied
  198. /// </summary>
  199. public override void Shutdown()
  200. {
  201. //Silence any looped sounds
  202. if (playingTallySound)
  203. Sound.Stop(tallySound);
  204. Sound.Stop(menuMusic);
  205. base.Shutdown();
  206. }
  207. /// <summary>
  208. /// Renders this The ship upgrade screen. Current money and settings come from the static SpacewarGame object
  209. /// </summary>
  210. public override void Render()
  211. {
  212. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  213. GraphicsDevice device = graphicsService.GraphicsDevice;
  214. Texture2D mainTexture = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + upgradeTexture);
  215. SpriteBatch.Begin(SpriteSortMode.Texture, BlendState.Opaque);
  216. //Sprites will always be at the back.
  217. device.DepthStencilState = DepthStencilState.DepthRead;
  218. //Main background
  219. SpriteBatch.Draw(mainTexture, Vector2.Zero, new Rectangle(0, 0, 1280, 720), Color.White);
  220. SpriteBatch.End();
  221. //New sprite to ensure they are drawn on top
  222. SpriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend);
  223. for (int player = 0; player < 2; player++)
  224. {
  225. for (int weapon = 1; weapon < 5; weapon++)
  226. {
  227. //Grey out weapons when you are ready
  228. if ((player1Ready && player == 0) || (player2Ready && player == 1))
  229. {
  230. SpriteBatch.Draw(mainTexture, weaponPositions[player, weapon - 1], readySprite(player, weapon), Color.White);
  231. }
  232. //Show purchased weapon
  233. else if ((int)purchasedWeapon[player] == weapon)
  234. {
  235. SpriteBatch.Draw(mainTexture, weaponPositions[player, weapon - 1], selectedSprite(player, weapon), new Color(new Vector4(1f, 1f, 1f, flashPercent[player])));
  236. }
  237. //Disable weapons you can't afford
  238. else if (SpacewarGame.Players[player].Cash < SpacewarGame.Settings.Weapons[weapon].Cost)
  239. {
  240. SpriteBatch.Draw(mainTexture, weaponPositions[player, weapon - 1], disabledSprite(player, weapon), Color.White);
  241. }
  242. }
  243. }
  244. //Ready buttons
  245. if (player1Ready)
  246. SpriteBatch.Draw(mainTexture, new Vector2(55, 620), new Rectangle(10, 1205, 190, 70), Color.White);
  247. if (player2Ready)
  248. SpriteBatch.Draw(mainTexture, new Vector2(1040, 620), new Rectangle(330, 1205, 190, 70), Color.White);
  249. SpriteBatch.End();
  250. Font.Begin(); //Could reuse the sprite above but things may be drawn in the wrong order
  251. //Current cash and weapon costs
  252. //Ensure we use US number formatting
  253. for (int i = 0; i < 621; i += 620)
  254. {
  255. if ((!player1Ready && i == 0) || (!player2Ready && i == 620))
  256. {
  257. Font.Draw(FontStyle.WeaponSmall, 296 + i, 255, String.Format("{0:$##,##0}", SpacewarGame.Settings.Weapons[1].Cost), upgradeFontColor);
  258. Font.Draw(FontStyle.WeaponSmall, 110 + i, 438, String.Format("{0:$##,##0}", SpacewarGame.Settings.Weapons[2].Cost), upgradeFontColor);
  259. Font.Draw(FontStyle.WeaponSmall, 480 + i, 438, String.Format("{0:$##,##0}", SpacewarGame.Settings.Weapons[3].Cost), upgradeFontColor);
  260. Font.Draw(FontStyle.WeaponSmall, 296 + i, 621, String.Format("{0:$##,##0}", SpacewarGame.Settings.Weapons[4].Cost), upgradeFontColor);
  261. }
  262. }
  263. Font.Draw(FontStyle.WeaponLarge, 322, 40, "$", upgradeFontColor);
  264. Font.Draw(FontStyle.WeaponLarge, 346, 40, String.Format("{0:##,##0}", playerCashCount[0]), upgradeFontColor);
  265. Font.Draw(FontStyle.WeaponLarge, 840, 40, "$", upgradeFontColor);
  266. Font.Draw(FontStyle.WeaponLarge, 866, 40, String.Format("{0:##,##0}", playerCashCount[1]), upgradeFontColor);
  267. //Score buttons
  268. Font.Draw(FontStyle.ScoreButtons, 60 + 349, 70 + 24, scoreLookup[SpacewarGame.Players[0].Score]);
  269. Font.Draw(FontStyle.ScoreButtons, 1140 - 351, 70 + 24, scoreLookup[SpacewarGame.Players[1].Score]);
  270. Font.End();
  271. base.Render();
  272. }
  273. private Rectangle selectedSprite(int player, int weapon)
  274. {
  275. return new Rectangle(10 + 320 * (weapon - 1), 730 + player * 160, 150, 150);
  276. }
  277. private Rectangle disabledSprite(int player, int weapon)
  278. {
  279. return new Rectangle(170 + 320 * (weapon - 1), 730 + player * 160, 150, 150);
  280. }
  281. private Rectangle readySprite(int player, int weapon)
  282. {
  283. return new Rectangle(10 + 160 * (weapon - 1) + 640 * player, 1050, 150, 150);
  284. }
  285. public override void OnCreateDevice()
  286. {
  287. base.OnCreateDevice();
  288. weapons[0].ShapeItem.OnCreateDevice();
  289. weapons[1].ShapeItem.OnCreateDevice();
  290. }
  291. }
  292. }