///Project 4:

String author= "Chandni Ochani";
String title= "Dog Race";

Button n1, n2, n3, n4;
Dog g1, g2, g3;
color c1,c,c3;
bird b;
Varmit ry, cyber;

String name1= "Dawn";
String name2= "Mylo";
String name3= "Simba";


float track1= 150, track2=250, track3=350;
float start=100, finish=500, horizon=track1-50;
float xNews=350, yNews=horizon+25;
int winner=0;
int count=0;

void setup() {
  size( 640, 480 );
  init();
  reset();
}
void init() {
  n1 = new Button( name1, 15, 10 );
  n2 = new Button( name2, 15, 40 );
  n3 = new Button( name3, 15, 70 );
  n4 = new Button("ALL", 15, 100 );
  
  g1 = new Dog( name1, start, track1+30 );
  g2 = new Dog( name2, start, track2+30 );
  g3 = new Dog( name3, start, track3+30 );
   
    color(0);
    color(255);
}
void reset() {
  g1.x= start;
  g2.x= start;
  g3.x= start;
}

void draw(){
  scene();
  showAll();
  messages();
}

class bird{
  float xPos, yPos, ySpeed;
  bird(){
    xPos=200;
    yPos=400;
  }
  void drawBird(){
    stroke(250);
    noFill();
    strokeWeight(3);
    ellipse(xPos,yPos,30,30);
  }
  void jump(){
    ySpeed=-12;
  }
  void drag(){
    ySpeed+=0.5;
  }
  void move(){
    yPos+=ySpeed;
  }
}


void scene() {
  background( 0,0,195 );    
  fill( 0,200,0 );          
  rect( 0, horizon, width, height-horizon );
  
  fill(10);
  text( "Track #1", start-50, track1 );
  text( "Track #2", start-50, track2 );
  text( "Track #3", start-50, track3 );
  fill( 250, 200, 200 );
  rect( start, track1, finish-start+100, 80 );
  rect( start, track2, finish-start+100, 80 );
  rect( start, track3, finish-start+100,80 );

  stroke( 255,0,0 );
  strokeWeight( 8 );
  line( finish+100,track1-10, finish+100,height-40 );
  strokeWeight( 1 );
  stroke(0);
  
  n1.show();
  n2.show();
  n3.show();
  n4.show();
}

void showAll() {
  g1.show();
  g2.show();
  g3.show();
  fill(0);
  text((int)(g1.x-start),finish+100,track1);
  text((int)(g2.x-start),finish+100,track2);
  text((int)(g3.x-start),finish+100,track3);
}

void messages() {
  textSize(30);
  text( title, width/3, 30 );
  textSize(12);
  text( author, 10, height-5 );
  
  if (g1.x > finish) {
    winner=1;
    textSize(24);
    fill(g1.c);
    text( g1.name+ " won the race!", xNews, yNews );
  } else if (g2.x > finish) {
    winner=2;
    textSize(24);
    fill(g2.c);
    text( g2.name+ " wins the race!", xNews, yNews );
  } else if (g3.x > finish) {
    winner=3;
    textSize(24);
    fill(g3.c);
    text( g3.name+ " wins the race!", xNews, yNews );
  } else {
    winner=0;
  textSize(12);
  fill(150);
  text(("No winner,yet"), xNews, yNews);
  }
}

void keyPressed() {
  if (key == 't') { exit(); }
  if (key == 'e') { reset(); }
  if (key == 'm') { moveAll(); }
  
  // Cheat! //
  if (key == '1') { g1.x += random(99); }
  if (key == '2') { g2.x += random(99); }
  if (key == '3') { g3.x += random(99); }
}
void moveAll() {
    g1.move();
    g2.move();
    g3.move();
}

void mousePressed() {
    if(n1.clicked())g1.move();
    if(n2.clicked())g2.move();
    if(n3.clicked())g3.move();
    if(n4.clicked())moveAll();
}

class Button
{
  float x, y;
  float w=90, h=25;
  String name;
  color c=color(255);
  
  Button() {  };
  Button( String name, float x, float y ) {
    this.name= name; 
    this.x= x; 
    this.y= y;
  }
  
  void show() {
    fill(c);
    rect( x, y, w, h );
    fill(155,155,0);
    text("Move"+name,x+5,y+20);
  }
  boolean clicked() {
    return hit( mouseX, mouseY );
  }
  boolean hit( float xx, float yy) {
    return false;
  }
}

class Dog
{
  float x, y;
  String name;
  color c;
  float wag=4;
  
  Dog() {  };
  Dog( String name, float x, float y ) {
    this.name= name; this.x= x; this.y= y;
    fill (255,0,255);
    rect(x,200,100,20);
    
  }
  
  void show() {
    fill(c);
    text( name, x+10, y+20 );
  }
  void move() {
  }
}

class Varmit
{
  float x, y;
  color c=0;
  void show() {
  }
  void move() { 
  }
}