////  Q1.java -- CST 112 Quiz #1
////  Vincent R. Castillio

////////  GLOBAL DECLARATIONS  //////// 
boolean hit = false; 
float horizon;
float flagX=width/2,flagY=20;
float buttonX=400, buttonY=20;
float buttonW=120, buttonH=90;
float targetX, targetY;                // Coordinates of the bullseye.
float dartX, dartY, dartDX, dartDY;    // Coordinates & velocity for dart.

int score=0;
int bullseye= 50; 

////////  SETUP //////// 
void setup() {
  size(800, 600);
  reset();
}

void reset() {
  horizon=  height / 4;
  targetX=  width-120;
  targetY=  random( horizon, height-100) ;
  dartDX=3;
  dartX=  100;
  dartY=  random( horizon, height-100 );
}

////////  NEXT FRAME //////// 
void draw() {
  scene();
  action();
  messages();
}


////////  Draw the scene //////// 
void scene() {
  background( 30, 120, 255 );
  
  fill( 0, 255, 30 );
  rect( 0,horizon, width,height*3/4 );        // grass 

  fill(255);
  rect( width/2, 20, 120,90 );                // flag
  fill(255, 10, 30);
  rect( width/2, 20, 120, 30);
  fill( 10, 30, 255);
  rect( width/2, 80, 120, 30);
  
  fill(255,0,0);
  ellipse( targetX,targetY, 200,200 );    // bullseye  
  fill(255);
  ellipse( targetX, targetY, 150, 150);
  fill(255, 0, 0);
  ellipse( targetX,targetY, 100,100 );
  fill(255);
  ellipse( targetX, targetY, 50, 50);  
}


////////  ACTION:  ////////
void action() {  
  // Move the dart
  dartX = dartX + dartDX;
  // Check if dart has reached target.
  if (dartX >= targetX-60) {
    dartX= targetX-60; 
    dartDX=0;  // STOP the dart
    hit = true;   
  }
  
  // Draw the dart, target, etc.  
  fill( 0, 200, 0 );
  triangle( dartX-20,dartY, dartX-40,dartY-20, dartX-40,dartY+20 );
  ellipse( dartX,dartY, 80,20 );
  fill(0);
  triangle( dartX+35,dartY-3, dartX+35,dartY+3, dartX+60,dartY );

}

//////// MESSAGES:  title, author, score, etc. //////// 
void messages() {
  fill(0);
  textSize(24);
  text( "CST 112 Quiz #1", width*3/4, 20 );
  text( "SCORE:", width*3/4,40 );
  textSize(18);
  text( "Vincent R. Castillo", 10, height-10 );  
}
 
//////// EVENT HANDLERS:  keys & mouse ////////
void keyPressed() {
  if (key == 'q') { exit(); }
  if (key == 'r') { reset(); }  
  
  if (key == 't') { targetY = random(horizon, height - 100); }
  if (key == 'd') { 
    dartX = 100; dartY = random(horizon, height); 
    dartDX = random(1+10);
  }
}
void mousePressed() {
  ////Flag reset on click////
  if (mouseX> buttonX-buttonW
    && mouseX< buttonX+buttonW
    && mouseY> buttonY - buttonH
    && mouseY< buttonY + buttonH ) {
      dartX = 100; dartY = random(horizon, height);
      dartDX = random(1, 10);
    }    
  if (mouseY<horizon) {
  }
  if (mouseY >= horizon
    && mouseX < width/2) {  
      dartY = mouseY; dartX = 100;
      dartDX = random(1+10);
    }
}

