Advertisement
Jnk1296

Particle Engine

Jul 15th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. public class ParticleEngine implements SFEngine{
  2.    
  3.     // Used by particle emitter control
  4.     int ticks = 0;
  5.    
  6.     /*
  7.      * A lower number reduces the amount of particles emitted.
  8.      * A higher number increases the amount of particles emitted.
  9.      *
  10.      * Limit cannot exceed 1000, or a 'division by zero' error
  11.      * will be thrown, halting the program.
  12.      */
  13.     final int particleLimit = 500;
  14.    
  15.     // Values used for setting the backdrop of the game
  16.     public int redHue = 1;
  17.     public int greenHue = 28;
  18.     public int blueHue = 36;
  19.    
  20.     // Holds all particles
  21.     public static ArrayList<Entity> particles = new ArrayList<Entity>();
  22.    
  23.     // Particle Kill Queue
  24.     public ArrayList<Entity> toKill = new ArrayList<Entity>();
  25.    
  26.     @Override
  27.     public void tickEngine() {
  28.         // Ticks the Particle Emitter Control
  29.         if (ticks < 1000) {
  30.             ticks ++;
  31.         } else {
  32.             ticks = 0;
  33.         }
  34.        
  35.         // Tick Particles
  36.         tickElements();
  37.        
  38.         // Particle Emitter
  39.         /*
  40.          * The particle emitter works by limiting the number of instantiated particles
  41.          * per 1000 ticks. The number of particles spawned per 1000 ticks is specified
  42.          * by the particleLimit(int).
  43.          */
  44.         if (ticks % (1000 / particleLimit) == 0) {
  45.             spawnParticle();
  46.         }
  47.        
  48.         // Perform Object Removal
  49.         checkStatus();
  50.         process();
  51.     }
  52.    
  53.     public void spawnParticle() {
  54.         // Particles can range in size from 1 - 6
  55.         int size = new Random().nextInt(7);
  56.        
  57.         // If the particle size is set as 0, set it as 1
  58.         if (size < 1) size = 1;
  59.        
  60.         // X-Position may range from 0 - the frame's current width
  61.         int xPos = new Random().nextInt(INIT.currentWidth - (size - 1));
  62.        
  63.         // Y-Position is always at -10, which is above the top of the screen.
  64.         // This is so that the user cannot see the particles spawning on screen
  65.         int yPos = -10;
  66.        
  67.         // Particles can have a speed of descent ranging from 1 - 4
  68.         int ySpeed = new Random().nextInt(5);
  69.        
  70.         // If the descent speed is set as 0, set it as 1
  71.         if (ySpeed < 1) ySpeed = 1;
  72.        
  73.         // This is used for the particle's color selector
  74.         int random = new Random().nextInt(5);
  75.        
  76.         // Instantiate new particle
  77.         particles.add(new Particle(xPos, yPos, ySpeed, size, -1, random));
  78.     }
  79.    
  80.     @Override
  81.     public void renderEngine(Graphics g) {
  82.         // Set Background Color
  83.         g.setColor(new Color(redHue, greenHue, blueHue));
  84.        
  85.         // Fill the screen
  86.         g.fillRect(0, 0, INIT.currentWidth, INIT.currentHeight);
  87.        
  88.         // Render particles
  89.         renderElements(g);
  90.     }
  91.    
  92.     @Override
  93.     public void tickElements() {
  94.         for (Entity part:particles) {
  95.             part.tick();
  96.         }
  97.     }
  98.    
  99.     @Override
  100.     public void renderElements(Graphics g) {
  101.         for (Entity part:particles) {
  102.             part.render(g);
  103.         }
  104.     }
  105.    
  106.     @Override
  107.     public void checkStatus() {
  108.         for (Entity part:particles) {
  109.             if (part.isDead()) {
  110.                 toKill.add(part);
  111.             }
  112.         }
  113.     }
  114.    
  115.     @Override
  116.     public void process() {
  117.         for (Entity part:toKill) {
  118.             particles.remove(part);
  119.         }
  120.        
  121.         toKill.clear();
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement