Advertisement
DaveVoyles

Untitled

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