PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/BasketDefense/BasketDefense/StateMenu.cs

https://bitbucket.org/phunter95/basketdefense
C# | 322 lines | 270 code | 32 blank | 20 comment | 43 complexity | 40c7b2873f46aadd658b6c4380ddad06 MD5 | raw file
  1. //---------------------------------------------------------------//
  2. // Basket Defense | StateMenu.cs---------------------------------//
  3. // By: Phillip Hunter--------------------------------------------//
  4. // Desc: State for the Main Menu.--------------------------------//
  5. //---------------------------------------------------------------//
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using Microsoft.Xna.Framework.Graphics;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Input;
  13. using System.Net;
  14. using BasketDefenseEditor;
  15. namespace BasketDefense
  16. {
  17. class StateMenu : State
  18. {
  19. //DEBUG
  20. private KeyboardState thaOldKeyboard;
  21. private Int32 mouseX = 0;
  22. private Int32 mouseY = 0;
  23. private guiButton[] buttons;
  24. public static Texture2D texTitleCard;
  25. private SpriteFont fntSplash;
  26. private SpriteFont fntVersion;
  27. #region Splash System
  28. private String[] splashes;
  29. private String currentSplash;
  30. private Random r = new Random();
  31. private Single splashMod = 1;
  32. private Int32 splashModDir = 1;
  33. private Color splashCol = Color.Yellow;
  34. private String randomSplash = "";
  35. private Boolean isRandomSplash;
  36. private Int32 maxSplashWidth;
  37. private Boolean splashIsBig = false;
  38. private Boolean useWeb = true;
  39. private static WebClient web = new WebClient();
  40. public static String netSplash = "";
  41. #endregion Splash System
  42. public override void Init()
  43. {
  44. Logger.log("State: " + this.ToString() + " was initialized.", 2);
  45. Program.game.IsMouseVisible = true;
  46. #region Use web
  47. if (useWeb)
  48. {
  49. try
  50. {
  51. netSplash = web.DownloadString("http://dl.dropbox.com/u/149155351/netSplash.txt");
  52. if (netSplash == "")
  53. {
  54. useWeb = false;
  55. }
  56. }
  57. catch (Exception)
  58. {
  59. useWeb = false;
  60. }
  61. }
  62. #endregion Use web
  63. #region Splashes
  64. splashes = new String[]
  65. {
  66. "Beware of ants!",
  67. "Lotsa Spaghetti!",
  68. "Stevie is the Dog!",
  69. "BLACK HOOOOOOOOOLE!!!",
  70. "It moves...IT MOVES!!!",
  71. "Baskets are not symmetrical!",
  72. "They're after your " + System.Environment.NewLine + "11 foot longs!",
  73. "Get to the helicopter," + System.Environment.NewLine + "Street Fighter!",
  74. "Who calls them 'gamepads'?!",
  75. "Subway. Eat Flesh.",
  76. "Stop with the clicking!",
  77. "Comic Sans, not Monospaced!",
  78. "Let the slapfights begin!",
  79. "SHAAAAAAAUUUUN!!!",
  80. "JAAAAASON!",
  81. "I'M SPEAKING!!!!",
  82. "Duel of the Fates!",
  83. "Clash of Heroes!",
  84. "Codename: " + System.Environment.NewLine + "Kitty Energy Drink",
  85. "Currently " + Game1.knownLines + " lines of code!",
  86. "John Madden!",
  87. "Stove was here!",
  88. "Breaaaaaaaaad...!",
  89. "The Original Yeti is " + System.Environment.NewLine + "out on a picnic!",
  90. "FIX IT",
  91. "There's a K in Kombat!!!",
  92. "Pokémon is one word!",
  93. "A wild Davidog has appeared!",
  94. "I know how to spell!",
  95. "Your food is bad and you " + System.Environment.NewLine + "should feel bad!",
  96. "Kill the enemy.",
  97. "KILL THE ENEMY!!!",
  98. "Baskets look like T.V.s!",
  99. randomSplash
  100. };
  101. if (useWeb)
  102. {
  103. currentSplash = netSplash;
  104. }
  105. else
  106. {
  107. getNewSplash();
  108. }
  109. if (currentSplash == randomSplash)
  110. {
  111. isRandomSplash = true;
  112. }
  113. #endregion
  114. buttons = new guiButton[]
  115. {
  116. new guiButton(1, "Play", new Vector2(210, 180)),
  117. new guiButton(1, "Quit", new Vector2(210, 280)),
  118. new guiButton(1, "Tutorial", new Vector2(210,230))
  119. };
  120. }
  121. public override void Update()
  122. {
  123. // Cycle buttons.
  124. for (int i = 0; i < buttons.Length; i++)
  125. {
  126. buttons[i].Update();
  127. }
  128. if (buttons[0].isButtonClicked()) //Play button
  129. {
  130. Program.game.sm.SetState(new StateMap1());
  131. }
  132. if (buttons[1].isButtonClicked()) //Exit button
  133. {
  134. Program.game.Exit();
  135. }
  136. if (buttons[2].isButtonClicked()) //Tutorial button
  137. {
  138. Program.game.sm.SetState(new StateTutorial());
  139. }
  140. #region Websplash
  141. if (useWeb && (Game1.tickCount % 300 == 0))
  142. {
  143. try
  144. {
  145. netSplash = web.DownloadString("http://dl.dropbox.com/u/149155351/netSplash.txt");
  146. if (netSplash == "")
  147. {
  148. getNewSplash();
  149. useWeb = false;
  150. }
  151. currentSplash = netSplash;
  152. }
  153. catch (Exception)
  154. {
  155. useWeb = false;
  156. }
  157. }
  158. #endregion Websplash
  159. // Run the "FIX IT" special splash.df
  160. if (currentSplash.Contains("FIX IT"))
  161. {
  162. if (Game1.tickCount % 15 == 0)
  163. {
  164. if (currentSplash.Length <= 100)
  165. {
  166. currentSplash += System.Environment.NewLine + "FIX IT";
  167. }
  168. }
  169. }
  170. //DEBUG: Disable tutorial.
  171. buttons[2].active = false;
  172. #region Splash System
  173. if (isRandomSplash)
  174. {
  175. randomSplash = "RANDOM!: " + r.Next(Int32.MaxValue).ToString();
  176. currentSplash = randomSplash;
  177. }
  178. if (splashMod >= 5)
  179. {
  180. splashModDir = 0;
  181. }
  182. if (splashMod <= -5)
  183. {
  184. splashModDir = 1;
  185. }
  186. if (splashModDir == 0)
  187. {
  188. splashMod -= 0.2f;
  189. }
  190. else
  191. {
  192. splashMod += 0.2f;
  193. }
  194. if (splashMod < 1)
  195. {
  196. fntSplash.Spacing = 0;
  197. }
  198. else
  199. {
  200. fntSplash.Spacing = Math.Abs(splashMod - 4);
  201. }
  202. #endregion Splash System
  203. }
  204. public override void LoadContent()
  205. {
  206. texTitleCard = Program.game.LoadTexture("gui/titleCard");
  207. fntSplash = Program.game.LoadFont("fntSplash");
  208. fntVersion = Program.game.LoadFont("StageTitle");
  209. // Set the tested maxSplashWidth for the auto rotate system
  210. // (tests if the splash is too wide to fit. Not fullproof).
  211. maxSplashWidth = (int)(fntSplash.MeasureString("AAAAAAAAAAAAAAAAAB").X);
  212. // Load content for each button.
  213. for (int i = 0; i < buttons.Length; i++)
  214. {
  215. buttons[i].LoadContent();
  216. }
  217. }
  218. public override void UnloadContent()
  219. {
  220. }
  221. public override void Input(KeyboardState thaKeyboard, MouseState thaMouse)
  222. {
  223. mouseX = thaMouse.X;
  224. mouseY = thaMouse.Y;
  225. // Cycle buttons.
  226. for (int i = 0; i < buttons.Length; i++)
  227. {
  228. buttons[i].Input(thaMouse);
  229. }
  230. //DEBUG Get new splash if D and Space are pressed simultaneously.
  231. if ((thaKeyboard.IsKeyDown(Keys.D)) && (InputManager.IsKeyPressed(Keys.Space, thaKeyboard, thaOldKeyboard)))
  232. {
  233. if (StateMenu.netSplash == "Cheats!")
  234. {
  235. getNewSplash();
  236. }
  237. }
  238. if ((thaKeyboard.IsKeyDown(Keys.D)) && (InputManager.IsKeyPressed(Keys.E, thaKeyboard, thaOldKeyboard)))
  239. {
  240. if (StateMenu.netSplash == "Cheats!")
  241. {
  242. Editor editor = new Editor();
  243. editor.Show();
  244. }
  245. }
  246. thaOldKeyboard = thaKeyboard;
  247. }
  248. public override void Draw(SpriteBatch spriteBatch)
  249. {
  250. // Draw background.
  251. spriteBatch.Draw(texTitleCard, new Rectangle(0, 0, Game1.GAME_WIDTH, Game1.GAME_HEIGHT), Color.White);
  252. spriteBatch.DrawString(fntVersion, "V" + Game1.GAME_VERSION, new Vector2(Game1.GAME_WIDTH - fntVersion.MeasureString("V" + Game1.GAME_VERSION).X, Game1.GAME_HEIGHT - fntVersion.MeasureString("V" + Game1.GAME_VERSION).Y),Color.White);
  253. // Draw splash.
  254. if (splashIsBig == true)
  255. {
  256. spriteBatch.DrawString(fntSplash, currentSplash, new Vector2(440, 90 + splashMod), splashCol, 0.1f, new Vector2(0, 0), 1, SpriteEffects.None, 0);
  257. }
  258. else
  259. {
  260. spriteBatch.DrawString(fntSplash, currentSplash, new Vector2(480, 90 + splashMod), splashCol, 0.2f, new Vector2(0, 0), 1, SpriteEffects.None, 0);
  261. }
  262. //Cycle buttons.
  263. for (int i = 0; i < buttons.Length; i++)
  264. {
  265. buttons[i].Draw(spriteBatch);
  266. }
  267. }
  268. public override void DeInit()
  269. {
  270. }
  271. private void getNewSplash()
  272. {
  273. // Set the current splash to a random splash.
  274. currentSplash = splashes[r.Next(splashes.Length)];
  275. if (fntSplash != null) //TODO: fix, make run after LoadContent
  276. {
  277. // If splash is too big.
  278. if ((int)(fntSplash.MeasureString(currentSplash).X) > maxSplashWidth)
  279. {
  280. splashIsBig = true;
  281. }
  282. }
  283. // If the splash is empty, get a new splash.
  284. if (currentSplash == "")
  285. {
  286. getNewSplash();
  287. }
  288. }
  289. }
  290. }