Game1.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 Robot_Rampage
  12. {
  13. /// <summary>
  14. /// This is the main type for your game
  15. /// </summary>
  16. public class Game1 : Microsoft.Xna.Framework.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 Game1()
  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. // Allows the game to exit
  115. if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
  116. ButtonState.Pressed)
  117. this.Exit();
  118. switch (gameState)
  119. {
  120. case GameStates.TitleScreen:
  121. if ((GamePad.GetState(PlayerIndex.One).Buttons.A ==
  122. ButtonState.Pressed) ||
  123. (Keyboard.GetState().IsKeyDown(Keys.Space)))
  124. {
  125. GameManager.StartNewGame();
  126. gameState = GameStates.Playing;
  127. }
  128. break;
  129. case GameStates.Playing:
  130. Player.Update(gameTime);
  131. WeaponManager.Update(gameTime);
  132. EnemyManager.Update(gameTime);
  133. EffectsManager.Update(gameTime);
  134. GoalManager.Update(gameTime);
  135. if (GoalManager.ActiveTerminals == 0)
  136. {
  137. gameState = GameStates.WaveComplete;
  138. }
  139. break;
  140. case GameStates.WaveComplete:
  141. waveCompleteTimer +=
  142. (float)gameTime.ElapsedGameTime.TotalSeconds;
  143. if (waveCompleteTimer > waveCompleteDelay)
  144. {
  145. GameManager.StartNewWave();
  146. gameState = GameStates.Playing;
  147. waveCompleteTimer = 0.0f;
  148. }
  149. break;
  150. case GameStates.GameOver:
  151. gameOverTimer +=
  152. (float)gameTime.ElapsedGameTime.TotalSeconds;
  153. if (gameOverTimer > gameOverDelay)
  154. {
  155. gameState = GameStates.TitleScreen;
  156. gameOverTimer = 0.0f;
  157. }
  158. break;
  159. }
  160. base.Update(gameTime);
  161. }
  162. /// <summary>
  163. /// This is called when the game should draw itself.
  164. /// </summary>
  165. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  166. protected override void Draw(GameTime gameTime)
  167. {
  168. GraphicsDevice.Clear(Color.CornflowerBlue);
  169. spriteBatch.Begin();
  170. if (gameState == GameStates.TitleScreen)
  171. {
  172. spriteBatch.Draw(
  173. titleScreen,
  174. new Rectangle(0, 0, 800, 600),
  175. Color.White);
  176. }
  177. if ((gameState == GameStates.Playing) ||
  178. (gameState == GameStates.WaveComplete) ||
  179. (gameState == GameStates.GameOver))
  180. {
  181. TileMap.Draw(spriteBatch);
  182. WeaponManager.Draw(spriteBatch);
  183. Player.Draw(spriteBatch);
  184. EnemyManager.Draw(spriteBatch);
  185. EffectsManager.Draw(spriteBatch);
  186. GoalManager.Draw(spriteBatch);
  187. checkPlayerDeath();
  188. spriteBatch.DrawString(
  189. pericles14,
  190. "Score: " + GameManager.Score.ToString(),
  191. new Vector2(30, 5),
  192. Color.White);
  193. spriteBatch.DrawString(
  194. pericles14,
  195. "Terminals Remaining: " +
  196. GoalManager.ActiveTerminals,
  197. new Vector2(520, 5),
  198. Color.White);
  199. }
  200. if (gameState == GameStates.WaveComplete)
  201. {
  202. spriteBatch.DrawString(
  203. pericles14,
  204. "Beginning Wave " +
  205. (GameManager.CurrentWave + 1).ToString(),
  206. new Vector2(300, 300),
  207. Color.White);
  208. }
  209. if (gameState == GameStates.GameOver)
  210. {
  211. spriteBatch.DrawString(
  212. pericles14,
  213. "G A M E O V E R!",
  214. new Vector2(300, 300),
  215. Color.White);
  216. }
  217. spriteBatch.End();
  218. base.Draw(gameTime);
  219. }
  220. }
  221. }