// ---------------------------------------------------------
// Class: VehicleAchilles
// ---------------------------------------------------------
// Lover vehicles have the sensors wired through an
// inverter to the wheels on the same side.
// ---------------------------------------------------------

import processing.*;
import processing.core.*;

class VehicleAchilles extends Vehicle {
  PApplet par;
  float  tAttack=0;
  boolean addNewMid=true;
  float xDif, yDif;
  int whichSource;
  Source so;
  Source thisSo=null;
  boolean placed=false;
  float oldplace;
  float confused=0;
  // Constructor
  // -----------------------------
  VehicleAchilles(PApplet pa, float x, float y, float angle, float axle, PImage theimage) {
    super(pa,x,y,angle,axle, theimage);
    par=pa;
  }

  // Sensing logic, i.e. how the sensors are connected to the wheels
  // -----------------------------
  void doSenseLogic(SensoryField sfield, SensoryField ffield) {
    setVelocityL(sL.getSense(sfield,true));
    setVelocityR(sR.getSense(sfield,true));
    //par.println(sL.getSense(sfield,true));
  }

  float getSpeedRat(){

    return (float)2.0;
  }
  void doTortLogic(SensoryField tfield, SensoryField sfield) {
    // setVelocityL(sL.getSense(tfield,true));
    //setVelocityR(sR.getSense(tfield,true));
    // par.println(sL.getSense(tfield,false));
    tAttack= sL.getSense(tfield,false);

    if(tAttack>0.4&&addNewMid){
      par.fill(255);

      so = (Source) sfield.getSource(0); ///main goal
      oldplace=so.x;
      xDif=  this.x-so.x;
      yDif= this.y-so.y;
      //par.println("Xdif "+xDif);
      //        par.println("ydif "+yDif);



      sfield.addSource(new Source(this.x-xDif/2,this.y-yDif/2,tAttack*150,200));
      placed=true;

      whichSource=  sfield.sources.size()-1; //find out which source this guy places
      thisSo= (Source) sfield.getSource(whichSource);

      sfield.updateAll(par); 
      //setVelocityR(0);
      addNewMid=false;
      // par.rect(this.x-xDif/2,this.y-yDif/2,10,10);



      //      if (s.hitTest(this.x,this.y,1)) {}
      //      .setLocation(width,height,mouseX,mouseY);
      //      lightfield.removeSource(lightfield.sources.size()-1);
    }

    if (so!=null&&oldplace!=so.x&&placed) {
      // par.println("NOT EUQLAwhichsource? NOT EUQ "+whichSource);
      sfield.removeSource(sfield.sources.size()-1);
      sfield.updateAll(par); 
      addNewMid=true;
      placed=false;
    }

    if (thisSo!=null &&thisSo.hitTest(this.x,this.y,1)&&placed) {
      par.println("whichsource? "+whichSource);
      sfield.removeSource(whichSource);
      sfield.updateAll(par); 
      addNewMid=true;
      placed=false;
    }
  }



}

