Advertisement
DaveVoyles

Damn particles

Jul 21st, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.09 KB | None | 0 0
  1. // I'm trying to get my particle manager working in my game.
  2. * Everything works fine, EXCEPT for a particle appearing in the top left corner during initialization.
  3. * It stems from the line             shortParticle = new ShortParticle(textures, new Vector2());
  4. * under my Bat constructor. if I leave it blank, or use 0,0 for the Vector2 then the particle remains in the * top left corner until the ball and bat make contact. At that point the new particle continues to display
  5. * point of contact until the ball hits the bat on the other side.
  6. //
  7.  
  8. // 1)  How do I NOT have the particle display in the top left corner when the game starts?
  9. // 2) How do I kill the particle after 2 seconds? I tried adjusting the TTL in my ShortParticle class, but
  10. // it doesn't seem to change much.
  11.  
  12.     Public Class Ball
  13. {
  14.     // Ball constructor
  15.        public Ball(ContentManager contentManager, Vector2 ScreenSize, Bat bat, AIBat aiBat)
  16.         {    
  17.     ......
  18.         shortParticle = new ShortParticle(textures, new Vector2());
  19.         ......        
  20.          }
  21.  
  22.         public void Draw(SpriteBatch spriteBatch)
  23.         {
  24.                 // Draws the short particle
  25.                 shortParticle.Draw(spriteBatch);
  26.         }
  27.  
  28.     Update
  29.          }          
  30.         shortParticle.Update(gameTime);
  31.          }
  32.  
  33.  public void BatHit(int block)
  34.         {
  35.             if (direction > Math.PI * 1.5f || direction < Math.PI * 0.5f)
  36.             {
  37.         /// Generates particle when bat and ball collide
  38.                 RightBatParticles();
  39.                 ......
  40.              }  
  41.             }
  42. }
  43.  
  44.  
  45.  
  46. #region File Description
  47. //----------------------------------------------------------------------------------------------
  48. // Engine is designed by RB Whitaker http://rbwhitaker.wikidot.com/2d-particle-engine-1
  49. //----------------------------------------------------------------------------------------------------
  50. #endregion
  51.  
  52. using System;
  53. using System.Collections.Generic;
  54. using System.Linq;
  55. using System.Text;
  56. using Microsoft.Xna.Framework;
  57. using Microsoft.Xna.Framework.Graphics;
  58.  
  59. namespace Pong
  60. {
  61.     public class ParticleEngine
  62.     {
  63.         private Random random;
  64.         public Vector2 EmitterLocation { get; set; }
  65.         private List<Particle> particles;
  66.         private List<Texture2D> textures;
  67.  
  68.         public ParticleEngine(List<Texture2D> textures, Vector2 location)
  69.         {
  70.             EmitterLocation = location;
  71.             this.textures = textures;
  72.             this.particles = new List<Particle>();
  73.             random = new Random();
  74.         }
  75.  
  76.         public void Update()
  77.         {
  78.             int total = 10;
  79.  
  80.             for (int i = 0; i < total; i++)
  81.             {
  82.                 particles.Add(GenerateNewParticle());
  83.             }
  84.  
  85.             for (int particle = 0; particle < particles.Count; particle++)
  86.             {
  87.                 particles[particle].Update();
  88.                 if (particles[particle].TTL <= 0)
  89.                 {
  90.                     particles.RemoveAt(particle);
  91.                     particle--;
  92.                 }
  93.             }
  94.         }
  95.  
  96.  
  97.         /// <summary>
  98.         /// Generates a normal particle
  99.         /// </summary>
  100.         public Particle GenerateNewParticle()
  101.         {
  102.             Texture2D texture = textures[random.Next(textures.Count)];
  103.             Vector2 position = EmitterLocation;
  104.             Vector2 velocity = new Vector2(
  105.                                     1f * (float)(random.NextDouble() * 2 - 1),
  106.                                     1f * (float)(random.NextDouble() * 2 - 1));
  107.             float angle = 0;
  108.             float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
  109.             Color color = new Color(
  110.                         (float)random.NextDouble(),
  111.                         (float)random.NextDouble(),
  112.                         (float)random.NextDouble());
  113.             float size = (float)random.NextDouble();
  114.             int ttl = 20 + random.Next(40);
  115.  
  116.             return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl);
  117.         }
  118.  
  119.  
  120.         /// <summary>
  121.         /// Draws particles. I commented out spriteBatch for now, as I've adjusted it to be drawn during the bat's spriteBatch
  122.         /// </summary>
  123.         public void Draw(SpriteBatch spriteBatch)
  124.         {
  125.         //    spriteBatch.Begin();
  126.             for (int index = 0; index < particles.Count; index++)
  127.             {
  128.                 particles[index].Draw(spriteBatch);
  129.             }
  130.       //      spriteBatch.End();
  131.         }
  132.     }
  133. }
  134.  
  135. ////////////////////////////////////////////////////////////////////
  136.  
  137. #region File Description
  138. //----------------------------------------------------------------------------------------------
  139. // Engine is designed by RB Whitaker http://rbwhitaker.wikidot.com/2d-particle-engine-1
  140. //----------------------------------------------------------------------------------------------------
  141. #endregion
  142.  
  143. using System;
  144. using System.Collections.Generic;
  145. using System.Linq;
  146. using System.Text;
  147. using Microsoft.Xna.Framework;
  148. using Microsoft.Xna.Framework.Graphics;
  149.  
  150. namespace Pong
  151. {
  152.     public class ShortParticle
  153.     {
  154.         private Random random;
  155.         public Vector2 EmitterLocation { get; set; }
  156.         private List<Particle> particles;
  157.         private List<Texture2D> textures;
  158.         private int threeSecondTime = 3;
  159.         private int elapsedTime;
  160.  
  161.         public ShortParticle(List<Texture2D> textures, Vector2 location)
  162.         {
  163.             EmitterLocation = location;
  164.             this.textures = textures;
  165.             this.particles = new List<Particle>();
  166.             random = new Random();
  167.         }
  168.  
  169.         public void Update(GameTime gameTime)
  170.         {
  171.             // Keps track of time
  172.             elapsedTime = gameTime.ElapsedGameTime.Milliseconds;
  173.             // How many particles appear on screen at once
  174.             int total = 7;
  175.  
  176.             for (int i = 0; i < total; i++)
  177.             {
  178.                 particles.Add(GenerateNewParticle());
  179.             }
  180.  
  181.             for (int particle = 0; particle < particles.Count; particle++)
  182.             {
  183.                 particles[particle].Update();
  184.                 if (particles[particle].TTL <= 0)
  185.                 {
  186.                     particles.RemoveAt(particle);
  187.                     particle--;
  188.                 }
  189.  
  190.             }
  191.  
  192.             for (int particle = 0; particle < particles.Count; particle++)
  193.             {
  194.                 particles[particle].Update();
  195.                 if (threeSecondTime <= elapsedTime)
  196.                 {
  197.                     particles.RemoveAt(particle);
  198.                 }
  199.             }
  200.         }
  201.  
  202.  
  203.         /// <summary>
  204.         /// Generates a normal particle
  205.         /// </summary>
  206.         public Particle GenerateNewParticle()
  207.         {
  208.             Texture2D texture = textures[random.Next(textures.Count)];
  209.             Vector2 position = EmitterLocation;
  210.             Vector2 velocity = new Vector2(
  211.                                     1f * (float)(random.NextDouble() * 2 - 1),
  212.                                     1f * (float)(random.NextDouble() * 2 - 1));
  213.             float angle = 0;
  214.             float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
  215.             Color color = new Color(
  216.                         (float)random.NextDouble(),
  217.                         (float)random.NextDouble(),
  218.                         (float)random.NextDouble());
  219.             float size = (float)random.NextDouble();
  220.             int ttl = 20 + random.Next(40);
  221.  
  222.             return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl);
  223.         }
  224.  
  225.  
  226.         /// <summary>
  227.         /// Draws particles. I commented out spriteBatch for now, as I've adjusted it to be drawn during the bat's spriteBatch
  228.         /// </summary>
  229.         public void Draw(SpriteBatch spriteBatch)
  230.         {
  231.             //    spriteBatch.Begin();
  232.             for (int index = 0; index < particles.Count; index++)
  233.             {
  234.                 particles[index].Draw(spriteBatch);
  235.             }
  236.             //      spriteBatch.End();
  237.         }
  238.     }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement