#include "Star.h"
#include "Model.h"


Star::Star(Vec3f position)
{
	scale = 1;

	model = Model::load("resources/models/star/I_star.obj");
	specialstar = Model::load("resources/models/redstar/I_star.obj");

	scale = 0.2;
	this->position = position;
	rotVal = 0;

	int rand = std::rand() % 100;

	special = false;
	if (rand <= 25)
	{
		special = true;
	}
}


Star::~Star()
{
	if (model)
		Model::unload(model);
	if (specialstar)
		Model::unload(specialstar);
}

void Star::draw()
{
	if (model)
	{
		glPushMatrix();

		glTranslatef(position.x, position.y, position.z);
		glRotatef(rotation.x, 1, 0, 0);
		glRotatef(rotation.y, 0, 1, 0);
		glRotatef(rotation.z, 0, 0, 1);
		glScalef(scale, scale, scale);

		model->draw();

		if (special)
		{
			glRotatef(rotVal, 0, 1, 0);
			glTranslatef(4.0f, 12.0f, 4.0f);
			glRotatef(rotVal, 0, 1, 0);
			glScalef(0.3f, 0.3f, 0.3f);

			specialstar->draw();
		}

		glPopMatrix();
	}

}

void Star::update(float deltaTime)
{
	rotation.y += deltaTime * 100.0f;
	rotVal += deltaTime * 100.0f * 1.5f;
}

bool Star::inObject(const Vec3f & point)
{
	if (!model)
		return false;
	Vec3f center = position + model->center;
	float distance = ((point.x - center.x) * (point.x - center.x) + (point.z - center.z)*(point.z - center.z));
	if (distance < model->radius*scale*model->radius*scale)
		return true;
	return false;
}
