#include "component_control_shoot.h"

/* DEPENDENCIES: Shoot, Ammo */

/* Initializes control shoot component. */
void
component_control_shoot_init(ComponentControlShoot* self, u32 cooldown)
{
	self->cooldownMax = cooldown;
	self->cooldown = self->cooldownMax;
}

/* Ticks control_shoot component. */
void
component_control_shoot_tick(ComponentControlShoot* self, ECS* ecs)
{
	ComponentAmmo* ammo;

	if (self->cooldown > 0)
	{
		self->cooldown--;
		return;
	}

	ammo = ecs_component_get(ecs, ECS_COMPONENT_AMMO, self->component.id);

	if (input_held(&ecs->game->input, INPUT_SHOOT) && !ammo->isOut)
	{
		ComponentActionShoot* actionShoot;

		actionShoot = ecs_component_get(ecs, ECS_COMPONENT_ACTION_SHOOT, self->component.id);

		actionShoot->isFire = true;

		self->cooldown = self->cooldownMax;

		ammo->value--;
	}
}
