SelectionScreen.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SelectionScreen.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;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using Microsoft.Xna.Framework.Audio;
  16. #endregion
  17. namespace Spacewar
  18. {
  19. /// <summary>
  20. /// Represents the ship selection screen
  21. /// </summary>
  22. public class SelectionScreen : Screen
  23. {
  24. private static string selectionTexture = @"textures\ship_select_FINAL";
  25. private Vector4 white = new Vector4(1f, 1f, 1f, 1f);
  26. private SceneItem[] ships = new SceneItem[2];
  27. private int[] selectedShip = new int[] { 0, 0 };
  28. private int[] selectedSkin = new int[] { 0, 0 };
  29. private bool player1Ready = false;
  30. private bool player2Ready = false;
  31. private Cue menuMusic;
  32. /// <summary>
  33. /// Creates a new selection screen. Plays the music and initializes the models
  34. /// </summary>
  35. public SelectionScreen(Game game)
  36. : base(game)
  37. {
  38. //Start menu music
  39. menuMusic = Sound.Play(Sounds.MenuMusic);
  40. ships[0] = new SceneItem(game, new EvolvedShape(game, EvolvedShapes.Ship, PlayerIndex.One, selectedShip[0], selectedSkin[0], LightingType.Menu), new Vector3(-120, 0, 0));
  41. ships[0].Scale = new Vector3(.05f, .05f, .05f);
  42. scene.Add(ships[0]);
  43. ships[1] = new SceneItem(game, new EvolvedShape(game, EvolvedShapes.Ship, PlayerIndex.Two, selectedShip[1], selectedSkin[1], LightingType.Menu), new Vector3(120, 0, 0));
  44. ships[1].Scale = new Vector3(.05f, .05f, .05f);
  45. scene.Add(ships[1]);
  46. }
  47. /// <summary>
  48. /// Updates the scene, handles the input
  49. /// </summary>
  50. /// <param name="time">Current game time</param>
  51. /// <param name="elapsedTime">Elapsed time since last update</param>
  52. /// <returns>New gamestate if required or GameState.None</returns>
  53. public override GameState Update(TimeSpan time, TimeSpan elapsedTime)
  54. {
  55. //A button makes a player ready. B button makes a player not ready
  56. if ((XInputHelper.GamePads[PlayerIndex.One].APressed) || (!XInputHelper.GamePads[PlayerIndex.One].State.IsConnected && SpacewarGame.CurrentPlatform != PlatformID.Win32NT))
  57. player1Ready = true;
  58. if ((XInputHelper.GamePads[PlayerIndex.Two].APressed) || (!XInputHelper.GamePads[PlayerIndex.Two].State.IsConnected && SpacewarGame.CurrentPlatform != PlatformID.Win32NT))
  59. player2Ready = true;
  60. if (XInputHelper.GamePads[PlayerIndex.One].BPressed)
  61. player1Ready = false;
  62. if (XInputHelper.GamePads[PlayerIndex.Two].BPressed)
  63. player2Ready = false;
  64. for (int player = 0; player < 2; player++)
  65. {
  66. if ((!player1Ready && player == 0) || (!player2Ready && player == 1))
  67. {
  68. int ship = selectedShip[player];
  69. int skin = selectedSkin[player];
  70. if (XInputHelper.GamePads[(PlayerIndex)player].UpPressed)
  71. selectedShip[player] += 5; //Wrap around
  72. if (XInputHelper.GamePads[(PlayerIndex)player].DownPressed)
  73. selectedShip[player]++;
  74. if (XInputHelper.GamePads[(PlayerIndex)player].LeftPressed)
  75. selectedSkin[player] += 5; //This will wraparound
  76. if (XInputHelper.GamePads[(PlayerIndex)player].RightPressed)
  77. selectedSkin[player]++;
  78. //Make selections wrap around
  79. selectedShip[player] = selectedShip[player] % 3;
  80. selectedSkin[player] = selectedSkin[player] % 3;
  81. //If anything's change then load the new ship/skin
  82. if ((ship != selectedShip[player]) || (skin != selectedSkin[player]))
  83. {
  84. Sound.PlayCue(Sounds.MenuScroll);
  85. ships[player].ShapeItem = new EvolvedShape(GameInstance, EvolvedShapes.Ship, (PlayerIndex)player, selectedShip[player], selectedSkin[player], LightingType.Menu);
  86. }
  87. }
  88. }
  89. //Spin the Ships
  90. for (int i = 0; i < 2; i++)
  91. {
  92. //(i * 2 -1) makes player 2 spin the other way
  93. ships[i].Rotation = new Vector3(-.3f, (float)time.TotalSeconds * (i * 2 - 1), 0);
  94. }
  95. //Update the Scene
  96. base.Update(time, elapsedTime);
  97. //Play the next level when both players are ready
  98. if (player1Ready && player2Ready)
  99. {
  100. //Set global ship and skins
  101. for (int i = 0; i < 2; i++)
  102. {
  103. SpacewarGame.Players[i].ShipClass = (ShipClass)selectedShip[i];
  104. SpacewarGame.Players[i].Skin = selectedSkin[i];
  105. }
  106. Shutdown();
  107. return GameState.PlayEvolved;
  108. }
  109. else
  110. {
  111. return GameState.None;
  112. }
  113. }
  114. /// <summary>
  115. /// Tidy up anything that needs tidying
  116. /// </summary>
  117. public override void Shutdown()
  118. {
  119. //Stop menu music
  120. Sound.Stop(menuMusic);
  121. base.Shutdown();
  122. }
  123. /// <summary>
  124. /// Renders the screen
  125. /// </summary>
  126. public override void Render()
  127. {
  128. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameInstance.Services.GetService(typeof(IGraphicsDeviceService));
  129. GraphicsDevice device = graphicsService.GraphicsDevice;
  130. Texture2D mainTexture = SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + selectionTexture);
  131. SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Opaque);
  132. //Sprites will always be at the back.
  133. device.DepthStencilState = DepthStencilState.DepthRead;
  134. //Main background
  135. SpriteBatch.Draw(mainTexture, Vector2.Zero, new Rectangle(0, 0, 1280, 720), Color.White);
  136. SpriteBatch.End();
  137. //New sprite to ensure they are drawn on top
  138. SpriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend);
  139. //Ready buttons
  140. if (player1Ready)
  141. {
  142. SpriteBatch.Draw(mainTexture, new Vector2(50, 610), new Rectangle(960, 1095, 190, 80), Color.White);
  143. //grey out ships & skins
  144. SpriteBatch.Draw(mainTexture, new Vector2(10, 127), new Rectangle(594, 911, 290, 112), Color.White);
  145. SpriteBatch.Draw(mainTexture, new Vector2(10, 239), new Rectangle(594, 1050, 290, 180), Color.White);
  146. SpriteBatch.Draw(mainTexture, new Vector2(10, 419), new Rectangle(10, 1200, 290, 140), Color.White);
  147. SpriteBatch.Draw(mainTexture, new Vector2(331, 573), new Rectangle(960, 911, 68, 70), Color.White);
  148. SpriteBatch.Draw(mainTexture, new Vector2(399, 573), new Rectangle(1040, 911, 68, 70), Color.White);
  149. SpriteBatch.Draw(mainTexture, new Vector2(467, 573), new Rectangle(1120, 911, 68, 70), Color.White);
  150. }
  151. else
  152. {
  153. //P1 ship & skins
  154. switch (selectedShip[0])
  155. {
  156. case 0:
  157. SpriteBatch.Draw(mainTexture, new Vector2(10, 127), new Rectangle(10, 730, 290, 112), Color.White);
  158. break;
  159. case 1:
  160. SpriteBatch.Draw(mainTexture, new Vector2(10, 239), new Rectangle(10, 860, 290, 180), Color.White);
  161. break;
  162. case 2:
  163. SpriteBatch.Draw(mainTexture, new Vector2(10, 419), new Rectangle(10, 1050, 290, 140), Color.White);
  164. break;
  165. }
  166. switch (selectedSkin[0])
  167. {
  168. case 0:
  169. SpriteBatch.Draw(mainTexture, new Vector2(331, 573), new Rectangle(960, 730, 68, 70), Color.White);
  170. break;
  171. case 1:
  172. SpriteBatch.Draw(mainTexture, new Vector2(399, 573), new Rectangle(1040, 730, 68, 70), Color.White);
  173. break;
  174. case 2:
  175. SpriteBatch.Draw(mainTexture, new Vector2(467, 573), new Rectangle(1120, 730, 68, 70), Color.White);
  176. break;
  177. }
  178. }
  179. if (player2Ready)
  180. {
  181. SpriteBatch.Draw(mainTexture, new Vector2(1040, 610), new Rectangle(960, 1200, 190, 80), Color.White);
  182. //grey out ships & skins
  183. SpriteBatch.Draw(mainTexture, new Vector2(960, 127), new Rectangle(594, 1290, 290, 112), Color.White);
  184. SpriteBatch.Draw(mainTexture, new Vector2(1040, 239), new Rectangle(332, 1050, 225, 180), Color.White);
  185. SpriteBatch.Draw(mainTexture, new Vector2(1040, 419), new Rectangle(331, 1290, 225, 140), Color.White);
  186. SpriteBatch.Draw(mainTexture, new Vector2(745, 573), new Rectangle(960, 1000, 68, 70), Color.White);
  187. SpriteBatch.Draw(mainTexture, new Vector2(813, 573), new Rectangle(1040, 1000, 68, 70), Color.White);
  188. SpriteBatch.Draw(mainTexture, new Vector2(881, 573), new Rectangle(1120, 1000, 68, 70), Color.White);
  189. }
  190. else
  191. {
  192. //p2 ships & skins
  193. switch (selectedShip[1])
  194. {
  195. case 0:
  196. SpriteBatch.Draw(mainTexture, new Vector2(960, 127), new Rectangle(331, 730, 290, 112), Color.White);
  197. break;
  198. case 1:
  199. SpriteBatch.Draw(mainTexture, new Vector2(1040, 239), new Rectangle(331, 860, 225, 180), Color.White);
  200. break;
  201. case 2:
  202. SpriteBatch.Draw(mainTexture, new Vector2(1040, 419), new Rectangle(700, 730, 225, 140), Color.White);
  203. break;
  204. }
  205. switch (selectedSkin[1])
  206. {
  207. case 0:
  208. SpriteBatch.Draw(mainTexture, new Vector2(745, 573), new Rectangle(960, 820, 68, 70), Color.White);
  209. break;
  210. case 1:
  211. SpriteBatch.Draw(mainTexture, new Vector2(813, 573), new Rectangle(1040, 820, 68, 70), Color.White);
  212. break;
  213. case 2:
  214. SpriteBatch.Draw(mainTexture, new Vector2(881, 573), new Rectangle(1120, 820, 68, 70), Color.White);
  215. break;
  216. }
  217. }
  218. SpriteBatch.End();
  219. //Ship names
  220. Font.Begin();
  221. Font.Draw(FontStyle.ShipNames, 331, 500, selectedShip[0].ToString(), new Vector4(.2f, .89f, 1, 1));
  222. Font.Draw(FontStyle.ShipNames, 745, 500, selectedShip[1].ToString(), new Vector4(1, .733f, .392f, 1));
  223. Font.End();
  224. base.Render();
  225. }
  226. public override void OnCreateDevice()
  227. {
  228. base.OnCreateDevice();
  229. ships[0].ShapeItem.OnCreateDevice();
  230. ships[1].ShapeItem.OnCreateDevice();
  231. }
  232. }
  233. }