#include "gameplay/camera.h"

// follow our target set using the setTarget. If there is no target set the
// camera will not move.
void Camera::update(double deltaTime) {
  if (target == nullptr)
    return;
  float smoothingFactor = 5.0f;
  // if (glm::distance(target->getCenter(), getCenterPos()) > 20.f)
  position += (target->getCenter() - getCenterPos()) * smoothingFactor *
              static_cast<float>(deltaTime);
}

glm::mat4 Camera::getViewMatrix() {
  return glm::lookAt(position, position + front, up);
}

glm::mat4 Camera::getProjectionMatrix() {
  return glm::ortho(0.f, viewPortW, viewPortH, 0.f);
}

void Camera::setViewportSize(float width, float height) {
  viewPortW = width;
  viewPortH = height;
}

// The local coordinates are the corrdinates relative to the camera.
const glm::vec3 Camera::worldToLocal(const glm::vec3 &worldCoordinates) {
  // return worldCoordinates - position;
  return glm::vec3(getViewMatrix() * glm::vec4(worldCoordinates, 1.0f));
}
