#include "component_action_on_touch_give_goodies.h"

/* DEPENDENCIES: Goodies, ActEntity */
/* On deletion, give goodies to the ID belonging to ActEntity. */

/* Initializes action on touch give goodies component. */
void
component_action_on_touch_give_goodies_init(ComponentActionOnTouchGiveGoodies* self, u32 value)
{
	self->value = value;
}

/* Ticks aciton on touch give goodies component. */
void
component_action_on_touch_give_goodies_tick(ComponentActionOnTouchGiveGoodies* self, ECS* ecs)
{
	ComponentActEntity* actEntity;
	ComponentPhysics* physics;

	if (self->isGivenGoodies)
		return;

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

	if (physics->collisions.count == 0)
		return;

	actEntity = ecs_component_get(ecs, ECS_COMPONENT_ACT_ENTITY, self->component.id);

	for (s32 i = 0; i < (s32)physics->collisions.count; i++)
	{
		u32 id;

		id = *(u32*)vector_get(&physics->collisions, i);

		if (id == actEntity->id)
		{
			ComponentGoodies* goodies;
			
			goodies = ecs_component_get(ecs, ECS_COMPONENT_GOODIES, actEntity->id);

			goodies->value += self->value *= goodies->multiplier;

			sound_play(&ecs->game->resources.sounds[SOUND_GOODIE], SOUND_NO_PRIORITY);

			self->isGivenGoodies = true;
		}
	}
}
