#include "component_action_delete_on_touch_entity.h"

/* DEPENDENCIES: Physics */
/* This entity associated with this component will be deleted if it touches the specified entity. */

/* Initializes delete on touch component. */
void
component_action_delete_on_touch_entity_init(ComponentActionDeleteOnTouchEntity* self, ECS* ecs, u32 id)
{
	ComponentActEntity* actEntity;

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

	actEntity->id = id;
}

/* Initializes delete on touch component. */
void
component_action_delete_on_touch_entity_add(ComponentActionDeleteOnTouchEntity* self, ECS* ecs)
{
	ecs_component_add(ecs, ECS_COMPONENT_ACT_ENTITY, self->component.id);
}

/* Initializes delete on touch component. */
void
component_action_delete_on_touch_entity_delete(ComponentActionDeleteOnTouchEntity* self, ECS* ecs)
{
	ecs_component_delete(ecs, ECS_COMPONENT_ACT_ENTITY, self->component.id);
}

/* Ticks delete on touch component. */
void
component_action_delete_on_touch_entity_tick(ComponentActionDeleteOnTouchEntity* self, ECS* ecs)
{
	ComponentPhysics* physics;
	ComponentActEntity* actEntity;

	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)
		{
			ecs_entity_delete(ecs, self->component.id);
			break;
		}
	}
}
