Alphaam_RobotaPsyche/finalProject/association_and_evolution.pde


ArrayList<Vehicle> vehicles = new ArrayList<Vehicle>();
FlowField f;
int numberVehicles = 50; // no longer a constant because they can be added or removed

// Debugging
boolean singleFrame = false; // For debugging: when true, do only one frame and then stop
float red;
float green;
float blue;


void setup() {
  size (900, 900);
  background(255,255,255);
  f = new FlowField(15); // resolution of grid

  // Make some vehicles to start with
  for (int i = 0; i < numberVehicles; i++) {
    vehicles.add(new Vehicle(
      random(width), random(height), // location
      random(-10, 10), random(-10, 10), // velocity
      i, 
      255, 
      0, 
      0, 
      0, 
      0, 
      0));// original vehicles are greennn)); // serial number is the index number
  }
}

void draw() {
  
  //setting up loop that will mutate the mainCharacters
  for (int i = vehicles.size() - 1; i >= 0; i--) {
    Vehicle v = vehicles.get(i);

    if (!v.alive) {
      println("removing dead vehicle");
      vehicles.remove(i);//once main character has learnt for the subject, subject is deleted
      
      DNA dna = v.getDNA();
      // mutate DNA
      dna.mutate();
      
      red=dna.ellipseRed;
      green=dna.ellipseGreen;
      blue=dna.ellipseBlue;

    }
  }

  // If the mouse button is pressed, allow the mouse to drag
  // around the closest vehicle
  if (mousePressed) {
    Vehicle closestVehicle = getClosestVehicleTo(mouseX, mouseY);
    PVector newLocation = new PVector(mouseX, mouseY);
    closestVehicle.setLocation(newLocation);
  }

  // Loop that checks other vehicle conditions as well as updating DNA data on mutations that have occured.
  for (Vehicle v : vehicles) {
    DNA dna = v.getDNA();
    v.checkEdges();
    v.avoidRedVehicles(vehicles);
    v.update();
    dna.ellipseRed=red;
    dna.ellipseGreen=green;
    dna.ellipseBlue=blue;

    v.display(dna.ellipseRed, dna.ellipseGreen, dna.ellipseBlue); // display the vehicle
  }


  if (singleFrame) {
    noLoop(); // for debugging
  }
}

/*
Functions used for debugging and understanding what's going on
 */

// mouseClicked() is called after a mouse button has been pressed and then released
// Inspect the vehicle that is closest to the mouse
void mouseClicked() {

  Vehicle closestVehicle = getClosestVehicleTo(mouseX, mouseY);
  closestVehicle.inspect();
}

// Return the vehicle closest to the given location
Vehicle getClosestVehicleTo(float x, float y) {

  Vehicle closestVehicle = vehicles.get(0);
  float closestVehicleDistance = width*height; //assume worst case
  PVector location = new PVector(x, y);

  for (Vehicle v : vehicles) {
    float distance = v.distanceTo(location);

    if ( distance < closestVehicleDistance ) {
      closestVehicle = v;
      closestVehicleDistance = distance;
    }
  }
  return closestVehicle;
}

void keyPressed() {
  switch (key) {
  case 'p':
    println("Loop paused; framecount = " + frameCount);
    noLoop();
    break;
  case 'r':
    singleFrame = false;
    loop();
    break;
  case 'd':
    frameRate -= 10;
    println("frameRate = " + frameRate);
    break;
  case 'u':
    frameRate += 10;
    println("frameRate = " + frameRate);
    break;
  case 's': // single frame
    singleFrame = true;
    // println("single frame; framecount = " + frameCount);
    loop();
    break;
  case 'n'://create new vehicles
    for (int i = 0; i < numberVehicles; i++) {
      vehicles.add(new Vehicle(
        random(width), random(height), // location
        random(-10, 10), random(-10, 10), // velocity
        i, 
        255, 
        0, 
        0, 
        0, 
        0, 
        0));// original vehicles are greennn)); // serial number is the index number
    }
    break;
  default:
    println("p = pause; r = run; d = slow down; u = speed up; s = single frame; n = new vehicle");
  }
}
