RobotRampageGame.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. //using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. namespace RobotRampage
  12. {
  13. /// <summary>
  14. /// This is the main type for your game
  15. /// </summary>
  16. public class RobotRampageGame : Game
  17. {
  18. GraphicsDeviceManager graphics;
  19. SpriteBatch spriteBatch;
  20. Texture2D spriteSheet;
  21. Texture2D titleScreen;
  22. SpriteFont pericles14;
  23. enum GameStates { TitleScreen, Playing, WaveComplete, GameOver };
  24. GameStates gameState = GameStates.TitleScreen;
  25. float gameOverTimer = 0.0f;
  26. float gameOverDelay = 6.0f;
  27. float waveCompleteTimer = 0.0f;
  28. float waveCompleteDelay = 6.0f;
  29. public RobotRampageGame()
  30. {
  31. graphics = new GraphicsDeviceManager(this);
  32. Content.RootDirectory = "Content";
  33. }
  34. /// <summary>
  35. /// Allows the game to perform any initialization it needs to before starting to run.
  36. /// This is where it can query for any required services and load any non-graphic
  37. /// related content. Calling base.Initialize will enumerate through any components
  38. /// and initialize them as well.
  39. /// </summary>
  40. protected override void Initialize()
  41. {
  42. // TODO: Add your initialization logic here
  43. this.graphics.PreferredBackBufferWidth = 800;
  44. this.graphics.PreferredBackBufferHeight = 600;
  45. this.graphics.ApplyChanges();
  46. base.Initialize();
  47. }
  48. /// <summary>
  49. /// LoadContent will be called once per game and is the place to load
  50. /// all of your content.
  51. /// </summary>
  52. protected override void LoadContent()
  53. {
  54. // Create a new SpriteBatch, which can be used to draw textures.
  55. spriteBatch = new SpriteBatch(GraphicsDevice);
  56. spriteSheet = Content.Load<Texture2D>(@"Textures\SpriteSheet");
  57. titleScreen = Content.Load<Texture2D>(@"Textures\TitleScreen");
  58. pericles14 = Content.Load<SpriteFont>(@"Fonts\Pericles14");
  59. Camera.WorldRectangle = new Rectangle(0, 0, 1600, 1600);
  60. Camera.ViewPortWidth = 800;
  61. Camera.ViewPortHeight = 600;
  62. TileMap.Initialize(spriteSheet);
  63. Player.Initialize(
  64. spriteSheet,
  65. new Rectangle(0, 64, 32, 32),
  66. 6,
  67. new Rectangle(0, 96, 32, 32),
  68. 1,
  69. new Vector2(32, 32));
  70. EffectsManager.Initialize(
  71. spriteSheet,
  72. new Rectangle(0, 288, 2, 2),
  73. new Rectangle(0, 256, 32, 32),
  74. 3);
  75. WeaponManager.Texture = spriteSheet;
  76. GoalManager.Initialize(
  77. spriteSheet,
  78. new Rectangle(0, 7 * 32, 32, 32),
  79. new Rectangle(3 * 32, 7 * 32, 32, 32),
  80. 3,
  81. 1);
  82. EnemyManager.Initialize(
  83. spriteSheet,
  84. new Rectangle(0, 160, 32, 32));
  85. // TODO: use this.Content to load your game content here
  86. }
  87. /// <summary>
  88. /// UnloadContent will be called once per game and is the place to unload
  89. /// all content.
  90. /// </summary>
  91. protected override void UnloadContent()
  92. {
  93. // TODO: Unload any non ContentManager content here
  94. }
  95. private void checkPlayerDeath()
  96. {
  97. foreach (Enemy enemy in EnemyManager.Enemies)
  98. {
  99. if (enemy.EnemyBase.IsCircleColliding(
  100. Player.BaseSprite.WorldCenter,
  101. Player.BaseSprite.CollisionRadius))
  102. {
  103. gameState = GameStates.GameOver;
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Allows the game to run logic such as updating the world,
  109. /// checking for collisions, gathering input, and playing audio.
  110. /// </summary>
  111. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  112. protected override void Update(GameTime gameTime)
  113. {
  114. KeyboardState keyState = Keyboard.GetState();
  115. // Allows the game to exit
  116. if (keyState.IsKeyDown(Keys.Escape)
  117. || GamePad.GetState(PlayerIndex.One).Buttons.Back ==
  118. ButtonState.Pressed)
  119. this.Exit();
  120. switch (gameState)
  121. {
  122. case GameStates.TitleScreen:
  123. if ((GamePad.GetState(PlayerIndex.One).Buttons.A ==
  124. ButtonState.Pressed) ||
  125. (Keyboard.GetState().IsKeyDown(Keys.Space)))
  126. {
  127. GameManager.StartNewGame();
  128. gameState = GameStates.Playing;
  129. }
  130. break;
  131. case GameStates.Playing:
  132. Player.Update(gameTime);
  133. WeaponManager.Update(gameTime);
  134. EnemyManager.Update(gameTime);
  135. EffectsManager.Update(gameTime);
  136. GoalManager.Update(gameTime);
  137. if (GoalManager.ActiveTerminals == 0)
  138. {
  139. gameState = GameStates.WaveComplete;
  140. }
  141. break;
  142. case GameStates.WaveComplete:
  143. waveCompleteTimer +=
  144. (float)gameTime.ElapsedGameTime.TotalSeconds;
  145. if (waveCompleteTimer > waveCompleteDelay)
  146. {
  147. GameManager.StartNewWave();
  148. gameState = GameStates.Playing;
  149. waveCompleteTimer = 0.0f;
  150. }
  151. break;
  152. case GameStates.GameOver:
  153. gameOverTimer +=
  154. (float)gameTime.ElapsedGameTime.TotalSeconds;
  155. if (gameOverTimer > gameOverDelay)
  156. {
  157. gameState = GameStates.TitleScreen;
  158. gameOverTimer = 0.0f;
  159. }
  160. break;
  161. }
  162. base.Update(gameTime);
  163. }
  164. /// <summary>
  165. /// This is called when the game should draw itself.
  166. /// </summary>
  167. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  168. protected override void Draw(GameTime gameTime)
  169. {
  170. GraphicsDevice.Clear(Color.MonoGameOrange);
  171. spriteBatch.Begin();
  172. if (gameState == GameStates.TitleScreen)
  173. {
  174. spriteBatch.Draw(
  175. titleScreen,
  176. new Rectangle(0, 0, 800, 600),
  177. Color.White);
  178. }
  179. if ((gameState == GameStates.Playing) ||
  180. (gameState == GameStates.WaveComplete) ||
  181. (gameState == GameStates.GameOver))
  182. {
  183. TileMap.Draw(spriteBatch);
  184. WeaponManager.Draw(spriteBatch);
  185. Player.Draw(spriteBatch);
  186. EnemyManager.Draw(spriteBatch);
  187. EffectsManager.Draw(spriteBatch);
  188. GoalManager.Draw(spriteBatch);
  189. checkPlayerDeath();
  190. spriteBatch.DrawString(
  191. pericles14,
  192. "Score: " + GameManager.Score.ToString(),
  193. new Vector2(30, 5),
  194. Color.White);
  195. spriteBatch.DrawString(
  196. pericles14,
  197. "Terminals Remaining: " +
  198. GoalManager.ActiveTerminals,
  199. new Vector2(520, 5),
  200. Color.White);
  201. }
  202. if (gameState == GameStates.WaveComplete)
  203. {
  204. spriteBatch.DrawString(
  205. pericles14,
  206. "Beginning Wave " +
  207. (GameManager.CurrentWave + 1).ToString(),
  208. new Vector2(300, 300),
  209. Color.White);
  210. }
  211. if (gameState == GameStates.GameOver)
  212. {
  213. spriteBatch.DrawString(
  214. pericles14,
  215. "G A M E O V E R!",
  216. new Vector2(300, 300),
  217. Color.White);
  218. }
  219. spriteBatch.End();
  220. base.Draw(gameTime);
  221. }
  222. }
  223. }