Advertisement
Guest User

Untitled

a guest
Feb 16th, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using System.Collections.Generic;
  5.  
  6. namespace WindowsGame1
  7. {
  8.     class Emitter
  9.     {
  10.         Texture2D Texture;
  11.         public bool Active;
  12.         public int Density;
  13.         public Vector2 Position;
  14.         public int Life;
  15.         TimeSpan timeCreated;
  16.         TimeSpan timeToLive;
  17.         public float Gravity;
  18.         public float yvel;
  19.         public Particle particle;
  20.         List<Particle> particles;
  21.         Random Randomnum;
  22.         float Direction;
  23.         Vector2 InitialVel;
  24.  
  25.        
  26.         public void Initialize(Texture2D texture, Vector2 position,int density,int life,GameTime gameTime, float gravity)
  27.         {
  28.            
  29.             particles = new List<Particle>();
  30.             Texture = texture;
  31.             Active = true;
  32.             Position = position;
  33.             Density = density;
  34.             Life = life;
  35.             timeCreated = gameTime.TotalGameTime;
  36.             timeToLive = TimeSpan.FromSeconds(Life);
  37.             Gravity = gravity;
  38.             yvel = 0;
  39.  
  40.            
  41.             AddParticle();  
  42.            
  43.  
  44.            
  45.            
  46.          
  47.         }
  48.         public void AddParticle()
  49.         {
  50.             for (int i = 0; i < Density; i++)
  51.             {
  52.             particle = new Particle();
  53.             Randomnum = new Random();
  54.             Direction = Randomnum.Next(360);
  55.             InitialVel = AngleToVector(Direction);
  56.             particle.Initialize(Texture, Position, 10, 2, InitialVel);
  57.             particles.Add(particle);
  58.             }
  59.         }
  60.  
  61.         Vector2 AngleToVector(float angle)
  62.         {
  63.             return new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
  64.         }
  65.         public void Update(GameTime gameTime)
  66.         {
  67.             if (gameTime.TotalGameTime - timeCreated > timeToLive)
  68.             {
  69.                 Active = false;
  70.                 Position = Vector2.Zero;
  71.  
  72.             }
  73.  
  74.             else
  75.             {
  76.                 yvel += Gravity;
  77.                 Position.Y += yvel;
  78.             }
  79.             UpdateParticles(gameTime);
  80.         }
  81.         public void UpdateParticles(GameTime gameTime)
  82.         {
  83.             for (int i = 0; i < particles.Count; i++)
  84.             {
  85.                 particles[i].Update(gameTime);
  86.             }
  87.         }
  88.         public void Draw(SpriteBatch spriteBatch)
  89.         {
  90.             for (int i = 0; i < particles.Count; i++)
  91.             {
  92.                 particles[i].Draw(spriteBatch);
  93.             }
  94.             spriteBatch.Draw(Texture,Position,Color.White);
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement