//// Project 3 
//// Dalin Scarver, 2015 

///////////////////////////////////////// . ANY OF THIS CODE //////////////////////////////////

String title=  "Project 3 )";
String author=  "Dalin Scarver 2015";
String news="";
float helpX, helpY;

int GoldAcheived=0;
float sunX=10, sunY=10;
float horizon;
float houseX, houseY, houseW=100, houseH=60;

float goldX, goldY;
float SoX, SoY, SoW=50, SoH=80;
float JoeX, JoeY, JoeW=70, JoeH=90;
float JoeStep;        // Wobbly walk.
int step;
float look;

float bird1X, bird1Y=20, goldrush1Y=1;
float bird2X, bird2Y=40, goldrush2Y=1;
float bird3X, bird3Y=60, goldrush3Y=1;
float bird4X, bird4Y=80, goldrush4Y=1;

void setup() {                                                            /* . */
  //// setup ////
  size( 900,900 );
  horizon=  height/4;
  sunY=  horizon/2;
  houseX=  width - 2*houseW;
  houseY=  horizon-houseH;
  
  reset();
}
void reset() {                                                           /* . */
  // Start at opposite sides. //  
  if (random(1) > 0.5) {
    SoX=  width-30;
    JoeX=30;
  } else {
    JoeX=  width-30;
    SoX=30;
  }
  SoY=  random( horizon,height-60 );
  JoeY=  random( horizon,height-60 );
  goldX=  random( width/4, width*3/4);
  
  mouseY=height;
}


void draw() {
  // next frame //
  scene();
  if (key == '?') {

    return;
  }
  gold();
  chucky();
  monster();
  birds();

  // Different step, every 15 frames. //
  step = step+1;
  if (step % 15 == 0) JoeStep=  random( -15, 15 );
  // Title, etc.
  fill(0);
  text( title, width/3, 20 );
  text( author, 20, height-15 );
  fill(255,255,0);
  text( "GoldAcheived=  " + GoldAcheived,  width-200, 25 );
  text( news, 20, 25 );
}

void scene() {                                                      /* . */
  // sky, sun, etc.
  background( #6108FF );
  fill(255,255,0);              // yellow sun
  ellipse(sunX,sunY, 50,50);
  fill( #029D2C);          // grass
  rectMode( CORNER );
  rect(0,horizon, width,height*3/4);
  // sun moves across sky.  (-10 each day.) //
  if (sunX > width) {
    sunX=0;
    sunY=  random( 10, horizon-20);
    GoldAcheived=  GoldAcheived-10;
  }
  sunX=  sunX+2;
  fill(0);
  text( sunX, sunX, sunY+40);  
  //// Trees.
  for (float x=10; x<width; x+=16) {
    tree( x,horizon );
  }
  fill( 255,0,0 );
  house( houseX,horizon-60, 100,60 );
}
void tree( float x, float y ) {
  //// Draw a tree (80 tall, 10 wide, with leaves 40x40)       /* . */
    fill( 150,0,0 );              // Brown trunk (40 high)
    rect( x,y-80, 10,80 );
    fill( 0,200,0 );              // Green leaves
    ellipse( x+5,y-100, 40,40 );
}  
void house( float x, float y, float w, float h ) {
  //// Draw a house
  fill(#69029D);
  rect(x,y, w,h);
  triangle( x,y, x+w,y, x+w/2,y-h/2 );
}
void birds() {                                                                      /* . */
  /// Draw & move 3 birds
  bird1X += random(3,5);
  bird2X += random(2,4);
 

                                              /* . */
  fill( 255,0,0 );    // bird 1:  red
  drawBird( bird1X, bird1Y+5-random(5) );


  
  fill( #FF08FC);
  drawBird( bird2X, bird2Y );



  fill( 0 );
  drawBird( bird3X, bird3Y );



  fill( 0,255,255 );
  drawBird( bird4X, bird4Y );


}
void drawBird( float x, float y ) {                                            /* . */
  //// draw one bird /////
  triangle( x,y, x-60,y-10, x-60,y+10 );        // body
  // Make the wings flap (every 1/2 second).
  float flap;
  if ( step/30 % 4 == 0 ) flap=  20;
  else if ( step/30 % 4 == 1 ) flap=  -10;
  else if ( step/30 % 4 == 2 ) flap=  -20;
  else flap=  +10;
  birdWing( x, y, flap );
}
void birdWing( float x, float y, float yup ) {                             /* . */
  // Draw the wing (up or down).
  triangle( x-20,y, x-40,y, x-40,y-yup );
}


void chucky() {                                                                         /* . */
  //// move & draw So ////
  SoX=  SoX + (goldX-SoX)/90;
  SoY=  SoY + (goldY-SoY)/90;
  if (SoY<horizon) SoY=  horizon;    // Cannot fly!
  //// draw So ////
  fill( #86FF08 );
  rectMode( CENTER );
  rect( SoX, SoY, SoW,SoH );
  fill(0);
  text( "So", SoX-15, SoY-10 );
  // head //
  look=  goldX>SoX ? +1 : -1;
  fill(#86FF08);
  head( SoX,SoY-SoH/2-10, 40 );
  // legs & arms
  strokeWeight(6);
  leg( SoX-25, SoY+SoH/2, 40, -JoeStep/2 );
  leg( SoX+25, SoY+SoH/2, 40, JoeStep/2 );
  
}
void monster() {                                                                           /* . */
  // move & draw So //
  // chase So
  JoeX=  JoeX + (SoX-JoeX)/120;
  JoeY=  JoeY + (SoY-JoeY)/120;
  if (JoeY<horizon+100) JoeY=horizon+200;
  // draw joe //
  drawjoe( JoeX, JoeY );
}
void drawjoe( float JoeX, float JoeY ) {                                            /* . */
  // draw joe // 
  fill( #9D0233 );
  rectMode( CENTER );
  rect( JoeX, JoeY, JoeW, JoeH );
  
  text( "joe", JoeX-20, JoeY-10 );
  // head & eyes, arms, legs //
  look=  JoeX<SoX ? +1 : -1;
  fill(0);
  head( JoeX,JoeY-JoeH/2-10, 50 );
  //--  ellipse(JoeX,JoeY-60, 50,60);    // head
 
  strokeWeight(20);
  leg(  JoeX-30, JoeY+45, 80, JoeStep );
  leg(  JoeX+30, JoeY+45, 80, -JoeStep );
  strokeWeight(1);
}

//////// Common methods for some body parts. ////////
void head( float x, float y, float w ) {                                            /* . */
  //// Draw head and eyes
  ellipse( x, y, w, w*1.25 );
  
  
}
void eye( float x, float y, float d, float look ) {                                            /* . */
  
  fill(255);
  ellipse( x,y, d*1.5,d );
  fill(0,0,255);
  ellipse( x+look*d/4,y, d/2,d/2 );
}
void arm( float len, float x, float y ) {                                            /* . */
  
  rectMode( CORNERS );
  fill(0);
  rect( x,y, x+len,y+15 );    // arm
  fill(155);
  ellipse( x+len,y+15, 30,30 );    // hand
}
void leg( float x, float y, float len, float step ) {
  //// draw one leg & foot.line( x, y, x+step, y+len );

}

void gold() {                                            /* . */

  if (goldY>horizon) {
    fill(random(200,250),random(100,200),random(150));
    ellipse( goldX,goldY, 40+random(20),40+random(20) );
    
    
  }
}
void scoring() {
}




void JoeHit() {                                            /* . */
  
  GoldAcheived += 10;
  background(0);    // Flash black
  JoeX=0;
  JoeY=  random(horizon,height);
  news=  "Gold has been Acheieved!";
}
void eat() {
  //// Monster eats chucky!!!!   Deduct 200 from GoldAcheived.
  GoldAcheived=  GoldAcheived - 200;
  background(#08B3FF);
  reset();
}


void mousePressed() {
  //// Click. ////
  news="";
  goldX=  mouseX;
  goldY=  mouseY;
  GoldAcheived=  GoldAcheived - 50;
  JoeX=0;
  JoeY=  random(20,height-20);
  news= "Gold is now at ("+ int(goldX) +","+ int(goldY) +")" ;
}




