#include "component_action_grip.h"

/* DEPENDENCIES: ActionGripped, Physics */

/* Out of this entity's collided objects, find grippable entities. Attach the first one found to this. */
static void
_component_action_grip_check(ComponentActionGrip* self, ECS* ecs)
{
	ComponentPhysics* physics;

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

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

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

		actionGripped = ecs_component_get(ecs, ECS_COMPONENT_ACTION_GRIPPED, id);

		if 
		(
			actionGripped && 
			!actionGripped->isGripped &&
			actionGripped->isAbleToBeGripped
		)
		{
			actionGripped->isGripped = true;
			actionGripped->gripperID = self->component.id;
			self->isGripping = true;
		}
	}
}

/* Ticks action_grip component. */
void
component_action_grip_tick(ComponentActionGrip* self, ECS* ecs)
{
	if (!self->isGripping)
		_component_action_grip_check(self, ecs);

}
