#include "component_action_gripped.h"

/* DEPENDENCIES: GameObject */

/* Gripped component. These objects can be gripped by entities with ActionGrip components. */

/* What if this object is ungripped (i.e., the gripped is destroyed?) This happens... */
static void
_component_action_gripped_ungrip(ComponentActionGripped* self, ECS* ecs)
{
	/* Just kill yourself, lol! */
	ecs_entity_delete(ecs, self->component.id);
}

/* When this object is being gripped, do this. */
static void
_component_action_gripped_set(ComponentActionGripped* self, ECS* ecs)
{
	ComponentGameObject* gameObject;
	ComponentGameObject* gripperGameObject;
	ComponentPhysics* physics;
	ComponentPhysics* gripperPhysics;
	ComponentSprite* shadowSprite;

	gripperGameObject = ecs_component_get(ecs, ECS_COMPONENT_GAME_OBJECT, self->gripperID);

	/* If we can't find the gripper game object, ungrip this. */
	if (!gripperGameObject)
	{
		_component_action_gripped_ungrip(self, ecs);
		return;
	}

	gameObject = ecs_component_get(ecs, ECS_COMPONENT_GAME_OBJECT, self->component.id);
	physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);
	gripperPhysics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->gripperID);
	shadowSprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, gameObject->shadowID);

	glm_vec3_copy(gripperPhysics->position, physics->position);
	glm_vec3_copy(gripperPhysics->velocity, physics->velocity);

	gameObject->height = gripperGameObject->height - physics->size[0];
	gameObject->isAffectedByGravity = gripperGameObject->isAffectedByGravity;
	gameObject->isBounce = gripperGameObject->isBounce;
	gameObject->heightVelocity = gripperGameObject->heightVelocity;

	glm_vec4_copy((f32*)COLOR_TRANSPARENT, shadowSprite->color);
}

/* Ticks grip component. */
void
component_action_gripped_tick(ComponentActionGripped* self, ECS* ecs)
{
	if (self->isGripped)
		_component_action_gripped_set(self, ecs);
}
