ParticleSampleGame.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // ParticleSampleGame.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.Audio;
  15. using Microsoft.Xna.Framework.GamerServices;
  16. using Microsoft.Xna.Framework.Graphics;
  17. using Microsoft.Xna.Framework.Input;
  18. using Microsoft.Xna.Framework.Input.Touch;
  19. using Microsoft.Xna.Framework.Storage;
  20. using Microsoft.Xna.Framework.Content;
  21. using Microsoft.Xna.Framework.Media;
  22. #endregion
  23. namespace ParticleSample
  24. {
  25. /// <summary>
  26. /// This is the main type for the ParticleSample, and inherits from the Framework's
  27. /// Game class. It creates three different kinds of ParticleSystems, and then adds
  28. /// them to its components collection. It also has keeps a random number generator,
  29. /// a SpriteBatch, and a ContentManager that the different classes in this sample
  30. /// can share.
  31. /// </summary>
  32. public class ParticleSampleGame : Game
  33. {
  34. #region Fields and Properties
  35. GraphicsDeviceManager graphics;
  36. // The particle systems will all need a SpriteBatch to draw their particles,
  37. // so let's make one they can share. We'll use this to draw our SpriteFont
  38. // too.
  39. SpriteBatch spriteBatch;
  40. public SpriteBatch SpriteBatch
  41. {
  42. get { return spriteBatch; }
  43. }
  44. // Used to draw the instructions on the screen.
  45. SpriteFont arialFont;
  46. // a random number generator that the whole sample can share.
  47. private static Random random = new Random();
  48. public static Random Random
  49. {
  50. get { return random; }
  51. }
  52. // Here's the really fun part of the sample, the particle systems! These are
  53. // drawable game components, so we can just add them to the components
  54. // collection. Read more about each particle system in their respective source
  55. // files.
  56. ExplosionParticleSystem explosion;
  57. ExplosionSmokeParticleSystem smoke;
  58. SmokePlumeParticleSystem smokePlume;
  59. // State is an enum that represents which effect we're currently demoing.
  60. enum State
  61. {
  62. Explosions,
  63. SmokePlume
  64. };
  65. // the number of values in the "State" enum.
  66. const int NumStates = 2;
  67. State currentState = State.Explosions;
  68. // a timer that will tell us when it's time to trigger another explosion.
  69. const float TimeBetweenExplosions = 2.0f;
  70. float timeTillExplosion = 0.0f;
  71. // keep a timer that will tell us when it's time to add more particles to the
  72. // smoke plume.
  73. const float TimeBetweenSmokePlumePuffs = .5f;
  74. float timeTillPuff = 0.0f;
  75. // keep track of the last frame's keyboard and gamepad state, so that we know
  76. // if the user has pressed a button.
  77. KeyboardState lastKeyboardState;
  78. GamePadState lastGamepadState;
  79. #endregion
  80. #region Initialization
  81. public ParticleSampleGame()
  82. {
  83. graphics = new GraphicsDeviceManager(this);
  84. graphics.PreferredBackBufferWidth = 320;
  85. graphics.PreferredBackBufferHeight = 480;
  86. Content.RootDirectory = "Content";
  87. // create the particle systems and add them to the components list.
  88. // we should never see more than one explosion at once
  89. explosion = new ExplosionParticleSystem(this, 1);
  90. Components.Add(explosion);
  91. // but the smoke from the explosion lingers a while.
  92. smoke = new ExplosionSmokeParticleSystem(this, 2);
  93. Components.Add(smoke);
  94. // we'll see lots of these effects at once; this is ok
  95. // because they have a fairly small number of particles per effect.
  96. smokePlume = new SmokePlumeParticleSystem(this, 9);
  97. Components.Add(smokePlume);
  98. }
  99. /// <summary>
  100. /// Load your graphics content.
  101. /// </summary>
  102. protected override void LoadContent()
  103. {
  104. spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
  105. arialFont = Content.Load<SpriteFont>("font");
  106. }
  107. #endregion
  108. #region Update and Draw
  109. /// <summary>
  110. /// Allows the game to run logic such as updating the world,
  111. /// checking for collisions, gathering input and playing audio.
  112. /// </summary>
  113. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  114. protected override void Update(GameTime gameTime)
  115. {
  116. // check the input devices to see if someone has decided they want to see
  117. // the other effect, if they want to quit.
  118. HandleInput();
  119. float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
  120. switch (currentState)
  121. {
  122. // if we should be demoing the explosions effect, check to see if it's
  123. // time for a new explosion.
  124. case State.Explosions:
  125. UpdateExplosions(dt);
  126. break;
  127. // if we're showing off the smoke plume, check to see if it's time for a
  128. // new puff of smoke.
  129. case State.SmokePlume:
  130. UpdateSmokePlume(dt);
  131. break;
  132. }
  133. // the base update will handle updating the particle systems themselves,
  134. // because we added them to the components collection.
  135. base.Update(gameTime);
  136. }
  137. // this function is called when we want to demo the smoke plume effect. it
  138. // updates the timeTillPuff timer, and adds more particles to the plume when
  139. // necessary.
  140. private void UpdateSmokePlume(float dt)
  141. {
  142. timeTillPuff -= dt;
  143. if (timeTillPuff < 0)
  144. {
  145. Vector2 where = Vector2.Zero;
  146. // add more particles at the bottom of the screen, halfway across.
  147. where.X = graphics.GraphicsDevice.Viewport.Width / 2;
  148. where.Y = graphics.GraphicsDevice.Viewport.Height;
  149. smokePlume.AddParticles(where);
  150. // and then reset the timer.
  151. timeTillPuff = TimeBetweenSmokePlumePuffs;
  152. }
  153. }
  154. // this function is called when we want to demo the explosion effect. it
  155. // updates the timeTillExplosion timer, and starts another explosion effect
  156. // when the timer reaches zero.
  157. private void UpdateExplosions(float dt)
  158. {
  159. timeTillExplosion -= dt;
  160. if (timeTillExplosion < 0)
  161. {
  162. Vector2 where = Vector2.Zero;
  163. // create the explosion at some random point on the screen.
  164. where.X = RandomBetween(0, graphics.GraphicsDevice.Viewport.Width);
  165. where.Y = RandomBetween(0, graphics.GraphicsDevice.Viewport.Height);
  166. // the overall explosion effect is actually comprised of two particle
  167. // systems: the fiery bit, and the smoke behind it. add particles to
  168. // both of those systems.
  169. explosion.AddParticles(where);
  170. smoke.AddParticles(where);
  171. // reset the timer.
  172. timeTillExplosion = TimeBetweenExplosions;
  173. }
  174. }
  175. /// <summary>
  176. /// This is called when the game should draw itself.
  177. /// </summary>
  178. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  179. protected override void Draw(GameTime gameTime)
  180. {
  181. graphics.GraphicsDevice.Clear(Color.Black);
  182. spriteBatch.Begin();
  183. // draw some instructions on the screen
  184. string message = string.Format("Touch the Screen\nto switch effects.\n\n" +
  185. "Current effect:\n {0}!\n\n" +
  186. "Free particles:\n" +
  187. "Explosion: {1}\n" +
  188. "ExplosionSmoke:{2}\n" +
  189. "SmokePlume: {3}",
  190. currentState, explosion.FreeParticleCount,
  191. smoke.FreeParticleCount, smokePlume.FreeParticleCount );
  192. spriteBatch.DrawString(arialFont, message, new Vector2(10, 10), Color.White);
  193. spriteBatch.End();
  194. base.Draw(gameTime);
  195. }
  196. // This function will check to see if the user has just pushed the A button or
  197. // the space bar. If so, we should go to the next effect.
  198. private void HandleInput()
  199. {
  200. KeyboardState currentKeyboardState = Keyboard.GetState();
  201. GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
  202. TouchCollection currentTouchCollection = TouchPanel.GetState();
  203. // Allows the game to exit
  204. if (currentGamePadState.Buttons.Back == ButtonState.Pressed ||
  205. currentKeyboardState.IsKeyDown(Keys.Escape))
  206. this.Exit();
  207. // check to see if someone has just released the space bar.
  208. bool keyboardSpace =
  209. currentKeyboardState.IsKeyUp(Keys.Space) &&
  210. lastKeyboardState.IsKeyDown(Keys.Space);
  211. // check the gamepad to see if someone has just released the A button.
  212. bool gamepadA =
  213. currentGamePadState.Buttons.A == ButtonState.Pressed &&
  214. lastGamepadState.Buttons.A == ButtonState.Released;
  215. bool touched = false;
  216. // tap the screen to select
  217. foreach (TouchLocation location in currentTouchCollection)
  218. {
  219. switch (location.State)
  220. {
  221. case TouchLocationState.Pressed:
  222. touched = true;
  223. break;
  224. case TouchLocationState.Moved:
  225. break;
  226. case TouchLocationState.Released:
  227. break;
  228. }
  229. }
  230. // if either the A button or the space bar was just released, move to the
  231. // next state. Doing modulus by the number of states lets us wrap back
  232. // around to the first state.
  233. if (keyboardSpace || gamepadA || touched)
  234. {
  235. currentState = (State)((int)(currentState + 1) % NumStates);
  236. }
  237. lastKeyboardState = currentKeyboardState;
  238. lastGamepadState = currentGamePadState;
  239. }
  240. #endregion
  241. #region Helper Functions
  242. // a handy little function that gives a random float between two
  243. // values. This will be used in several places in the sample, in particilar in
  244. // ParticleSystem.InitializeParticle.
  245. public static float RandomBetween(float min, float max)
  246. {
  247. return min + (float)random.NextDouble() * (max - min);
  248. }
  249. #endregion
  250. }
  251. }