#include "test.h"

// Constructor
Test::Test(SDL_Renderer *renderer, Screen *screen, Asset *asset, Debug *debug)
{
	// Copia la dirección de los objetos
	this->renderer = renderer;
	this->screen = screen;
	this->asset = asset;
	this->debug = debug;

	// Inicializa variables
	for (int i = 0; i < 4; ++i)
	{
		point_t p;
		p.x = rand() % 256;
		p.y = rand() % 192;
		p.vx = (float)((rand() % 10) + 1) / 10.0f;
		p.vy = (float)((rand() % 10) + 1) / 10.0f;
		rand() % 2 == 0 ? p.dx = -1 : p.dx = 1;
		rand() % 2 == 0 ? p.dy = -1 : p.dy = 1;
		p.vx *= p.dx;
		p.vy *= p.dy;
		points.push_back(p);
	}
}

// Destructor
Test::~Test()
{
}

// Actualiza las variables
void Test::update()
{
	for (int i = 0; i < (int)points.size(); ++i)
	{
		points[i].x += points[i].vx;
		points[i].y += points[i].vy;

		if (points[i].x > 255)
		{
			points[i].x = 255;
			points[i].vx = -(float)((rand() % 10) + 1) / 10.0f;
		}
		else if (points[i].x < 0)
		{
			points[i].x = 0;
			points[i].vx = (float)((rand() % 10) + 1) / 10.0f;
		}

		if (points[i].y > 191)
		{
			points[i].y = 191;
			points[i].vy = -(float)((rand() % 10) + 1) / 10.0f;
		}
		else if (points[i].y < 0)
		{
			points[i].y = 0;
			points[i].vy = (float)((rand() % 10) + 1) / 10.0f;
		}
		std::string text = "P" + std::to_string(i) + ": x=" + std::to_string(points[i].x).substr(0,3) + " y=" + std::to_string(points[i].y).substr(0,3) + " vx=" + std::to_string(points[i].vx).substr(0,3) + " vy=" + std::to_string(points[i].vy).substr(0,3);
		debug->add(text);
	}
}

// Dibuja en pantalla
void Test::render()
{
	SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
	line_t l1 = {(int)points[0].x, (int)points[0].y, (int)points[1].x, (int)points[1].y};
	line_t l2 = {(int)points[2].x, (int)points[2].y, (int)points[3].x, (int)points[3].y};
	SDL_RenderDrawLine(renderer, l1.x1, l1.y1, l1.x2, l1.y2);
	SDL_RenderDrawLine(renderer, l2.x1, l2.y1, l2.x2, l2.y2);

	SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
	SDL_Point p = checkCollision(l1, l2);
	SDL_RenderDrawPoint(renderer, p.x, p.y);
}