> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.KEy8l6REb7O/rev.471
 * 
 * authors: 
 *   Gustav Delius

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/* code adapted from http://www.openprocessing.org/visuals/?visualID=6706 */

/* @pjs preload="/static/uploaded_resources/p.907/royal_wedding.jpg"; */

int numHaces;
Haz[] h;
int counter; // this will be used to start one star after another
boolean started; // whether the first star has been created
int alphaRange = 500;

PImage img;

void setup () {
  img = loadImage ("/static/uploaded_resources/p.907/royal_wedding.jpg");
  size (450, 297);
  started = false; // this will be set to true once the first star
                   // is created by the first mouse click
  counter=0; 
  
  // create many stars and put them into the array h
  numHaces = 100;
  h = new Haz[numHaces]; 
  for (int i=0; i<numHaces; i++) {
    h[i] = new Haz(); 
  }
  
  background(0);
  text("Click to start rocket", 50, 250);
  noStroke();
  smooth ();
}

void draw () { 
  // update all stars that are already started
  if (started == true) {
    for (int i=0; i<counter; i++) {
      h[i].update();
    }
  }
}

void mousePressed () {
  // each time the mouse is pressed start the next star at the
  // location of the mouse, with random number of rays
  h[counter].start(mouseX, mouseY, int (random (30,100)));
  counter++;
  
  if (!started) {
    background(0); // to erase text hint
    started = true; // so that we actually start drawing something in draw()
  }
}

class Haz {

  int numrays; // number of rays
  Ray[] r; // array to hold all the rays
  Ray rocket; // an initial vertical ray to represent the rocket
  float rocketspeed;
  float flighttime;
  float maxrheight;
  float xIniHaz, yIniHaz; // center point of star
  float rocketxspeed;
  float rocketyspeed;

  void start (float xIniHazCon ,float yIniHazCon, int numraysCon) {
    xIniHaz=xIniHazCon;
    yIniHaz=yIniHazCon;
    numrays=numraysCon;
    
    // create the ray for the rocket
    rocketxspeed = random(-1,1);
    rocketyspeed = random(-4,-2);
    flighttime = rocketyspeed/0.05;
    maxrheight = rocketyspeed*flighttime/2;
    rocket = new Ray(xIniHaz, yIniHaz, rocketxspeed, rocketyspeed, 2, 500);
    
    // create all the rays and store them in the array r
    r= new Ray[numrays];
    for (int i=0; i<numrays; i++) {
      r[i] = new Ray(xIniHaz - rocketxspeed*flighttime, yIniHaz - maxrheight, random (-2, 2), random(-2.5, 0.5), 1, 60); 
    }
  }

  void update () {
    if (rocket.speedY < 0) { // we are in the rocket stage
      rocket.update();
    } else { // we are in the explosion stage
    // move and display all the rays
      for (int i=0; i<numrays; i++) {
        r[i].update();
      }
    }
  } 
}

class Ray {

  float x,y; // starting point for ray
  float speedX, speedY;
  color c; // color of the ray
  int thickness; // thickness of ray
  int lifetime; // lifetime of ray

  Ray (float xCon ,float yCon , float speedXCon, float speedYCon, int thickCon, int lifetimeCon) {
    x=xCon;
    y=yCon;
    speedX = speedXCon;
    speedY = speedYCon;
    thickness = thickCon;
    lifetime = lifetimeCon;
  }

  void update () {
    if (lifetime > 0) {
      // set the colour to that of the pixel of the photo
      c = img.get (int (x), int(y));
      x = x + speedX;
      y = y + speedY;
      speedY = speedY + 0.05;
      fill(c);    
      ellipse(x, y, thickness, thickness*speedY);
      lifetime--;
    }
  }

}