
#include "ball_list.hh"
#include "bonus_list.hh"

void				bonus_list_collide (struct s_bonus_list* bonus_list, struct s_pad* pad, struct s_ball_list* ball_list)
{
	struct s_bonus*	bonus;
	unsigned int	i;

	for (i = 0; i < BONUS_COUNT_ITEM; ++i)
		if (bonus_list->state[i] == BONUS_STATE_ALIVE)
		{
			bonus = &bonus_list->list[i];

			if ((bonus->mobile.x < bonus_list->x1 && mobile_speed_x (&bonus->mobile) < 0) ||
			    (bonus->mobile.x > bonus_list->x2 && mobile_speed_x (&bonus->mobile) > 0))
				mobile_bounce (&bonus->mobile, MOBILE_BOUNCE_VERT);

			if (bonus->coord.py < bonus_list->y1 - BONUS_DIST_INACTIVE)
				bonus_list->state[i] = BONUS_STATE_INACTIVE;
			else if (bonus_collide (bonus, pad->coord.px, pad->coord.py, pad->x, pad->coord.py, -pad->length - PAD_SIZE_RADIUS, pad->length + PAD_SIZE_RADIUS, -PAD_SIZE_RADIUS, PAD_SIZE_RADIUS))
			{
				CHD_PlayStart (bonus_list->sound[BONUS_COUNT_SOUND_FIXED + (int)bonus->type], 0);
				particle_create (bonus_list->particle, &bonus->coord, particle_color_cyan, particle_define_bonus_take, 0);

				switch (bonus->type)
				{
					case BONUS_TYPE_BALL_ONESHOT:
						ball_list_state (ball_list, BALL_STATE_ONESHOT);
					break;

					case BONUS_TYPE_BALL_SPLIT:
						ball_list_insert (ball_list, pad->coord.px, pad->coord.py);
					break;

					case BONUS_TYPE_BALL_TRAVERSE:
						ball_list_state (ball_list, BALL_STATE_TRAVERSE);
					break;

					case BONUS_TYPE_BALL_TYPE_HEAVY:
						ball_list_type (ball_list, BALL_TYPE_HEAVY);
					break;

					case BONUS_TYPE_BALL_TYPE_LIGHT:
						ball_list_type (ball_list, BALL_TYPE_LIGHT);
					break;

					case BONUS_TYPE_PAD_DEC:
						pad_length (pad, -1);
					break;

					case BONUS_TYPE_PAD_INC:
						pad_length (pad, 1);
					break;
				}

				bonus_list->state[i] = BONUS_STATE_INACTIVE;
			}
		}
}

void				bonus_list_define (struct s_bonus_list* bonus_list, struct s_particle* particle, struct s_surface* surface, float x1, float x2, float y1, float y2)
{
	unsigned int	i;

	for (i = 0; i < BONUS_COUNT_ITEM; ++i)
		bonus_list->state[i] = BONUS_STATE_INACTIVE;

	CHD_LoadSound ("data/sounds/bonus_loot.wav", FMOD_DEFAULT, &bonus_list->sound[BONUS_SOUND_LOOT]);
	CHD_LoadSound ("data/sounds/bonus_take_ball_oneshot.wav", FMOD_DEFAULT, &bonus_list->sound[BONUS_COUNT_SOUND_FIXED + BONUS_TYPE_BALL_ONESHOT]);
	CHD_LoadSound ("data/sounds/bonus_take_ball_split.wav", FMOD_DEFAULT, &bonus_list->sound[BONUS_COUNT_SOUND_FIXED + BONUS_TYPE_BALL_SPLIT]);
	CHD_LoadSound ("data/sounds/bonus_take_ball_traverse.wav", FMOD_DEFAULT, &bonus_list->sound[BONUS_COUNT_SOUND_FIXED + BONUS_TYPE_BALL_TRAVERSE]);
	CHD_LoadSound ("data/sounds/bonus_take_ball_weight_heavy.wav", FMOD_DEFAULT, &bonus_list->sound[BONUS_COUNT_SOUND_FIXED + BONUS_TYPE_BALL_TYPE_HEAVY]);
	CHD_LoadSound ("data/sounds/bonus_take_ball_weight_light.wav", FMOD_DEFAULT, &bonus_list->sound[BONUS_COUNT_SOUND_FIXED + BONUS_TYPE_BALL_TYPE_LIGHT]);
	CHD_LoadSound ("data/sounds/bonus_take_pad_dec.wav", FMOD_DEFAULT, &bonus_list->sound[BONUS_COUNT_SOUND_FIXED + BONUS_TYPE_PAD_DEC]);
	CHD_LoadSound ("data/sounds/bonus_take_pad_inc.wav", FMOD_DEFAULT, &bonus_list->sound[BONUS_COUNT_SOUND_FIXED + BONUS_TYPE_PAD_INC]);

	surface_load (surface, "data/bonus_ball_oneshot.png", &bonus_list->texture[BONUS_TYPE_BALL_ONESHOT]);
	surface_load (surface, "data/bonus_ball_split.png", &bonus_list->texture[BONUS_TYPE_BALL_SPLIT]);
	surface_load (surface, "data/bonus_ball_traverse.png", &bonus_list->texture[BONUS_TYPE_BALL_TRAVERSE]);
	surface_load (surface, "data/bonus_ball_type_heavy.png", &bonus_list->texture[BONUS_TYPE_BALL_TYPE_HEAVY]);
	surface_load (surface, "data/bonus_ball_type_light.png", &bonus_list->texture[BONUS_TYPE_BALL_TYPE_LIGHT]);
	surface_load (surface, "data/bonus_pad_dec.png", &bonus_list->texture[BONUS_TYPE_PAD_DEC]);
	surface_load (surface, "data/bonus_pad_inc.png", &bonus_list->texture[BONUS_TYPE_PAD_INC]);

	bonus_list->particle = particle;
	bonus_list->x1 = x1;
	bonus_list->x2 = x2;
	bonus_list->y1 = y1;
	bonus_list->y2 = y2;
}

void				bonus_list_draw (struct s_bonus_list* bonus_list)
{
	unsigned int	i;

	for (i = 0; i < BONUS_COUNT_ITEM; ++i)
		if (bonus_list->state[i] == BONUS_STATE_ALIVE)
			bonus_draw (&bonus_list->list[i]);
}

void				bonus_list_insert (struct s_bonus_list* bonus_list, float x, float y)
{
	unsigned int	i;
	unsigned int	t;

	for (i = 0; i < BONUS_COUNT_ITEM; ++i)
		if (bonus_list->state[i] == BONUS_STATE_INACTIVE)
		{
			t = rand () % BONUS_COUNT_TYPE;

			CHD_PlayStart (bonus_list->sound[BONUS_SOUND_LOOT], 0);

			bonus_define (&bonus_list->list[i], x, y, (enum e_bonus_type)t, bonus_list->texture[t]);
			bonus_list->state[i] = BONUS_STATE_ALIVE;

			particle_create (bonus_list->particle, &bonus_list->list[i].coord, particle_color_purple, particle_define_bonus_take, 0);

			break;
		}
}

void				bonus_list_loop (struct s_bonus_list* bonus_list)
{
	unsigned int	i;

	for (i = 0; i < BONUS_COUNT_ITEM; ++i)
		if (bonus_list->state[i] == BONUS_STATE_ALIVE)
			bonus_loop (&bonus_list->list[i], bonus_list->particle);
}

void				bonus_list_move (struct s_bonus_list* bonus_list)
{
	unsigned int	i;

	for (i = 0; i < BONUS_COUNT_ITEM; ++i)
		if (bonus_list->state[i] == BONUS_STATE_ALIVE)
			bonus_move (&bonus_list->list[i]);
}
