/*
 * ball.cpp
 *
 *  Created on: 11. dec. 2016
 *      Author: dusted
 */

#include <cmath>
#include "ball.hpp"

Ball::Ball(SDL_Texture *tex, SDL_Rect rect, float x, float y, float velx, float vely)
{
    this->rect = rect;
    this->posx = x;
    this->posy = y;
    this->velx = velx;
    this->vely = vely;
    this->texture = tex;
}

void Ball::move(float time)
{
    this->posx += this->velx * time;
    this->posy += this->vely * time;

    //if (this->vely < 50)
    //this->vely += 1 * time;
}

void Ball::draw(SDL_Renderer *ren)
{
    this->rect.x = int(this->posx);
    this->rect.y = int(this->posy);
    SDL_RenderCopy(ren, this->texture, NULL, &this->rect);
}

float pyDist(float x1, float y1, float x2, float y2)
{
    float a = (x1 > x2) ? x1 - x2 : x2 - x1;
    float b = (y1 > y2) ? y1 - y2 : y2 - y1;
    float c = std::sqrt(a * a + b * b);
    return c > 0 ? c : -c;
}

float Ball::distTo(Ball *b)
{
    return pyDist(this->posx + this->rect.w / 2.0, this->posy + this->rect.w / 2.0,
                  b->posx + b->rect.w / 2.0, b->posy + b->rect.w / 2.0);
}

void Ball::reactInsideBox(int fx, int fy, int tx, int ty)
{

    if (this->posx < fx)
    {
        this->velx *= -1.0;
        this->posx = fx;
    }
    else if (this->posx + this->rect.w > tx)
    {
        this->velx *= -1.0;
        this->posx = tx - this->rect.w;
    }

    if (this->rect.y < fy)
    {
        this->vely *= -1.0;

        this->posy = fy;
    }
    else if ((this->rect.y + this->rect.h) > ty)
    {
        this->vely *= -1.0;
        this->posy = ty - this->rect.h;
    }

        /*if (fabs(this->vely) < 10)
	{
	    this->vely *= 5;
	}
    }

    if (fabs(this->velx) < 6)
    {
	this->velx *= 5;
    }*/
    }
