//CSC 123 Lab 11 base code for one point perspective exercize and class practice.
//by ZJ Wood
//This code currently draws a road and five very simple cars that drive down the road
//TODO: **create a new class called 'airplane' or 'saucer' that draws a flying ship that also heads off
//      into the distance  - you can even draw a little city or dome or destination if desired - make the
//      ship look different then the cars***
//TODO: add code such that when the mouse is clicked the cars and ships restart - do not 'new' variables, just 
//      reset variables are desired
//TODO: change the code so that the cars look better
//TODO: change the code so taht the cars do not run over one another (you may use a 'hack', ie have them
//      start at different times or a more complex method)

class car {
  PVector loc, stop, begin;
  PVector dir;
  float speed;
  float dist;
  color c;
  boolean alive;
  
  car(PVector start, PVector end) {
    begin = start.get();
    loc = start.get();
    stop = end.get();
    end.sub(start);
    dir = end.get();
    dist = dir.mag();
    dir.normalize();

    speed = random(1, 2);
    c = color(random(20, 240), random(10, 240), random(10, 240));
    alive = true;
  }
  
  void draw() {
    float more;
    PVector temp;
    
    temp = new PVector(stop.x, stop.y);
    temp.sub(loc);
    more = temp.mag();
    if (alive) {
      fill(c);
      pushMatrix();
        translate(loc.x, loc.y);
        scale( (more/dist)*1.6 );
        ellipse(0, 0, 40, 20);
      popMatrix();
    }
  }
  
  void drive() {
    float more;
    PVector temp;
    
    temp = new PVector(begin.x, begin.y);
    temp.sub(loc);
    more = temp.mag();
    if (more < dist) {
      loc.x += dir.x*speed;
      loc.y += dir.y*speed;
    } else {
      alive = false;
    }
  }  
    
}

car theCars[];
int numC;

void setup() {
  
  size(400, 400);
  smooth();
  numC = 5;
  
  theCars = new car[numC];
  
  for (int i=0; i < numC; i++) {
    theCars[i] = new car(new PVector(0, height), new PVector(width*.8, height/2));
  }
}

void draw() {
 background(175, 221, 245);
 //draw forground
 noStroke();
 fill(147, 107, 70);
 rect(0, height/2,  width, height/2);
 //draw road
 stroke(255);
 fill(0);
 triangle(width*.8, height/2, width*.2, height+20, -.2*width, height+20);
 
 for (int i=0; i < numC; i++) {
   theCars[i].draw();
   theCars[i].drive();
 }  
}