HashSet fallbits = new HashSet();
HashSet sitbits = new HashSet();
void setup(){
    size(400,400);
draw4(); 
}
int timer = 0;
int BIGTIMER = 200;
void mouseDragged(){
   sitbits.add(new spitbit(mouseX,mouseY));
timer = BIGTIMER;
//println("sitbits.add(new spitbit("+mouseX+","+mouseY+");");

}
void mouseClicked(){
   // splodeone(mouseX,mouseY,color(200,200,200));
}


void draw(){
  
  background(0);
timer --;
if(timer < 0){
draw4(); 
timer = BIGTIMER*3;;
} 
 
  Iterator i = sitbits.iterator();
  while(i.hasNext()){
     spitbit s = (spitbit)i.next();
    boolean remove = s.go();
   if(remove){
      i.remove(); 
   }
  }

   i = fallbits.iterator();
  while(i.hasNext()){
     glowbit g = (glowbit)i.next();
    boolean remove = g.move();
   g.draw(); 
   if(remove){
      i.remove(); 
   }
  }
}

class spitbit{
   float x,y; color c;
   float life;
   float MAXLIFE = 80;
  spitbit(float startx, float starty/*, color startc*/){
    float c1 = 127+((startx * 128)/width);
    float c2 = 127+((starty * 128)/height);
    float c3 =128;//random(128,255); 
    c = color(c1,c2,c3);
    x = startx; y = starty; //c = startc;
    life = MAXLIFE;
  }
  
  boolean go(){
     life-= .2;
    if(life <= MAXLIFE * .75) return true;
   if(int(random(5)) == 0){
       splodeone(x,y,c);
   }  
   return false;
  }
   
  
}


void splodeone(float x, float y, color c){
    float STARTRANGE = 3;

    fallbits.add(new glowbit(x,y,random(-STARTRANGE,STARTRANGE),random(-STARTRANGE * 2,STARTRANGE),c));        
}


void splode(float x, float y){
    float STARTRANGE = 3;
    int howmany = int(random(10,20));
    color c = color(random(128,255),random(128,255),random(128,255));
    for(int i = 0; i < howmany; i++){
      fallbits.add(new glowbit(x,y,random(-STARTRANGE,STARTRANGE),random(-STARTRANGE * 2,STARTRANGE),c));        
    }
}

float GRAVITY = .5;

class glowbit{
  int pieces = 4;
  float x[] = new float[pieces];
  float y[] = new float[pieces];
  float xs,ys;
  
  float r,g,b;
 glowbit(float startx,float starty,float startxs, float startys,color c){
  for(int i = 0; i < pieces; i++){
     x[i] = startx; y[i] = starty;  
  }
  xs = startxs;
  ys = startys;
  r = red(c);
  g = green(c);
  b = blue(c);
 } 
 
 
 
 
 //returns true if this bit can be removed
  boolean move(){
    for(int i = pieces - 1; i > 0; i--){
      x[i] = x[i-1];
      y[i] = y[i-1];
    } 

    ys = ys + GRAVITY;
    x[0] = x[0] + xs;
    y[0] = y[0] + ys;
    if(y[pieces-1] > height + 20){
       return true; 
    }
    return false;
  }
  void draw(){
     
   
    for(int i = 1; i < pieces; i++){
      float mult = ((pieces - i)*1.0) /( pieces*1.0);
      //println(mult);
      stroke(r*mult,g*mult,b*mult);
       line(x[i-1],y[i-1], x[i],y[i]);

    }

  }
  
}


void draw4(){
   String [] lines = loadStrings("four.txt");
   for(int i = 0; i < lines.length; i++){
      String line = lines[i];
     String[] parts = line.split("\t");
    float x = Float.parseFloat(parts[0]);
   float y = Float.parseFloat(parts[1]);
   
 sitbits.add(new spitbit(x,y)); 
   } 
}