
#include "bonus.h"

char	bonus_collide (struct s_bonus* bonus, float x_old, float y_old, float x_new, float y_new, float w_min, float w_max, float h_min, float h_max)
{
	return collide (bonus->coord.px, bonus->coord.py, bonus->mobile.x, bonus->mobile.y, -BONUS_SIZE_WIDTH, BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, BONUS_SIZE_HEIGHT, x_old, y_old, x_new, y_new, w_min, w_max, h_min, h_max);
}

void	bonus_define (struct s_bonus* bonus, float x, float y, enum e_bonus_type type, GLuint texture)
{
	coord_set_pos (&bonus->coord, x, y, 0);
	coord_set_rot (&bonus->coord, 0, 0, 0);

	mobile_define (&bonus->mobile, x, y, BONUS_ANGLE_INITIAL + BONUS_ANGLE_RANDOM * (rand () % 2000 * 0.001f - 1.0f), BONUS_SPEED_INITIAL);

	bonus->dx = BONUS_ANGLE_ROTATE * (rand () % 2000 * 0.001f - 1.0f);
	bonus->dy = BONUS_ANGLE_ROTATE * (rand () % 2000 * 0.001f - 1.0f);
	bonus->dz = BONUS_ANGLE_ROTATE * (rand () % 2000 * 0.001f - 1.0f);
	bonus->effect = 0;
	bonus->texture = texture;
	bonus->type = type;
}

void	bonus_draw (struct s_bonus* bonus)
{
	coord_item (&bonus->coord);
		glColor3f (1, 1, 1);
		glEnable (GL_TEXTURE_2D);
		glBindTexture (GL_TEXTURE_2D, bonus->texture);

		glBegin (GL_QUADS);
			glTexCoord2f (0, 0);
			glVertex3f (-BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);
			glTexCoord2f (0, 1);
			glVertex3f (-BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 1);
			glVertex3f (BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 0);
			glVertex3f (BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);

			glTexCoord2f (0, 0);
			glVertex3f (-BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);
			glTexCoord2f (0, 1);
			glVertex3f (-BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 1);
			glVertex3f (BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 0);
			glVertex3f (BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);

			glTexCoord2f (0, 0);
			glVertex3f (-BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);
			glTexCoord2f (0, 1);
			glVertex3f (-BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 1);
			glVertex3f (-BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 0);
			glVertex3f (-BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);

			glTexCoord2f (0, 0);
			glVertex3f (BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);
			glTexCoord2f (0, 1);
			glVertex3f (BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 1);
			glVertex3f (BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 0);
			glVertex3f (BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);

			glTexCoord2f (0, 0);
			glVertex3f (-BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);
			glTexCoord2f (0, 1);
			glVertex3f (-BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 1);
			glVertex3f (BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 0);
			glVertex3f (BONUS_SIZE_WIDTH, -BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);

			glTexCoord2f (0, 0);
			glVertex3f (-BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);
			glTexCoord2f (0, 1);
			glVertex3f (-BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 1);
			glVertex3f (BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, BONUS_SIZE_DEPTH);
			glTexCoord2f (1, 0);
			glVertex3f (BONUS_SIZE_WIDTH, BONUS_SIZE_HEIGHT, -BONUS_SIZE_DEPTH);
		glEnd ();

		glDisable (GL_TEXTURE_2D);
	coord_stop ();
}

void	bonus_loop (struct s_bonus* bonus, struct s_particle* particle)
{
	mobile_compose (&bonus->mobile, BONUS_ANGLE_GRAVITY, BONUS_TEMP_WEIGHT * gl_vertex.speed);
	mobile_move (&bonus->mobile, gl_vertex.speed);

	if (bonus->effect <= gl_vertex.time)
	{
		particle_create (particle, &bonus->coord, particle_color_yellow, particle_define_bonus_fall, 0);

		bonus->effect = gl_vertex.time + BONUS_DELAY_EFFECT;
	}

	bonus->coord.rx += bonus->dx * gl_vertex.speed;
	bonus->coord.ry += bonus->dy * gl_vertex.speed;
	bonus->coord.rz += bonus->dz * gl_vertex.speed;
}

void	bonus_move (struct s_bonus* bonus)
{
	bonus->coord.px = bonus->mobile.x;
	bonus->coord.py = bonus->mobile.y;
}
