#include "camera.hpp"
#include <numbers>
#include <cmath>

void CameraLOL::plan_run(Raycaster &rayview, int dir) {
  t = 0.0;
  targetX = rayview.$posX + int(rayview.$dirX * 1.5 * dir);
  targetY = rayview.$posY + int(rayview.$dirY * 1.5 * dir);
  targetDir = dir;
}

bool CameraLOL::play_run(Raycaster &rayview) {
  t += moveSpeed;
  rayview.$posX = std::lerp(rayview.$posX, targetX, t);
  rayview.$posY = std::lerp(rayview.$posY, targetY, t);
  return t >= 1.0;
}

void CameraLOL::plan_rotate(Raycaster &rayview, int dir) {
  t = 0.0;
  double angle_dir = std::numbers::pi * 0.5 * dir;

  targetDirX = rayview.$dirX * cos(angle_dir) - rayview.$dirY * sin(angle_dir);
  targetDirY = rayview.$dirX * sin(angle_dir) + rayview.$dirY * cos(angle_dir);

  targetPlaneX = rayview.$planeX * cos(angle_dir) - rayview.$planeY * sin(angle_dir);
  targetPlaneY = rayview.$planeX * sin(angle_dir) + rayview.$planeY * cos(angle_dir);

  targetDir = dir;
}

bool CameraLOL::play_rotate(Raycaster &rayview) {
  t += rotSpeed;
  rayview.$dirX = std::lerp(rayview.$dirX, targetDirX, t);
  rayview.$dirY = std::lerp(rayview.$dirY, targetDirY, t);
  rayview.$planeX = std::lerp(rayview.$planeX, targetPlaneX, t);
  rayview.$planeY = std::lerp(rayview.$planeY, targetPlaneY, t);

  return t > 1.0;
}

void CameraLOL::plan_strafe(Raycaster &rayview, int dir) {
  t = 0.0;

  targetX = rayview.$posX + int(-rayview.$dirY * 1.5 * dir);
  targetY = rayview.$posY + int(rayview.$dirX * 1.5 * dir);
}

bool CameraLOL::play_strafe(Raycaster &rayview) {
  t += moveSpeed;
  rayview.$posX = std::lerp(rayview.$posX, targetX, t);
  rayview.$posY = std::lerp(rayview.$posY, targetY, t);
  return t >= 1.0;
}
