// Project: 		ToyDesignPrototypes
// File: 			TraceBug.java
// Created by: 		rcarlsen, Feb 17, 2009

/*
 * Simply draw a trace then it will repeatedly redraw the trace
 */

// Imports
import processing.core.*;

public class TraceBug extends PApplet {
  
  Trace tracebot;
  
  public void setup() {
    size(800,600);
    smooth();
    
    // just create a default one
    initTracebot();
  }
  
  
  public void draw() {
    background(0);
    
    // the display method will handle the animation
    tracebot.display();
  }
  
  
  public void mousePressed(){
    tracebot = new Trace(mouseX,mouseY);
  }
  
  public void mouseDragged(){
    tracebot.update(mouseX,mouseY);
  }
  
  public void mouseReleased(){
    if(tracebot.getLength()<=1)
      initTracebot();
  }
  
  public void keyPressed(){
    switch(key){
    case 'l':
    case 'L':
      tracebot.displayLines = !tracebot.displayLines;
    }
  }
  
  public void initTracebot(){
    // with a length of 0 it's won't be displayed
    // manually making a second point.
    tracebot = new Trace(width/2,height/2);
    tracebot.update(width/2,height/2);
  }
  
  
  // Trace object //
  public class Trace {
    // setting up the initial location
    // and subsequent offsets for looping
    float x,y;
    public float offX=0;
    public float offY=0;
    
    // memory of the trace itself
    float[] stepsX = new float[10];
    float[] stepsY = new float[10];
    int index = 0;
    
    // timing stuff to animate the drawing of the trace
    int stepIndex = 1;
    int stepInterval = 30; // approximately the number of millis/frame at 30fps
    int stepTimer = 0;
    
    // a simple icon for the toy
    Car car = new Car();
    
    // some drawing parameters:
    float sWeight = 10.0f;
    int sColor = 255;
    
    boolean displayLines = true;
    
    Trace(float _x, float _y){
      offX = _x;
      offY = _y;
      x=0;
      y=0;
    }
    
    public void update(float _x, float _y){
      if(index >= stepsY.length-1){
        stepsY = expand(stepsY);
        stepsX = expand(stepsX);
      }
      
      stepsX[index] = _x - offX;
      stepsY[index] = _y - offY;
      index++;
    }
    
    public void displayAll(){
      for(int i=1;i<index;i++){
        stroke(sColor,150);
        strokeWeight(sWeight);
        line(stepsX[i-1],stepsY[i-1],stepsX[i],stepsY[i]);
      }
    }
    
    public void display(){
      if(index==0) return;
      
      pushMatrix();
      translate(this.offX,this.offY);

      if(displayLines){
        stroke(sColor,150);
        strokeWeight(sWeight);
        noFill();

        beginShape();
        vertex(stepsX[0],stepsY[0]);
        for(int i=1;i<stepIndex;i++){        
          vertex(stepsX[i],stepsY[i]);
          //car.update(stepsX[i],stepsY[i]);
        }
        endShape();
      }
      // draw the car icon:
      car.display();
      popMatrix();
      
      // update to the next segment if the time is right:
      if(millis()-stepTimer>stepInterval){
        //stepIndex = (stepIndex+1)%index;           
        stepIndex++;
        if(stepIndex > index){
          if(mousePressed == true){
            stepIndex = index;
          } else {
            this.offX += stepsX[index-1];
            this.offY += stepsY[index-1];

            stepIndex = 1;
          }
        }
        
        car.update(stepsX[stepIndex-1],stepsY[stepIndex-1]);
        stepTimer = millis();
      }
      

    }
    
    public int getLength(){
      return index;
    }
  }
  
  
  //simple class to draw a car icon
  // will rotate to automatically face the direction of movement
  public class Car {
    PImage img;
    float x,y;
    float a;
    
    public Car(){
      img = loadImage("bluecar.png");
      x=0;
      y=0;
    }
    
    public void update(float _x,float _y){
      float dX = _x-x;
      float dY = y-_y;
      if(dX != 0 || dY != 0)
        a = (a+atan2(dX,dY))/2.0f;
      x=_x;
      y=_y;
    }
    
    public void display(){
      pushMatrix();
      // this is being drawn within another translation matrix.
      translate(x,y);
      rotate(a);
      image(img,-img.width/2,-img.height/2);
      popMatrix();
    }
    
    
  }
  
}  
