Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. package net.halalaboos.client.utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import net.halalaboos.client.Client;
  8. import static org.lwjgl.opengl.GL11.*;
  9.  
  10. public abstract class SimpleParticleEngine {
  11.  
  12.     private Random random;
  13.     private List<Particle> particles;
  14.     private int particleExpirationTime;
  15.    
  16.     /**
  17.      * @param expirationTime - Time it takes for the particle to expire.
  18.      * */
  19.     public SimpleParticleEngine(int expirationTime) {
  20.         random = new Random();
  21.         particles = new ArrayList<Particle>();
  22.         this.particleExpirationTime = expirationTime;
  23.     }
  24.     /**
  25.      * Updates all particles, applying gravity to them. <br>
  26.      * Called off inside of a tick method.
  27.      * @param gravity - Rate that the particles will fall.
  28.      * */
  29.     public void updateParticles(float gravity) {
  30.         for(int index = 0; index < particles.size(); index++) {
  31.             Particle particle = particles.get(index);
  32.            
  33.             particle.update(gravity);
  34.            
  35.             if(particle.hasExpired()) {
  36.                 particles.remove(particle);
  37.                 index--;
  38.             }
  39.         }
  40.     }
  41.     /**
  42.      * Renders all particles onto the screen.
  43.      * */
  44.     public void renderParticles() {
  45.         Client.getRenderUtils().start2dRendering();
  46.         for (Particle particle : particles) {
  47.             glPushMatrix();
  48.             glTranslated(particle.position.getX(), particle.position.getY(), 0);
  49.             renderParticle(particle);
  50.             glPopMatrix();
  51.         }
  52.         Client.getRenderUtils().end2dRendering();
  53.     }
  54.    
  55.     public abstract void renderParticle(Particle particle);
  56.    
  57.     /**
  58.      * Spawns a set of particles at the position given.
  59.      * @param xPosition - X position the particles will begin their rain at.
  60.      * @param yPosition - Y position the particles will begin their reign at.
  61.      * @param spawnRate - How many particles spawned when called.
  62.      * @param disperseRate - How much the particles will disperse.
  63.      * */
  64.     public void spawnParticles(float xPosition, float yPosition, float spawnRate, float disperseRate) {
  65.         for(int index = 0; index < spawnRate; index++) {
  66.             double x = (xPosition - disperseRate / 2) + (random.nextDouble() * disperseRate);
  67.             double y = (yPosition - disperseRate / 2) + (random.nextDouble() * disperseRate);
  68.             Particle particle = new Particle(x, y);
  69.             particles.add(particle);
  70.         }
  71.     }
  72.    
  73.     public List<Particle> getParticles() {
  74.         return particles;
  75.     }
  76.  
  77.     public class Particle {
  78.         public Coordinates position;
  79.         private double maxTick, currentTick, randomValue;
  80.        
  81.         public Particle(double posX, double posY) {
  82.             randomValue = random.nextInt(100) + 1;
  83.             position = new Coordinates(posX, posY, 0);
  84.             maxTick = particleExpirationTime + random.nextInt(10);
  85.         }
  86.        
  87.         private void update(float gravity) {
  88.             position.setY(position.getY() + gravity + random.nextInt((int) (gravity)));
  89.             currentTick++;
  90.         }
  91.        
  92.         private boolean hasExpired() {
  93.             return maxTick - currentTick <= 0;
  94.         }
  95.        
  96.         public Coordinates getPosition() {
  97.             return position;
  98.         }
  99.        
  100.         public double getRandomValue() {
  101.             return randomValue;
  102.         }
  103.     }
  104.    
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement