#include "ProjectileComponent.hpp"

ProjectileComponent::ProjectileComponent(TransformComponent *_projectile, TransformComponent *_target, bool _homing, float _var)
{
    projectile = _projectile;
    target = _target;

    homing = _homing;

    if (homing)
    {
        speed = _var;
    }
    else
    {

        //TODO: Fix Rigidbody Functionality\

        angle = _var;

        // Vec2D projectileXZPos = projectile->GetPosition();
        // Vec2D targetXZPos = target->getPos();

        // float deltaY = targetXZPos.y - projectileXZPos.y;

        // float distance = targetXZPos.x - projectileXZPos.x;

        // float time = sqrt(((2.0f / gravity) * (deltaY - (tan(angle * 0.01745329) * distance))));

        // float vX = distance / time;
        // float vY = (deltaY - 0.5f * gravity * time * time) / time;

        // Vec2D v = Vec2D(vX, vY);
        // projectile->SetVelocity(v);
    }
}

ProjectileComponent::~ProjectileComponent()
{
}

void ProjectileComponent::Update(SDL_Event e, int frame, std::vector<std::string> broadcastList)
{
    ParseAndUpdateBroadcast(broadcastList);
    if (homing)
    {
        Vec2D projectileXZPos = projectile->getPos();
        Vec2D targetXZPos = target->getPos();

        Vec2D v = Normalize(Vec2D(targetXZPos.x - projectileXZPos.x, targetXZPos.y, projectileXZPos.y)) * speed;

        projectile->SetTransform(projectileXZPos.x + v.x, projectileXZPos.y + v.y);
    }
}

void ProjectileComponent::Render(SDL_Renderer *ren)
{
}
