#include "component_action_redirect.h"

/* DEPENDENCIES: Physics, Return */

/* Initializes redirect component. */
void 
component_action_redirect_init(ComponentActionRedirect* self, f32 multiplier, u32 sender)
{
	self->multiplier = multiplier;
	self->sender = sender;
}

/* Ticks redirect component. */
/* If redirection is specified, will get angle between the game's mouse
 * and the object, and launch towards that. */
void 
component_action_redirect_tick(ComponentActionRedirect* self, ECS* ecs)
{
	if (self->isRedirect)
	{
		ComponentPhysics* physics;
		ComponentActionReturn* actionReturn;
		ComponentActionDamage* actionDamage;
		vec3 velocity;
		f32 speed;

		physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);
		actionReturn = ecs_component_get(ecs, ECS_COMPONENT_ACTION_RETURN, self->component.id);
		actionDamage = ecs_component_get(ecs, ECS_COMPONENT_ACTION_DAMAGE, self->component.id);

		glm_vec3_copy(physics->velocity, velocity);

		speed = glm_vec3_norm(velocity) * self->multiplier;

		actionDamage->value += COMPONENT_ACTION_REDIRECT_DAMAGE_INCREMENT;

		if (!self->isGivenAngle)
		{
			physics->angle = mouse_angle_from_world_position_get
			(
				&ecs->game->input.mouse, 
				&ecs->game->renderer,
				physics->position
			);

		}
		else
			actionDamage->value *= COMPONENT_ACTION_REDIRECT_DAMAGE_MULTIPLIER;

		velocity[0] = -cos(physics->angle) * speed;
		velocity[1] = -sin(physics->angle) * speed;

		glm_vec3_copy(velocity, physics->velocity);

		actionReturn->lifetime = actionReturn->lifetimeMax;

		sound_play(&ecs->game->resources.sounds[SOUND_REDIRECT], SOUND_NO_PRIORITY);

		self->isGivenAngle = false;
		self->isRedirect = false;
	}
}
