// Animations -- Functions with arguments  (head3)
String title=  "Animations.";
String subtitle= "Functions with arguments (head3)";
String help=  "Click to place gold (or 'g' key) "
  + "\ns to start, r to reset, q to quit"
  + "\n    Also use keys:  f, g, h, s, t, etc.";
String news=  "";
String author=  "Prof.BAM";
int score;

float horizon=100;
float x1,y1,h1,d1, x2,y2,h2,d2, x3,y3,h3,d3, x4,y4,h4,d4, x5,y5,h5,d5;
color c1,c2,c3,c4,c5;
float zoogX, zoogY, zoogDX=2, zoogDY=1;
float houseX=50, houseY=100, houseW=160, houseH=80;
float goldX=0, goldY=0;
float monsterX, monsterY;

void setup() {
  size( 800, 600 );
  smooth();
  horizon=  height/4;
  reset();  
}
void reset() {
  randomTrees();
  randomHouse();
  goldX=goldY=0;        // Gold vanishes.
  zoogX=  houseX+50;
  zoogY=  houseY-100;
  zoogDX=zoogDY=0;
}
void start() {
  zoogDX=  random(1,3);
  zoogDY=  random(1,2);
}
void randomTrees() {
  h1=random(50,200); d1=h1*random(0.25,0.95); x1=random(0,width); y1=random(horizon-h1,height);
  h2=random(50,200); d2=h2*random(0.25,0.95); x2=random(0,width); y2=random(horizon-h2,height);
  h3=random(50,200); d3=h3*random(0.25,0.95); x3=random(0,width); y3=random(horizon-h3,height); 
  h4=random(50,200); d4=h4*random(0.25,0.95); x4=random(0,width); y4=random(horizon-h4,height);
  h5=random(50,200); d5=h5*random(0.25,0.95); x5=random(0,width); y5=random(horizon-h5,height);
  c1=color( random(50,100), random(200,250), random(50,150) );
  c2=color( random(50,100), random(200,250), random(50,150) );
  c3=color( random(50,100), random(200,250), random(50,150) );
  c4=color( random(50,100), random(200,250), random(50,150) );
  c5=color( random(50,100), random(200,250), random(50,150) );
}
void randomHouse() {
  houseX=  random(30,width-100);
  houseY=  random( horizon, height-40);
  houseW=  random( 100,200 );
  houseH=  random( houseW/4, houseW*3/2 );
}
void draw() {
  scene();
  action();
  messages();
}
void action() {
  // Move Zoog
  if (goldX>horizon) {
    zoogDX=  (goldX-zoogX) / random(60,90);
    zoogDY=  (goldY-zoogY) / random(90,120);
  }
  if (zoogX>width-50 || zoogX<20) { zoogDX=  -zoogDX; }
  if (zoogY>height-50 || zoogY<horizon) { zoogDY=  -zoogDY; }
  zoogX = zoogX + zoogDX;
  zoogY = zoogY + zoogDY;
  // Catch bad movements:
  if (zoogX<0 || zoogX>width || zoogY<0 || zoogY>height) reset();

  // Zoog gets the gold!
  if (dist(zoogX,zoogY, goldX,goldY) < 50) {
    background( 255,100,0 );    // Flash!
    score += 1;
    goldX=goldY=0;
    randomTrees();
    randomHouse();
    zoogX=houseX+houseW/2;
    zoogY=houseY-houseH/2;
    zoogDX=zoogDY=0;
  }

  // Draw Zoog:
  showZoog( zoogX, zoogY, 1 );
  showZoog( zoogX-30*zoogDX, 90+zoogY, 0.5 );
  
  
  trees();
}

void showZoog( float x, float y, float scale ) {  
  float d=40*scale, w=50*scale, h=80*scale;
  //
  fill( 255,150,80 );          // Orange
  ellipse( x,y, d,d );         // head  
  // Eyes
  float shift;
  if (zoogDX>0) shift=  2;
  else shift=  -2;
  eye( x-8, y-8, shift );
  eye( x+8, y-8, shift );
  
  // Coordinates of shoulder:
  float xx=  x - w/2;          // Left side of shoulder.
  float yy=  y + d/2;          // Top of shoulder.
  fill( 255, 255, 0 );
  rect( xx, yy, w, h );        // Body
  fill(0);
  text( "Zoog", xx+10, yy+15 );    // Name on shirt
  if (score>0) text( score, xx+w/3, yy+30 );
  
  // Arms & Legs
  float step=w/4, bottom=yy+h, foot=yy+h+h*2/3;

  // Animation (2-part)
  if ( x % 100 < 50 ) { step =  0.01*step; }
  if (zoogDX<0) step= -step; 
  // Arms
  limb( xx, yy+5, step/2, yy+h*2/3 );
  limb( xx+w, yy+5, step/2, yy+h*2/3 );
  // Legs
  limb( xx+8, bottom, step, foot );
  limb( xx+w-8, bottom, step, foot );
  float shoe= 24*scale;
  if (step<=0) shoe=  -shoe;
  shoe( xx+8+step,foot, shoe );
  shoe( xx+w-8+step,foot, shoe );
}
void limb( float x, float y, float step, float yy ) {
  strokeWeight(10);
  line( x,y, x+step,yy );
  strokeWeight(1);
}
void shoe( float x, float y, float shoe ) {
  fill( 120,0,0 );            // Brown shoe.
  rect( x,y+4, shoe,shoe/3 );
}  
void eye( float x, float y, float shift ) {
  fill( 255 );                  // White
  ellipse( x,y, 12,12 );
  fill( 0,0,255 );              // Blue
  ellipse( x+shift,y, 5,5 );
}


void scene() {
  background(150, 220, 250);
  fill( 150, 255, 200 );
  rect( 0, horizon, width, height );    // Grass.
  // Gold (if below horizon.
  if (goldX>horizon) {
    noStroke();
    goldCircle( goldX,goldY, 50 );
    goldCircle( goldX,goldY, 30 );
    goldCircle( goldX,goldY, 10 );
    stroke(1);
  }
  house( houseX, houseY, houseW, houseH );
  house( houseX+houseW+10, houseY-houseH-20, houseW/3, houseH/3 );
}
void house( float x, float y, float w, float h ) {
  fill(255,0,0);
  rect( x,y, w, -h );
  triangle( x-10,y-h, x+w/2,y-h-h/2, x+w+10,y-h );
  fill(0);
  rect( x+w/4,y, w/4, -h*3/4 );        // door
  rect( x+w*2/3,y-h/2, w/6, -h/3 );    // window
}
void trees() {
  tree( x1, y1, h1, d1, c1 );
  tree( x2, y2, h2, d2, c2 );
  tree( x3, y3, h3, d3, c3 );
  tree( x4, y4, h4, d4, c4 );
  tree( x5, y5, h5, d5, c5 );
}  
void tree( float x, float y, float h, float d, color c ) {
  fill( 150-h/4, 20+h/2, 20 );    // Brown trunk.
  rect( x-d/4,y+d/3, d/2,h );
  fill(c);
  ellipse( x,y, d,d );            // Green circle.
}
void goldCircle( float x, float y, float d ) {
    fill( random(200,250), random(200,250), random(20) );
    ellipse( x,y, random(d-10,d), random(d-10,d) );
}  

void messages() {
  fill(0);
  textSize(24);
  text( title, width/3, 24 );
  text( news, 10, 48 );
  textSize(12);
  text( subtitle, width/3, 36 );
  if (score>0) {
    text( "SCORE: "+score, width*2/3, 24 );
  }
  text( help, width*2/3, 48 );
  text( author, 10, height-20 );
}

void mousePressed() {
  if (mouseY<horizon+50) return;
  if ( dist(zoogX,zoogY, mouseX,mouseY)  <100) return;    // Cheating
  goldX=  mouseX;
  goldY=  mouseY;
}
void keyPressed() {
  if (key == 'q') exit();
  if (key == 'r') reset();
  if (key == 'g') {
    if (dist(zoogX,zoogY, goldX,goldY) < 100) return;
    goldX=random( 20, width-50 ); 
    goldY=random( horizon,height-50 ); 
  }
  if (key == 't') randomTrees();
  if (key == 'h') { randomHouse(); }
  if (key == 's') { start(); }
  if (key == 'f') { zoogDX=zoogDY=0; }
}
