Guest User

ParticleEmitter.java

a guest
Aug 13th, 2012
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. package elec0.particleEngine;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.newdawn.slick.Color;
  7. import org.newdawn.slick.opengl.Texture;
  8.  
  9. import elec0.particleEngine.Particle;
  10. import elec0.utils.Timing;
  11. import elec0.utils.Vector3f;
  12.  
  13. public class ParticleEmitter
  14. {
  15.     // Privates
  16.     private float CONST_GRAVITY = 0.005f;
  17.     private Vector3f startPosition;
  18.     private float particleCount;
  19.     private Color startColor;
  20.     private List<Particle> particleList = new ArrayList<Particle>();
  21.     private String particleBehavior;
  22.    
  23.     //Publics
  24.     public String textureStyle;
  25.     public Texture particleTexture; // This is set from ParticleEngine when it
  26.    
  27.     // ParticleEmitter constructor start
  28.     ParticleEmitter(float particleCount, Vector3f position, Color color)
  29.     {
  30.         this.particleCount = particleCount;
  31.         this.startPosition = position;
  32.         this.startColor = color;
  33.         for(int i = 0; i < this.particleCount; ++i)
  34.         {
  35.             particleList.add(new Particle(startPosition, startColor));
  36.         }
  37.     }
  38.     ParticleEmitter(float particleCount, Vector3f position, Color color, String textureStyle)
  39.     {
  40.         this.particleCount = particleCount;
  41.         this.startPosition = position;
  42.         this.startColor = color;
  43.         this.textureStyle = textureStyle;
  44.         for(int i = 0; i < this.particleCount; ++i)
  45.         {
  46.             particleList.add(new Particle(startPosition, startColor));
  47.         }
  48.     }
  49.     ParticleEmitter(float particleCount, Vector3f position, Color color, String textureStyle, String particleBehavior)
  50.     {
  51.         this.particleCount = particleCount;
  52.         startPosition = position;
  53.         startColor = color;
  54.         this.textureStyle = textureStyle;
  55.         this.particleBehavior = particleBehavior;
  56.        
  57.         for(int i = 0; i < this.particleCount; ++i)
  58.         {
  59.             particleList.add(new Particle(startPosition, startColor, particleBehavior));
  60.            
  61.         }
  62.     }
  63.     // ParticleEmitter constructor end
  64.    
  65.    
  66.     public void draw()
  67.     {
  68.         particleTexture.bind();
  69.        
  70.         for(Particle p : particleList)
  71.             p.draw();
  72.     }
  73.    
  74.     public void update()
  75.     {
  76.         List<Particle> newList = new ArrayList<Particle>();
  77.        
  78.         for(Particle p : particleList)
  79.         {
  80.            
  81.             p.moveVector = new Vector3f(0.00001f, 0.00001f, 0.00001f);
  82. //          p.moveVector.addToY(-CONST_GRAVITY);
  83.             float start = startPosition.getY();
  84.             p.position.addTo(p.moveVector);
  85.            
  86.             float end = startPosition.getY();
  87.             if(start != end)
  88.                 System.out.println("hit");
  89.             // Check to see if particle's dead yet
  90.             if(Timing.getTime() > (p.birthTime + p.lifetime))
  91.             {
  92.                
  93.                 // It's dead, reset it.
  94.                 newList.add(new Particle(startPosition, startColor, p.behavior));
  95.                
  96.             }
  97.             else
  98.             {
  99.                
  100.                 // If it's not dead, put it in the new list
  101.                 newList.add(p);
  102.                
  103.             }  
  104.  
  105.         }
  106.        
  107.         particleList = newList; // So we can edit things while iterating over the list
  108.        
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment