Advertisement
Guest User

Untitled

a guest
May 29th, 2015
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4.  
  5. namespace game
  6. {
  7. class ParticleExplosion
  8. {
  9. // Particle arrays and vertex buffer
  10. VertexPositionTexture[] verts;
  11. Vector3[] vertexDirectionArray;
  12. Color[] vertexColorArray;
  13. VertexBuffer particleVertexBuffer;
  14.  
  15. // Position
  16. Vector3 position;
  17.  
  18. // Life
  19. int lifeLeft;
  20.  
  21. // Rounds and particle counts
  22. int numParticlesPerRound;
  23. int maxParticles;
  24. static Random rnd = new Random();
  25. int roundTime;
  26. int timeSinceLastRound = 0;
  27.  
  28. // Vertex and graphics info
  29. GraphicsDevice graphicsDevice;
  30.  
  31. // Settings
  32. ParticleSettings particleSettings;
  33.  
  34. // Effect
  35. Effect particleEffect;
  36.  
  37. // Textures
  38. Texture2D particleColorsTexture;
  39.  
  40. // Array indices
  41. int endOfLiveParticlesIndex = 0;
  42. int endOfDeadParticlesIndex = 0;
  43.  
  44. public bool IsDead
  45. {
  46. get { return endOfDeadParticlesIndex == maxParticles; }
  47. }
  48.  
  49. public ParticleExplosion(GraphicsDevice graphicsDevice, Vector3 position,
  50. int lifeLeft, int roundTime, int numParticlesPerRound, int maxParticles,
  51. Texture2D particleColorsTexture, ParticleSettings particleSettings, Effect particleEffect)
  52. {
  53. this.position = position;
  54. this.lifeLeft = lifeLeft;
  55. this.numParticlesPerRound = numParticlesPerRound;
  56. this.maxParticles = maxParticles;
  57. this.roundTime = roundTime;
  58. this.graphicsDevice = graphicsDevice;
  59. this.particleSettings = particleSettings;
  60. this.particleEffect = particleEffect;
  61. this.particleColorsTexture = particleColorsTexture;
  62.  
  63. InitializeParticleVertices();
  64.  
  65. }
  66.  
  67. private void InitializeParticleVertices()
  68. {
  69. // Instantiate all particle arrays
  70. verts = new VertexPositionTexture[maxParticles * 4];
  71. vertexDirectionArray = new Vector3[maxParticles];
  72. vertexColorArray = new Color[maxParticles];
  73.  
  74. // Get color data from colors texture
  75. Color[] colors = new Color[particleColorsTexture.Width * particleColorsTexture.Height];
  76. particleColorsTexture.GetData(colors);
  77.  
  78. // Loop until max particles
  79. for (int i = 0; i < maxParticles; ++i)
  80. {
  81. float size = (float)rnd.NextDouble() * particleSettings.maxSize;
  82.  
  83. // Set position, direction and size of particle
  84. verts[i * 4] = new VertexPositionTexture(position, new Vector2(0, 0));
  85. verts[(i * 4) + 1] = new VertexPositionTexture(new Vector3(position.X, position.Y + size, position.Z), new Vector2(0, 1));
  86. verts[(i * 4) + 2] = new VertexPositionTexture(new Vector3(position.X + size, position.Y, position.Z), new Vector2(1, 0));
  87. verts[(i * 4) + 3] = new VertexPositionTexture(new Vector3(position.X + size, position.Y + size, position.Z), new Vector2(1, 1));
  88.  
  89. // Create a random velocity/direction
  90. Vector3 direction = new Vector3(
  91. (float)rnd.NextDouble() * 2 - 1,
  92. (float)rnd.NextDouble() * 2 - 1,
  93. (float)rnd.NextDouble() * 2 - 1);
  94. direction.Normalize();
  95.  
  96. // Multiply by NextDouble to make sure that
  97. // all particles move at random speeds
  98. direction *= (float)rnd.NextDouble();
  99.  
  100. // Set direction of particle
  101. vertexDirectionArray[i] = direction;
  102.  
  103. // Set color of particle by getting a random color from the texture
  104. vertexColorArray[i] = colors[(rnd.Next(0, particleColorsTexture.Height) * particleColorsTexture.Width) + rnd.Next(0, particleColorsTexture.Width)];
  105.  
  106. }
  107.  
  108. // Instantiate vertex buffer
  109. particleVertexBuffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionTexture), verts.Length, BufferUsage.None);
  110.  
  111. }
  112.  
  113. public void Update(GameTime gameTime)
  114. {
  115. // Decrement life left until it's gone
  116. if (lifeLeft > 0)
  117. lifeLeft -= gameTime.ElapsedGameTime.Milliseconds;
  118.  
  119. // Time for new round?
  120. timeSinceLastRound += gameTime.ElapsedGameTime.Milliseconds;
  121. if (timeSinceLastRound > roundTime)
  122. {
  123. // New round - add and remove particles
  124. timeSinceLastRound -= roundTime;
  125.  
  126. // Increment end of live particles index each
  127. // round until end of list is reached
  128. if (endOfLiveParticlesIndex < maxParticles)
  129. {
  130. endOfLiveParticlesIndex += numParticlesPerRound;
  131. if (endOfLiveParticlesIndex > maxParticles)
  132. endOfLiveParticlesIndex = maxParticles;
  133. }
  134. if (lifeLeft <= 0)
  135. {
  136. // Increment end of dead particles index each
  137. // round until end of list is reached
  138. if (endOfDeadParticlesIndex < maxParticles)
  139. {
  140. endOfDeadParticlesIndex += numParticlesPerRound;
  141. if (endOfDeadParticlesIndex > maxParticles)
  142. endOfDeadParticlesIndex = maxParticles;
  143. }
  144. }
  145. }
  146.  
  147. // Update positions of all live particles
  148. for (int i = endOfDeadParticlesIndex;
  149. i < endOfLiveParticlesIndex; ++i)
  150. {
  151. verts[i * 4].Position += vertexDirectionArray[i];
  152. verts[(i * 4) + 1].Position += vertexDirectionArray[i];
  153. verts[(i * 4) + 2].Position += vertexDirectionArray[i];
  154. verts[(i * 4) + 3].Position += vertexDirectionArray[i];
  155.  
  156. }
  157. }
  158.  
  159. public void Draw(Camera camera)
  160. {
  161. graphicsDevice.SetVertexBuffer(particleVertexBuffer);
  162.  
  163. // Only draw if there are live particles
  164. if (endOfLiveParticlesIndex - endOfDeadParticlesIndex > 0)
  165. {
  166. for (int i = endOfDeadParticlesIndex; i < endOfLiveParticlesIndex; ++i)
  167. {
  168. particleEffect.Parameters["WorldViewProjection"].SetValue(
  169. camera.view * camera.projection);
  170. particleEffect.Parameters["particleColor"].SetValue(vertexColorArray[i].ToVector4());
  171.  
  172. // Draw particles
  173. foreach (EffectPass pass in particleEffect.CurrentTechnique.Passes)
  174. {
  175. pass.Apply();
  176.  
  177. graphicsDevice.DrawUserPrimitives<VertexPositionTexture>(
  178. PrimitiveType.TriangleStrip,
  179. verts, i * 4, 2);
  180.  
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement