#include "raylib.h"
#include "raymath.h"
#include "stdio.h"

#include "keys.h"

Texture ninja_texture;

float dash_time = 0;

void load_ninja()
{
    ninja_texture = LoadTexture("data/ninja.png");
}

float ninja_radius()
{
    return fmax(ninja_texture.width, ninja_texture.height)/2;
}

Vector2 ninja_dash(Vector2 pos, Vector2 to, float sec)
{
    static Vector2 target = {0};
    Vector2 dash = {0};
    if (sec <= EPSILON)
        return dash;
    if (key_pressed(KC_SHOOT)) {
        dash_time = sec;
        Vector2 diff = Vector2Subtract(to, pos);
        Vector2 scale = Vector2Normalize(diff);
        int dist = 200;
        scale.x *= dist;
        scale.y *= dist;
        target = Vector2Add(pos, scale);
    }
    if (dash_time > 0) {
        float dt = GetFrameTime();
        dash_time -= dt;
        dash.x = Lerp(pos.x, target.x, dt/sec) - pos.x;
        dash.y = Lerp(pos.y, target.y, dt/sec) - pos.y;
    }
    return dash;
}

float ninja_dash_time()
{
    return dash_time;
}

void draw_ninja(Vector2 pos, Camera2D *cam)
{
    Vector2 dash = ninja_dash(GetScreenToWorld2D(pos, *cam), GetScreenToWorld2D(GetMousePosition(), *cam), 0.12);
    cam->target = Vector2Add(cam->target, dash);
    Texture n = ninja_texture;

    Rectangle source = {0, 0, n.width, n.height};
    Rectangle dest = {pos.x, pos.y, n.width, n.height};
    Vector2 origin = {n.width/2, n.height/2};
    float angle = -Vector2LineAngle(pos, GetMousePosition());
    angle *= 180/PI;
    angle += 90;
    DrawTexturePro(n, source, dest, origin, angle, WHITE);
}