Advertisement
DaveVoyles

ParticleEngine and Particle

Jun 14th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7.  
  8. namespace Pong
  9. {
  10.     public class ParticleEngine
  11.     {
  12.         private Random random;
  13.         public Vector2 EmitterLocation { get; set; }
  14.         private List<Particle> particles;
  15.         private List<Texture2D> textures;
  16.  
  17.         public ParticleEngine(List<Texture2D> textures, Vector2 location)
  18.         {
  19.             EmitterLocation = location;
  20.             this.textures = textures;
  21.             this.particles = new List<Particle>();
  22.             random = new Random();
  23.         }
  24.  
  25.         public void Update()
  26.         {
  27.             int total = 10;
  28.  
  29.             for (int i = 0; i < total; i++)
  30.             {
  31.                 particles.Add(GenerateNewParticle());
  32.             }
  33.  
  34.             for (int particle = 0; particle < particles.Count; particle++)
  35.             {
  36.                 particles[particle].Update();
  37.                 if (particles[particle].TTL <= 0)
  38.                 {
  39.                     particles.RemoveAt(particle);
  40.                     particle--;
  41.                 }
  42.             }
  43.         }
  44.  
  45.         private Particle GenerateNewParticle()
  46.         {
  47.             Texture2D texture = textures[random.Next(textures.Count)];
  48.             Vector2 position = EmitterLocation;
  49.             Vector2 velocity = new Vector2(
  50.                                     1f * (float)(random.NextDouble() * 2 - 1),
  51.                                     1f * (float)(random.NextDouble() * 2 - 1));
  52.             float angle = 0;
  53.             float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
  54.             Color color = new Color(
  55.                         (float)random.NextDouble(),
  56.                         (float)random.NextDouble(),
  57.                         (float)random.NextDouble());
  58.             float size = (float)random.NextDouble();
  59.             int ttl = 20 + random.Next(40);
  60.  
  61.             return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl);
  62.         }
  63.  
  64.         public void Draw(SpriteBatch spriteBatch)
  65.         {
  66.             spriteBatch.Begin();
  67.             for (int index = 0; index < particles.Count; index++)
  68.             {
  69.                 particles[index].Draw(spriteBatch);
  70.             }
  71.             spriteBatch.End();
  72.         }
  73.     }
  74. }
  75.  
  76.  
  77.  
  78.  
  79. ////////////////////////////////////////////////////////////////////
  80.  
  81.  
  82. using System;
  83. using System.Collections.Generic;
  84. using System.Linq;
  85. using System.Text;
  86. using Microsoft.Xna.Framework.Graphics;
  87. using Microsoft.Xna.Framework;
  88.  
  89. namespace Pong
  90. {
  91.     public class Particle
  92.     {
  93.         public Texture2D Texture { get; set; }
  94.         public Vector2 Position { get; set; }
  95.         public Vector2 Velocity { get; set; }
  96.         public float Angle { get; set; }
  97.         public float AngularVelocity { get; set; }
  98.         public Color Color { get; set; }
  99.         public float Size { get; set; }
  100.         public int TTL { get; set; }
  101.  
  102.         public Particle(Texture2D texture, Vector2 position, Vector2 velocity,
  103.             float angle, float angularVelocity, Color color, float size, int ttl)
  104.         {
  105.             Texture = texture;
  106.             Position = position;
  107.             Velocity = velocity;
  108.             Angle = angle;
  109.             AngularVelocity = angularVelocity;
  110.             Color = color;
  111.             Size = size;
  112.             TTL = ttl;
  113.         }
  114.  
  115.         public void Update()
  116.         {
  117.             TTL--;
  118.             Position += Velocity;
  119.             Angle += AngularVelocity;
  120.         }
  121.  
  122.         public void Draw(SpriteBatch spriteBatch)
  123.         {
  124.             Rectangle sourceRectangle = new Rectangle(0, 0, Texture.Width, Texture.Height);
  125.             Vector2 origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
  126.  
  127.             spriteBatch.Draw(Texture, Position, sourceRectangle, Color,
  128.                 Angle, origin, Size, SpriteEffects.None, 0f);
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement