float GROUND = 400;

ArrayList<Drop> drops = new ArrayList<Drop>();
ArrayList<Drop> killem = new ArrayList<Drop>();

ArrayList<Fire> fires = new ArrayList<Fire>();
ArrayList<Fire> firekill = new ArrayList<Fire>();


void makeFires(){
   fires = new ArrayList<Fire>();
 for(int i = 0; i < 5; i++){
      fires.add(new Fire()); 
   } 
}

boolean playing = false;
boolean played = false;

float started;
float last = -1;
float best = 9999999;

void startGame(){
   makeFires(); 
  playing = true;
  started = millis();
}

void mousePressed(){
   if(!playing && !played) {
     startGame();     
   } 
}
void keyPressed(){
  if(key == ' '){
     startGame(); 
  }
}

void setup(){
  frameRate(50);
   size(500,500); 
   smooth();
   
  
   textSize(20);
   
}
Hose h = new Hose();

float round(float val, int dp)
{
  return int(val*pow(10,dp))/pow(10,dp);
} 

void draw(){
 background(100,100,200); 

  stroke(0);
 //building
 fill(128);
    rect(100,100,300,300); 
    fill(50);
  for(int a = 0; a < 5; a++){
  for(int b = 0; b < 5; b++){
    rect( 125 + a*54, 125 + b*50, 40,40);
  }    
  }
 
 
  noStroke();
 fill(75,75,0);
 rect(0,GROUND,500,100);
 
 
   fill(250); 


 if(! playing){
      textAlign(CENTER);
   if(!played) text("click to play",250,425); 
   else text("YOU PUT OUT THE FIRE!",250,425); 
   String title = "THE HOSE";
   if(played){
       title = "spacebar to replay "+title;
   }
   
   text(title,250,450); 
   if(played){
      text("LAST TIME:"+last+"   BEST TIME:"+best,250,475); 
   }
  
 } else {
      textAlign(CENTER);
   text("FIRE! FIRE! FIRE! FIRE!",250+random(-2,2),450+random(-2,2)); 

  float elapsed = round( (millis() - started) / 1000,2);
textAlign(LEFT);
   text("TIME: "+elapsed,200,475); 

   
 }
 
h.move();

  for(Fire f : fires){
    if(! f.check()){
       firekill.add(f);   
    } else {
    f.draw();
    } 
  }


  for(Drop d : drops){
     d.move();
    d.draw(); 
    if(d.kill()){
       killem.add(d); 
    }
  }
  
  
    h.draw();
  
  drops.removeAll(killem);
  killem.clear();
  fires.removeAll(firekill);
  firekill.clear();
  
  if(fires.size() == 0 && playing){
     played = true;
     playing = false;
     last =  round( (millis() - started) / 1000,2); 
      if(best > last) best = last;
  }
}

class Hose{
   float x=300,y=100;
   float gx=150,gy=GROUND;
   float a, xs, ys;
   float mx = x-1,my = y-1;
   float mys;
   void move(){
     

     if(mousePressed){
       drops.add(new Drop(this));
       mx = mouseX;
       my = mouseY;
       if(my > GROUND) my = GROUND;
       mys = 0;
     } else {
       mys += .1;
       my += mys;
       if(my >= GROUND) {
         my = GROUND; mys = 0;
       }
     }

     a = atan2(my-y, mx-x);
     if(abs(a - PI) < .1) a = 0;
       ys += sin(a)/3;
       xs += cos(a)/3; 


     ys+=.1;
//     print(xs);
            if(y >= GROUND && abs(xs) < .5) xs = 0;
   //  println("--"+xs);

     x += xs;
     y += ys;
     if(y >= GROUND){
       y = GROUND;
       xs *= .2;  

  //  print("slow "+xs);   
       ys *= .5;     
     }
     if(y < 0){
       y = 0;
       ys *= -1;       
     }
     if(x < 0){
       x = 0;
       xs *= -1;       
     }
     if(x > 500){
       x = 500;
       xs *= -1;       
     }


   }
   
  void draw(){
    noFill();
   stroke(255, 102, 0);

stroke(200,200,0);
strokeWeight(4);
bezier(x, y, mx, my, gx+100, gy,0,gy);


//stroke(255);
//line(x, y, mx,my);
stroke(100);
//println(a);
//line(x,y,x+cos(a)*20,y+20*sin(a));
//line(gx, gy, gx-100, gy);

  } 
}

class Fire{
 float  x = random(150,350);
 float y = random(150,GROUND-50);
  int health = 100;
  
  boolean check(){
     for(Drop d : drops){
        if(dist(x,y,d.x,d.y) < 10){
           health--; 

        }
     } 
     if(health <= 0){
        return false; 
     }
     return true;
  }
  
  void draw(){
    noStroke();
    fill(random(200,250),random(150,22),0);
     triangle(
       x + random(health), y + random(health),
       x + random(health), y + random(health),
       x + random(health), y + random(health)
     );
    
  }
  
  
}


class Drop{
  float x,y,xs,ys;
  int age = 0;
  Drop(Hose h){
     x = h.x ;
    y = h.y ; 
    xs = h.xs;//cos(h.a);
    ys = h.ys;//sin(h.a);
  }
  void draw(){
     fill(0,0,255);
     noStroke();
     
    rect(x,y,random(2,4),random(2,4)); 
  }
  boolean kill(){
     if(age >= 100){
        return true;
     } 
     return false;
  }
  void move(){
      x += xs;
      y += ys;
      ys += .1;

      if(y > GROUND){
         ys *= -.5; 
           xs *= -.5; 
       age++;
    
    }
      
      
  }
  
}