//// Mutt chases Bob.
//// B.A Martin:20219

String title="Mutt chases Bob", author="B.A.Martin:20219";
float horizon;

// Positions & speeds of objects.
float sunX, sunY;
float houseX=100, houseY=100;
float goldX, goldY;
float bobX, bobY, bobXspeed, bobYspeed;
float muttX, muttY, muttXspeed, muttYspeed;
float muttW=80, muttH=40;
// Bird & bomb
float birdX=200,birdY=50, birdXspeed=3,birdYspeed=0.05;
float bombY=0, bombYspeed=0, gravity=0.1;


color BLUE=color(0,0,255);
color BROWN=color(127,0,0);

//// SETUP:  Define screen size, set modes.
void setup()
{
  size( 800, 600 );
  horizon= height/4;
  reset();
}
void reset() {
  sunX=  width/2;               // Start the sun half-way across the screen.
  sunY=  50;
  bobX=  houseX;               // Start Bob at house.
  bobY=  houseY;
  bobXspeed=  random( 2,5 );    // Random speed.
  bobYspeed=  random( -3, +3 );
  muttX=  0;                    // Start Mutt at left.
  muttY=  height/2;
  goldX= random(50,width-50);
  goldY= random(horizon+20,height-40);
}


//// DRAW:  sky & sun plus creature
void draw()
{
  scene();
  action();
  show();
  messages();            // (Display the messages last.)
}


//// SCENE:  sky, sun, house.
void scene() {
  background( 150, 200, 250 );          // Blue sky
  fill( 255, 255, 0 );
  ellipse( sunX, sunY, 30, 30 );        // Yellow sun
  fill( 255, 0, 0 );
  rect( houseX,houseY, 100,50 );              // Red house
  triangle( 100,100, 200,100, 150,50 );
  // Grass
  fill( 50,150,50 );
  rect( 0,horizon, width,height-horizon );
}

//// MESSAGES.
void messages() {
  fill(0);
  text( title, width/3, 10 );
  String help1= "Click to reset Bob.\n";
  String help2= "Press 's' key to lower the sun, 'q' to quit.";
  text (help1+help2, 10, height/2 );
  // Also display the author and file name.
  text( author, 10, height-10 );
}

//// ACTION:  sun moves (then resets to random height)
void action() {
  if (sunX > width) {
    sunX=  0;
    sunY=  random( 20, 120 );
  }
  sunX=  sunX + 1;

  // Bob chases Gold.
  if (bobX>width-20 || bobX<0) { bobXspeed *= -1; }
  if (bobY>horizon || bobY<0) { bobYspeed *= -1; }
  bobXspeed= (goldX-bobX) / 60;
  bobYspeed= (goldY-bobY) / 60;
  bobX=  bobX + bobXspeed;
  bobY=  bobY + bobYspeed;
  // Got the gold!
  if ( dist(bobX,bobY, goldX,goldY) < 10) reset() ;
  			  
  // Mutt chases Bob
  muttXspeed= (bobX-muttX) / 30;
  muttYspeed= (bobY-muttY) / 30;
  muttX=  muttX + muttXspeed;
  muttY=  muttY + muttYspeed;
  // Bird
  if (birdX>width-20 || birdX<0) { birdX=0; birdY=random(30,horizon-30); }
  if (birdY>horizon || birdY<0) { birdYspeed *= -1; }
  birdX=  birdX + birdXspeed;
  birdY=  birdY + birdYspeed;  
  if (bombY>0) {
    bombY += bombYspeed;
    bombYspeed += gravity;
  }
  if (bombY>height) {
    bombY=0;
    bombYspeed=0;
  }
}

//// SHOW:  Bob & Mutt
void show() {
  goldShow();
  bobShow();
  muttShow();
  text( birdX, 10,10 );
  text( birdY, 10,20 );
  birdShow();
}
void goldShow() {
  fill( random(150,200), random(150,200), random(0,100) );
  ellipse( goldX, goldY, random(40,50), random(40,50) );
}
void bobShow() {
  // Draw Bob.
  fill( BLUE );
  rect( bobX,bobY, 50, 80 );                 // Blue creature
  ellipse( bobX+25, bobY-20, 40, 40 );       // Head on top
  // Eyes.
  fill( 255 );
  ellipse( bobX+15, bobY-25, 12, 12 );
  ellipse( bobX+35, bobY-25, 12, 12 );
  fill( 0, 150, 0 );
  ellipse( bobX+15, bobY-25, 4, 4 );
  ellipse( bobX+35, bobY-25, 4, 4 );
  //  
}
void muttShow() {
  // Draw Mutt.
  fill( BROWN );
  rect( muttX, muttY, muttW, muttH );
  float headX=muttX+muttW*3/4;
  rect( headX, muttY, muttW/2, -muttH/2 );
  // Eye.
  fill( 255 );
  ellipse( headX+15, muttY-muttH/2+5, 12, 12 );
}
void birdShow() {
  fill(200,0,200);
  triangle( birdX,birdY, birdX-60,birdY-12, birdX-60,birdY+12 );
  // Bomb
  if (bombY>0) {
    fill(0);
    ellipse( birdX, bombY, 20,30);
    // +++ fins/
  }
}

  
//// EVENT HANDLERS ////
void mousePressed() {
  reset();
  // Set the position (x,y)
  goldX=  mouseX;
  goldY=  mouseY;
}
void keyPressed() {
  if (key == 'q') { exit(); }
  if (key == 'r') { reset(); }
  if (key == 's') { sunY=  sunY + 50;  }
  if (key == 'b') { bombY=birdY; }
}