Game1.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. using Tile_Engine;
  12. namespace Gemstone_Hunter
  13. {
  14. /// <summary>
  15. /// This is the main type for your game
  16. /// </summary>
  17. public class Game1 : Microsoft.Xna.Framework.Game
  18. {
  19. GraphicsDeviceManager graphics;
  20. SpriteBatch spriteBatch;
  21. Player player;
  22. SpriteFont pericles8;
  23. Vector2 scorePosition = new Vector2(20, 580);
  24. enum GameState { TitleScreen, Playing, PlayerDead, GameOver };
  25. GameState gameState = GameState.TitleScreen;
  26. Vector2 gameOverPosition = new Vector2(350, 300);
  27. Vector2 livesPosition = new Vector2(600, 580);
  28. Texture2D titleScreen;
  29. float deathTimer = 0.0f;
  30. float deathDelay = 5.0f;
  31. public Game1()
  32. {
  33. graphics = new GraphicsDeviceManager(this);
  34. Content.RootDirectory = "Content";
  35. }
  36. /// <summary>
  37. /// Allows the game to perform any initialization it needs to before starting to run.
  38. /// This is where it can query for any required services and load any non-graphic
  39. /// related content. Calling base.Initialize will enumerate through any components
  40. /// and initialize them as well.
  41. /// </summary>
  42. protected override void Initialize()
  43. {
  44. // TODO: Add your initialization logic here
  45. this.graphics.PreferredBackBufferWidth = 800;
  46. this.graphics.PreferredBackBufferHeight = 600;
  47. this.graphics.ApplyChanges();
  48. base.Initialize();
  49. }
  50. /// <summary>
  51. /// LoadContent will be called once per game and is the place to load
  52. /// all of your content.
  53. /// </summary>
  54. protected override void LoadContent()
  55. {
  56. spriteBatch = new SpriteBatch(GraphicsDevice);
  57. TileMap.Initialize(
  58. Content.Load<Texture2D>(@"Textures\PlatformTiles"));
  59. TileMap.spriteFont =
  60. Content.Load<SpriteFont>(@"Fonts\Pericles8");
  61. pericles8 = Content.Load<SpriteFont>(@"Fonts\Pericles8");
  62. titleScreen = Content.Load<Texture2D>(@"Textures\TitleScreen");
  63. Camera.WorldRectangle = new Rectangle(0, 0, 160 * 48, 12 * 48);
  64. Camera.Position = Vector2.Zero;
  65. Camera.ViewPortWidth = 800;
  66. Camera.ViewPortHeight = 600;
  67. player = new Player(Content);
  68. LevelManager.Initialize(Content, player);
  69. }
  70. private void StartNewGame()
  71. {
  72. player.Revive();
  73. player.LivesRemaining = 3;
  74. player.WorldLocation = Vector2.Zero;
  75. LevelManager.LoadLevel(0);
  76. }
  77. /// <summary>
  78. /// UnloadContent will be called once per game and is the place to unload
  79. /// all content.
  80. /// </summary>
  81. protected override void UnloadContent()
  82. {
  83. // TODO: Unload any non ContentManager content here
  84. }
  85. /// <summary>
  86. /// Allows the game to run logic such as updating the world,
  87. /// checking for collisions, gathering input, and playing audio.
  88. /// </summary>
  89. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  90. protected override void Update(GameTime gameTime)
  91. {
  92. KeyboardState keyState = Keyboard.GetState();
  93. // Allows the game to exit
  94. if (keyState.IsKeyDown(Keys.Escape) || GamePad.GetState(PlayerIndex.One).Buttons.Back ==
  95. ButtonState.Pressed)
  96. this.Exit();
  97. GamePadState gamepadState = GamePad.GetState(PlayerIndex.One);
  98. float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  99. if (gameState == GameState.TitleScreen)
  100. {
  101. if (keyState.IsKeyDown(Keys.Space) ||
  102. gamepadState.Buttons.A == ButtonState.Pressed)
  103. {
  104. StartNewGame();
  105. gameState = GameState.Playing;
  106. }
  107. }
  108. if (gameState == GameState.Playing)
  109. {
  110. player.Update(gameTime);
  111. LevelManager.Update(gameTime);
  112. if (player.Dead)
  113. {
  114. if (player.LivesRemaining > 0)
  115. {
  116. gameState = GameState.PlayerDead;
  117. deathTimer = 0.0f;
  118. }
  119. else
  120. {
  121. gameState = GameState.GameOver;
  122. deathTimer = 0.0f;
  123. }
  124. }
  125. }
  126. if (gameState == GameState.PlayerDead)
  127. {
  128. player.Update(gameTime);
  129. LevelManager.Update(gameTime);
  130. deathTimer += elapsed;
  131. if (deathTimer > deathDelay)
  132. {
  133. player.WorldLocation = Vector2.Zero;
  134. LevelManager.ReloadLevel();
  135. player.Revive();
  136. gameState = GameState.Playing;
  137. }
  138. }
  139. if (gameState == GameState.GameOver)
  140. {
  141. deathTimer += elapsed;
  142. if (deathTimer > deathDelay)
  143. {
  144. gameState = GameState.TitleScreen;
  145. }
  146. }
  147. base.Update(gameTime);
  148. }
  149. /// <summary>
  150. /// This is called when the game should draw itself.
  151. /// </summary>
  152. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  153. protected override void Draw(GameTime gameTime)
  154. {
  155. GraphicsDevice.Clear(Color.Black);
  156. spriteBatch.Begin(
  157. SpriteSortMode.BackToFront,
  158. BlendState.AlphaBlend);
  159. if (gameState == GameState.TitleScreen)
  160. {
  161. spriteBatch.Draw(titleScreen, Vector2.Zero, Color.White);
  162. }
  163. if ((gameState == GameState.Playing) ||
  164. (gameState == GameState.PlayerDead) ||
  165. (gameState == GameState.GameOver))
  166. {
  167. TileMap.Draw(spriteBatch);
  168. player.Draw(spriteBatch);
  169. LevelManager.Draw(spriteBatch);
  170. spriteBatch.DrawString(
  171. pericles8,
  172. "Score: " + player.Score.ToString(),
  173. scorePosition,
  174. Color.White);
  175. spriteBatch.DrawString(
  176. pericles8,
  177. "Lives Remaining: " + player.LivesRemaining.ToString(),
  178. livesPosition,
  179. Color.White);
  180. }
  181. if (gameState == GameState.PlayerDead)
  182. {
  183. }
  184. if (gameState == GameState.GameOver)
  185. {
  186. spriteBatch.DrawString(
  187. pericles8,
  188. "G A M E O V E R !",
  189. gameOverPosition,
  190. Color.White);
  191. }
  192. spriteBatch.End();
  193. base.Draw(gameTime);
  194. }
  195. }
  196. }