import InterpolatedVar.*;
import SGGUI.*;
 
 
Vector rain = new Vector();
 
PImage guy,cloud;
float px = 300;
float py = 200;
float harmonic = 0;
float harmonicStep = 0;
float lpx;
 
PFont fnt;
 
int direction = 0;
int time = 0;
 
int cloudX = 200;
 
int dropsHit = 0;
 
SGGUI controller;
VerticalLayout settings;
 
SGSlider rate,wind,turbidity,wide;
SGLabel numHit;
 
 
InterpolatedVar cloudSep;
 
void setup(){
 
size(550,400,P3D);
fnt = loadFont("writing.vlw");
textFont(fnt);
textMode(SCREEN);
cloudSep = new CosInterpolatedVar(this, 0,30);
 
controller = new SGGUI(this,fnt);
settings = new VerticalLayout(controller,10,10,150);
rate = new SGSlider( "Rate",20,2,0,120);
wind = new SGSlider("Wind",1,.2,-7,14);
turbidity = new SGSlider( "Turbidity",2,.2,0,25);
wide = new SGSlider( "Cloud Wide",100,5,0,300);
numHit = new SGLabel("Drops Hit",0);
settings.add(rate);
settings.add(wind);
settings.add(turbidity);
settings.add(wide);
settings.add(numHit);
controller.add(settings);
guy = loadImage("poenderous.png");
cloud = loadImage("cloud.png");
textureMode(NORMALIZED);
 
 
}
 
 
void draw(){
 
time++;
background(100,100,200);
 
noStroke();
pushMatrix();
translate(px,height-70 + harmonic);
if (lpx > px) direction = 1;
if (lpx < px) direction = 2;
beginShape(QUADS);
texture(guy);
if (direction == 2){
vertex(-40,-100,0,0,0);
vertex(40,-100,0,1,0);
vertex(40,100,0,1,1);
vertex(-40,100,0,0,1);
}
else{
vertex(40,-100,0,0,0);
vertex(-40,-100,0,1,0);
vertex(-40,100,0,1,1);
vertex(40,100,0,0,1);
}
endShape();
popMatrix();
lpx = px;
 
 
for (int i =0; i < rate.getVal(); i++){
rain.add(new Drop(random(width/2 - wide.getVal()/2,width/2 + wide.getVal()/2),random(-turbidity.getVal()/10.f,turbidity.getVal()/10.f) + wind.getVal(),random(4,6)));
}
 
 
Drop rd;
loadPixels();
for (int i = rain.size()-1; i >=0; i--){
rd = (Drop)rain.elementAt(i);
rd.touch();
 
if (rd.y > height-1 || rd.x > width || rd.x < 0)
rain.removeElementAt(i);
else if (blue(pixels[(int)(rd.y)*width + (int)rd.x]) != 200 && rd.y > 200){
rain.removeElementAt(i);
dropsHit++;
}
 
}
 
for (int i = rain.size()-1; i >=0; i--){
rd = (Drop)rain.elementAt(i);
rd.draw();
}
 
if (wide.getVal() > 150 && cloudSep.getTargetVal() == 0){
cloudSep.setVal(75);
}
else if (wide.getVal() < 150 && cloudSep.getTargetVal() == 75){
cloudSep.setVal(0);
}
image(cloud,width/2-cloud.width/2 - cloudSep.getVal(),0);
image(cloud,width/2-cloud.width/2 + cloudSep.getVal(),0);
numHit.setVal(dropsHit);
}
 
 
 
class Drop{
float x,y;
float dx,dy;
int col;
 
Drop(float x, float dx, float dy){
this.x = x;
y = 90;
this.dx = dx;
this.dy = dy;
col = (int)random(200,250);
}
 
void touch(){
x += dx;
y += dy;
}
void draw(){
stroke(220,220,col);
line(x,y,x+dx,y+dy);
}
 
 
}
 
 
void mouseDragged(){
if (controller.getMouseConsumed() == 0){
px += (mouseX-pmouseX);
harmonicStep += .18;
harmonic = 7*cos(harmonicStep);
}
}
 
void keyPressed(){
if (key == ' ') dropsHit = 0;
}