// Camera

class Camera {
  // References
  Sprite targetSprite;
  // Properties
  float x,y,zoom;
  float zoomTarget;
  float zNeutral;
  int framesLeftToCameraShake = -1;
  Camera() {
    reset();
  }
  void draw() {
    //Math.
    if (zoom != zoomTarget) {
      zoom += (zoomTarget-zoom) / 32;
    }
    
    // Targeting!
    if (targetSprite != null) {
      x += (targetSprite.x-x) / 32;
      y += (targetSprite.y-targetSprite.h-y) / 32;
    }
    else {
      x += (width*0.5-x) / 32;
      y += (height*0.5-y) / 32;
    }
    
    //zoom = mouseX/(float)width;

    //"Draw"
    float shakeOffset = 0;
    if (framesLeftToCameraShake-->0) shakeOffset = random(-framesLeftToCameraShake,framesLeftToCameraShake)*2;
    camera(x,y+shakeOffset,zNeutral*zoom,
    x, y+shakeOffset, 0,
    0, 1, 0);
  }
  void drawDefault() {
    //Draw at default position. This is used for menu screens.
    camera(width/2, height/2, zNeutral, 
    width/2, height/2, 0, 
    0, 1, 0);
  }
  void shake(int frames) {
    framesLeftToCameraShake = frames;
  }
  
  void goToTarget() {
    x = targetSprite.x;
    y = targetSprite.y-targetSprite.h;
  }
  void onReachedZoomTarget() {
    zoom = zoomTarget;
  }
  
  
  void reset() {
    x = width/2;
    y = height/2;
    zNeutral = (height/2.0) / tan(PI*60.0 / 360.0);
    zoomTarget = 1;
    zoom = zoomTarget;
  }
}

