#include "component_action_delete_on_touch_game_entity_type.h"

/* DEPENDENCIES: Physics, GameEntityType */
/* This entity associated with this component will be deleted if it touches the first entity with a matching game entity type. */

/* Initializes delete on touch component. */
void
component_action_delete_on_touch_game_entity_type_init(ComponentActionDeleteOnTouchGameEntityType* self, GameEntityType type)
{
	self->type = type;
}

/* Initializes delete on touch component. */
void
component_action_delete_on_touch_game_entity_type_add(ComponentActionDeleteOnTouchGameEntityType* 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_game_entity_type_delete(ComponentActionDeleteOnTouchGameEntityType* 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_game_entity_type_tick(ComponentActionDeleteOnTouchGameEntityType* self, ECS* ecs)
{
	ComponentPhysics* physics;

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

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

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

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

		gameEntityType = ecs_component_get(ecs, ECS_COMPONENT_GAME_ENTITY_TYPE, id);


		if (!gameEntityType)
			continue;

		if (gameEntityType->type == self->type)
		{
			ComponentActEntity* actEntity;
			actEntity = ecs_component_get(ecs, ECS_COMPONENT_ACT_ENTITY, self->component.id);

			actEntity->id = id;

			ecs_entity_delete(ecs, self->component.id);
			break;
		}
	}
}
