Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ParticleEngine implements SFEngine{
- // Used by particle emitter control
- int ticks = 0;
- /*
- * A lower number reduces the amount of particles emitted.
- * A higher number increases the amount of particles emitted.
- *
- * Limit cannot exceed 1000, or a 'division by zero' error
- * will be thrown, halting the program.
- */
- final int particleLimit = 500;
- // Values used for setting the backdrop of the game
- public int redHue = 1;
- public int greenHue = 28;
- public int blueHue = 36;
- // Holds all particles
- public static ArrayList<Entity> particles = new ArrayList<Entity>();
- // Particle Kill Queue
- public ArrayList<Entity> toKill = new ArrayList<Entity>();
- @Override
- public void tickEngine() {
- // Ticks the Particle Emitter Control
- if (ticks < 1000) {
- ticks ++;
- } else {
- ticks = 0;
- }
- // Tick Particles
- tickElements();
- // Particle Emitter
- /*
- * The particle emitter works by limiting the number of instantiated particles
- * per 1000 ticks. The number of particles spawned per 1000 ticks is specified
- * by the particleLimit(int).
- */
- if (ticks % (1000 / particleLimit) == 0) {
- spawnParticle();
- }
- // Perform Object Removal
- checkStatus();
- process();
- }
- public void spawnParticle() {
- // Particles can range in size from 1 - 6
- int size = new Random().nextInt(7);
- // If the particle size is set as 0, set it as 1
- if (size < 1) size = 1;
- // X-Position may range from 0 - the frame's current width
- int xPos = new Random().nextInt(INIT.currentWidth - (size - 1));
- // Y-Position is always at -10, which is above the top of the screen.
- // This is so that the user cannot see the particles spawning on screen
- int yPos = -10;
- // Particles can have a speed of descent ranging from 1 - 4
- int ySpeed = new Random().nextInt(5);
- // If the descent speed is set as 0, set it as 1
- if (ySpeed < 1) ySpeed = 1;
- // This is used for the particle's color selector
- int random = new Random().nextInt(5);
- // Instantiate new particle
- particles.add(new Particle(xPos, yPos, ySpeed, size, -1, random));
- }
- @Override
- public void renderEngine(Graphics g) {
- // Set Background Color
- g.setColor(new Color(redHue, greenHue, blueHue));
- // Fill the screen
- g.fillRect(0, 0, INIT.currentWidth, INIT.currentHeight);
- // Render particles
- renderElements(g);
- }
- @Override
- public void tickElements() {
- for (Entity part:particles) {
- part.tick();
- }
- }
- @Override
- public void renderElements(Graphics g) {
- for (Entity part:particles) {
- part.render(g);
- }
- }
- @Override
- public void checkStatus() {
- for (Entity part:particles) {
- if (part.isDead()) {
- toKill.add(part);
- }
- }
- }
- @Override
- public void process() {
- for (Entity part:toKill) {
- particles.remove(part);
- }
- toKill.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement