#include "racecar.h"

Racecar::Racecar(float maxSpeed, float acceleration, float steerSpeed)
    : maxSpeed(maxSpeed), acceleration(acceleration), steerSpeed(steerSpeed), dx(0), dy(0), steeringAngle(0) {
    if (!font.loadFromFile("cascaydia.otf")) {
        std::cerr << "Failed to load font" << std::endl;
        exit(1);
    }

    carText.setString("|:=:>"); // Replace "o" with a racecar emoji or any desired character
    carText.setFont(font);
    carText.setCharacterSize(12);
    carText.setFillColor(sf::Color::White);
    
    speed = 0;
}

void Racecar::update(float dt) {
    carText.move(speed * dt * cos(steeringAngle), speed * dt * sin(steeringAngle));
}

void Racecar::accelerate(float dt) {
    if (speed < maxSpeed) {
        speed += acceleration * dt;
    }
}

void Racecar::decelerate(float dt) {
    if (speed < 0) {
        speed += 3 * acceleration * dt;
    } else if (speed > 0) {
        speed -= 3 * acceleration * dt;
    }
}

void Racecar::steer(float dt, float direction) {
    steeringAngle += steerSpeed * direction * dt;
    carText.setRotation(steeringAngle * 180 / M_PI);
}

void Racecar::brake(float dt) {
    if (speed > 0) {
        speed -= 9 * acceleration * dt;
    }
    if (speed < 0) {
        speed += 9 * acceleration * dt;
    }
}

void Racecar::reverse(float dt) {
    if (speed > -maxSpeed / 2) {
        speed -= 1.5 * acceleration * dt;
    }
}

sf::Text Racecar::getDrawable() {
    return carText;
}

void Racecar::setPosition(float x, float y) {
    carText.setPosition(x, y);
}

float Racecar::getSpeed() const {
    return speed;
}

