#include "component_action_on_delete_drop_goodies.h"

/* DEPENDENCIES: EntityGoodie, Physics */

static void _component_action_on_delete_drop_goodies_spawn(ComponentActionOnDeleteDropGoodies* self, ECS* ecs);

/* Spawns a goodie. */
static void
_component_action_on_delete_drop_goodies_spawn(ComponentActionOnDeleteDropGoodies* self, ECS* ecs)
{
	u32 goodie;
	f32 height;
	vec3 velocity;
	vec3 position;
	ComponentPhysics* physics;
	ComponentSprite* sprite;
	ComponentGameEntityType* gameEntityType;

	physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);

	goodie = ecs_entity_add(ecs);

	glm_vec3_copy(physics->position, position);

	velocity[0] = RANDOM_F32
	(
		COMPONENT_ACTION_ON_DELETE_DROP_GOODIES_VELOCITY_MIN,
		COMPONENT_ACTION_ON_DELETE_DROP_GOODIES_VELOCITY_MAX
	);

	velocity[0] += physics->velocity[0];

	velocity[1] = RANDOM_F32
	(
		COMPONENT_ACTION_ON_DELETE_DROP_GOODIES_VELOCITY_MIN,
		COMPONENT_ACTION_ON_DELETE_DROP_GOODIES_VELOCITY_MAX
	);

	velocity[1] += physics->velocity[1];

	velocity[2] = 0.0f;

	height = RANDOM_F32
	(
		COMPONENT_ACTION_ON_DELETE_DROP_GOODIES_HEIGHT_MIN,
		COMPONENT_ACTION_ON_DELETE_DROP_GOODIES_HEIGHT_MAX
	);

	if (RANDOM < COMPONENT_ACTION_ON_DELETE_DROP_GOODIES_HEIGHT_SPECIAL_CHANCE)
		height = COMPONENT_ACTION_ON_DELETE_DROP_GOODIES_HEIGHT_SPECIAL;

	entity_goodie_init
	(
		ecs,
		goodie,
		position,
		velocity,
		height
	);

	sprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, goodie);
	gameEntityType = ecs_component_get(ecs, ECS_COMPONENT_GAME_ENTITY_TYPE, self->component.id);

	sprite->atlas.index = GAME_ENTITY_GOODIE_INDEX[gameEntityType->type];
}

/* Initializes delete on touch component. */
void
component_action_on_delete_drop_goodies_init(ComponentActionOnDeleteDropGoodies* self, u32 count)
{
	self->count = count;
}

/* Initializes delete on touch component. */
void
component_action_on_delete_drop_goodies_delete(ComponentActionOnDeleteDropGoodies* self, ECS* ecs)
{
	for (s32 i = 0; i < (s32)self->count; i++)
		_component_action_on_delete_drop_goodies_spawn(self, ecs);
}
