Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*; //We need it for the Iterator
- PShader neon; //You can find the shader I'm using by searching "neon shader Processing" on Google!
- PImage moon;
- Grid grid;
- Mountain mountain;
- FManager fm; //The launcher
- void setup() {
- size(1920, 1080, P2D);
- neon = loadShader("neon.glsl");
- moon = loadImage("moon.png");
- grid = new Grid();
- mountain = new Mountain();
- fm = new FManager(new PVector(width * 0.5, height), FMM_BUGBURST_LARGE);
- }
- void draw() {
- background(0);
- grid.display();
- mountain.run();
- image(moon, width * 0.85, height * 0.03);
- fm.run();
- filter(neon);
- if (frameCount < 1000) {
- //saveFrame("movie/pic####.tif");
- }
- }
- //_____________________________________________________
- //Const
- //Several modes, sets of ruls, for the launcher to follow
- //FMM stands for FManager Mode
- final int FMM_SINGLE_STRAIGHT = 101;
- final int FMM_SINGLE_LARGE = 102;
- final int FMM_SMLBURST_STRAIGHT = 103;
- final int FMM_SMLBURST_LARGE = 104;
- final int FMM_BIGBURST_STRAIGHT = 105;
- final int FMM_BUGBURST_LARGE = 106;
- //_____________________________________________________
- class FManager {
- ArrayList<Firework> fireworkL;
- PVector pos;
- //How will the launcher behave?
- int freq; //The frequence at which we will launch new fireworks
- boolean burst; //Will we launch a single Firework, or several?
- int burstSeverity; //A little? A lot?
- PVector spread; //Where will the fireworks be launched? Always straig up, or with an angle? With how much vertical velocity?
- int spreadXmin;
- int spreadXmax;
- int spreadYmin;
- int spreadYmax;
- FManager(PVector _pos, int _mode) {
- fireworkL = new ArrayList<Firework>();
- pos = _pos.copy();
- this.setMode(_mode);
- }
- void update() {
- if (burst) {
- if (frameCount%freq < burstSeverity) {
- if (frameCount%freq == 0) {
- pos.x = random(width);
- }
- this.launchFirework();
- }
- } else if (frameCount%freq == 0) {
- pos.x = random(width);
- this.launchFirework();
- }
- Iterator<Firework> it = fireworkL.iterator();
- while (it.hasNext()) {
- Firework f = it.next();
- f.update();
- if (f.isDone() && !f.children()) {
- f.createChildren();
- }
- if (f.isFinished()) {
- it.remove();
- }
- }
- }
- void display() {
- for (Firework f : fireworkL) {
- f.display();
- }
- }
- void run() {
- this.update();
- this.display();
- }
- void setRules(PVector _spread, int _freq, int _burstSeverity) {
- freq = _freq;
- burst = true;
- burstSeverity = _burstSeverity;
- spread = _spread.copy();
- }
- void setRules(PVector _spread, int _freq) {
- freq = _freq;
- burst = false;
- burstSeverity = 0;
- spread = _spread.copy();
- }
- void setMode(int _mode) {
- int fMin = -60;
- int fMax = -60;
- int flMin = -10;
- int flMax = +10;
- int freqFix = 50;
- switch(_mode) {
- case FMM_SINGLE_STRAIGHT:
- freq = freqFix;
- burst = false;
- burstSeverity = 0;
- spreadXmin = -1;
- spreadXmax = 1;
- spreadYmin = fMin;
- spreadYmax = fMax;
- break;
- case FMM_SINGLE_LARGE:
- freq = freqFix;
- burst = false;
- burstSeverity = 0;
- spreadXmin = flMin;
- spreadXmax = flMax;
- spreadYmin = fMin;
- spreadYmax = fMax;
- break;
- case FMM_SMLBURST_STRAIGHT:
- freq = freqFix;
- burst = true;
- burstSeverity = 3;
- spreadXmin = -1;
- spreadXmax = 1;
- spreadYmin = fMin;
- spreadYmax = fMax;
- break;
- case FMM_SMLBURST_LARGE:
- freq = freqFix;
- burst = true;
- burstSeverity = 3;
- spreadXmin = flMin;
- spreadXmax = flMax;
- spreadYmin = fMin;
- spreadYmax = fMax;
- break;
- case FMM_BIGBURST_STRAIGHT:
- freq = freqFix;
- burst = true;
- burstSeverity = 10;
- spreadXmin = -1;
- spreadXmax = 1;
- spreadYmin = fMin;
- spreadYmax = fMax;
- break;
- case FMM_BUGBURST_LARGE:
- freq = freqFix;
- burst = true;
- burstSeverity = 10;
- spreadXmin = flMin;
- spreadXmax = flMax;
- spreadYmin = fMin;
- spreadYmax = fMax;
- break;
- }
- }
- void launchFirework() {
- //Firework(PVector _pos, PVector _vel, int _precision, int _radius, color _c, int _intensity, float _decaySpeed)
- int precisionF = floor(random(8, 11));
- float radiusF = random(15, 30);
- color cF = retroColor();
- int intensityF = floor(random(5, 10));
- spread = new PVector(random(spreadXmin, spreadXmax), random(spreadYmin, spreadYmax));
- fireworkL.add(new Firework(pos, spread, precisionF, radiusF, cF, intensityF));
- }
- }
- //_____________________________________________________
- //Const
- //Gravity
- final PVector GRAVITY = new PVector(0, 1.2);
- //_____________________________________________________
- class Firework {
- //Children
- ArrayList<Firework> fireworkL;
- //Physics (See "The Nature of Code", Daniel Shiffman, Force Chapter, can legaly be found for free on internet)
- // http://natureofcode.com/book/chapter-2-forces/
- //And the chapter 4 for the particle generator (In this sketch, Firework is a particle, while being a Particle
- // http://natureofcode.com/book/chapter-4-particle-systems/
- PVector pos;
- PVector vel;
- PVector acc = new PVector();
- float friction = 0.98;
- //Triangle? Square? Hexagon?
- int precision;
- color c;
- float radius;
- //Determines if we take care of it, or the entities a floor lower, his Fireworks in the ArrayList
- boolean hasChildren;
- //Was it the first firework entity to be launched into the sky?
- //TRUE: Trigger him on a drop on the y axis (y incrementing)
- //FALSE; Trigger it with a set amount of frames
- boolean original;
- //TRUE
- int trigger_dropSize = 20;
- float highestPoint = height; //The highest point he reached, useful to calculate the drop height.
- //FALSE
- float lifeSpan = 255; //The firework's remaining black powder
- float decaySpeed; //The speed at which the tank is being emptied
- //A last variable, to make sure the Firework won't grow in a exponential manner.
- int intensity;
- boolean finished = false;
- Firework(PVector _pos, PVector _vel, int _precision, float _radius, color _c, int _intensity) {
- pos = _pos.copy();
- vel = _vel.copy();
- precision = _precision;
- radius = _radius;
- c = _c;
- intensity = _intensity;
- original = true;
- fireworkL = new ArrayList<Firework>();
- }
- Firework(PVector _pos, PVector _vel, int _precision, float _radius, color _c, int _intensity, float _decaySpeed) {
- pos = _pos.copy();
- vel = _vel.copy();
- precision = _precision;
- radius = _radius;
- c = _c;
- intensity = _intensity;
- decaySpeed = _decaySpeed;
- original = false;
- fireworkL = new ArrayList<Firework>();
- }
- //In update, we will take care of the data, physics and such
- void update() {
- if (!hasChildren) {
- this.applyForce(GRAVITY);
- vel.add(acc);
- vel.mult(friction);
- pos.add(vel);
- acc.mult(0);
- if (highestPoint > pos.y) {
- highestPoint = pos.y;
- }
- } else {
- Iterator<Firework> it = fireworkL.iterator();
- while (it.hasNext()) {
- Firework f = it.next();
- f.update();
- if (f.isDone() && !f.children()) {
- f.createChildren();
- }
- }
- }
- }
- void display() {
- if (!hasChildren) {
- pushMatrix();
- translate(pos.x, pos.y);
- noFill();
- stroke(c, lifeSpan);
- beginShape(LINES);
- for (int i = 0; i < precision; i++) {
- float x = cos((TWO_PI/precision)*i)*radius;
- float y = sin((TWO_PI/precision)*i)*radius;
- float xp = cos((TWO_PI/precision)*(i+1))*radius;
- float yp = sin((TWO_PI/precision)*(i+1))*radius;
- vertex(x, y);
- vertex(xp, yp);
- }
- endShape();
- popMatrix();
- } else {
- for (Firework f : fireworkL) {
- f.display();
- }
- }
- }
- //The condition depends on the boolean "original", see the variables for more information
- boolean isDone() {
- if (original) {
- if (pos.y - trigger_dropSize > highestPoint) {
- return true;
- } else {
- return false;
- }
- } else {
- lifeSpan -= decaySpeed;
- if (lifeSpan < 0) {
- return true;
- } else {
- return false;
- }
- }
- }
- boolean children() {
- return hasChildren;
- }
- void createChildren() {
- int intensityNew = intensity - 1;
- if (intensity > 0) {
- int children;
- if (!original) {
- children = floor(random(intensity));
- } else {
- children = intensity;
- }
- float radiusNew = radius - random(5);
- radiusNew = (radiusNew < 1) ? 1 : radiusNew; //We're making sure the radius isn't going to go under 1, so that we can still see the next fireworks
- float velocity = radiusNew * 0.5;
- int precisionNew = precision - 1;
- precisionNew = (precisionNew < 2) ? 2 : precisionNew;
- color cNew = retroColor();
- float decaySpeedNew;
- if (!original) {
- decaySpeedNew = decaySpeed + random(10);
- } else {
- decaySpeedNew = 30;
- }
- float angleOffset = random(TWO_PI);
- //Firework(PVector _pos, PVector _vel, int _precision, int _radius, color _c, int _intensity, float _decaySpeed)
- for (int i = 0; i < children; i++) {
- float velX = cos((TWO_PI/children)*i + angleOffset)*velocity;
- float velY = sin((TWO_PI/children)*i + angleOffset)*velocity;
- fireworkL.add(new Firework(pos, new PVector(velX, velY), precisionNew, radiusNew, cNew, intensityNew, decaySpeedNew));
- }
- hasChildren = true;
- } else {
- finished = true;
- }
- }
- void applyForce(PVector _force) {
- acc.add(_force);
- }
- boolean isFinished() {
- if (!hasChildren) {
- return finished;
- } else {
- int compt = 0;
- for (Firework f : fireworkL) {
- if (f.isFinished()) {
- compt++;
- }
- }
- if (compt == fireworkL.size()) {
- return true;
- } else {
- return false;
- }
- }
- }
- }
- color retroColor() {
- return color(random(255), random(255), random(255));
- /*if (random(1) < 0.5) {
- return color(255, 0, 255);
- } else {
- return color(0, 255, 255);
- }*/
- }
- class Grid {
- float tileSize = 100;
- Grid() {
- }
- void display() {
- beginShape(LINES);
- stroke(255, 0, 255, 200);
- float sizeX = width;
- float sizeY = height;
- for (float x = 0; x <= sizeX; x += tileSize) {
- vertex(x, 0);
- vertex(x, sizeY);
- }
- for (float y = 0; y <= sizeY; y += tileSize) {
- vertex(0, y);
- vertex(sizeX, y);
- }
- endShape();
- }
- }
- class Mountain {
- float amp = 350;
- float yAverage = height*0.6;
- int nbPoint = 8;
- float n = random(255);
- float nSpeed = 0.01;
- float ex = 0.03;
- Mountain() {
- }
- void update() {
- n += nSpeed * (sin(frameCount*0.1) + 1);
- }
- void display() {
- fill(0);
- stroke(255, 0, 255, 200);
- beginShape();
- vertex(width, 0);
- vertex(0, 0);
- for (float x = 0; x <= width; x += width/nbPoint) {
- float y = yAverage + map(noise(n, x*ex), 0, 1, -amp/2, amp/2);
- vertex(x, y);
- }
- endShape(CLOSE);
- }
- void run() {
- this.update();
- this.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement