#include "component_control_lock.h"

/* DEPENDENCIES: ActionLock, ActionReturn */

static void _component_control_lock(ComponentControlLock* self, ECS* ecs);

static void
_component_control_lock(ComponentControlLock* self, ECS* ecs)
{
	ComponentActionLock* actionLock;
	ComponentActionReturn* actionReturn;

	/* Determine if they're yours, and then lock them. */
	for (s32 i = 0; i < (s32)ecs->lists[ECS_COMPONENT_ACTION_LOCK].components.count; i++)
	{
		actionLock =  ecs_component_from_index_get(ecs, ECS_COMPONENT_ACTION_LOCK, i);
		actionReturn =  ecs_component_get(ecs, ECS_COMPONENT_ACTION_RETURN, actionLock->component.id);

		if 
		(
			actionLock->sender == self->component.id &&
			!actionReturn->isReturning
		)
			actionLock->isLock = !actionLock->isLock;
	}
}

/* Ticks control lock */
void
component_control_lock_tick(ComponentControlLock* self, ECS* ecs)
{
	if (input_press(&ecs->game->input, INPUT_LOCK))
		_component_control_lock(self, ecs);
}
