#include "entity_damage_number.h"

/* Initializes a player entity. */
void
entity_damage_number_init(ECS* ecs, u32 id, vec3 position, u32 damage)
{
	ComponentActionDeleteOnTimer* actionDeleteOnTimer;
	ComponentAnimationColorChange* animationColorChange;
	ComponentAnimationMove* animationMove;
	ComponentAnimationSquash* animationSquash;
	ComponentAnimationStretch* animationStretch;
	ComponentText* text;
	char string[ENTITY_DAMAGE_NUMBER_TEXT_MAX];
	f32 base;

	base = ENTITY_DAMAGE_NUMBER_SCALE + ((damage - 1) * ENTITY_DAMAGE_NUMBER_SCALE_MULTIPLIER);

	base = MAX(base, ENTITY_DAMAGE_NUMBER_SCALE_MAX);

	text = ecs_component_add(ecs, ECS_COMPONENT_TEXT, id);
	actionDeleteOnTimer = ecs_component_add(ecs, ECS_COMPONENT_ACTION_DELETE_ON_TIMER, id);
	animationColorChange = ecs_component_add(ecs, ECS_COMPONENT_ANIMATION_COLOR_CHANGE, id);
	animationMove = ecs_component_add(ecs, ECS_COMPONENT_ANIMATION_MOVE, id);
	animationSquash = ecs_component_add(ecs, ECS_COMPONENT_ANIMATION_SQUASH, id);
	animationStretch = ecs_component_add(ecs, ECS_COMPONENT_ANIMATION_STRETCH, id);

	snprintf
	(
		string,
		ENTITY_DAMAGE_NUMBER_TEXT_MAX,
		ENTITY_DAMAGE_NUMBER_FORMAT,
		damage
	);

	component_text_init
	(
		text,
		&ecs->game->resources.fonts[FONT_DAMAGE_NUMBER],
		string,
		strlen(string),
		-1,
		position,
		(f32*)ENTITY_DAMAGE_NUMBER_COLOR
	);

	component_action_delete_on_timer_init
	(
		actionDeleteOnTimer,
		ENTITY_DAMAGE_NUMBER_TIMER
	);

	component_animation_color_change_init
	(
		animationColorChange,
		(f32*)COLOR_TRANSPARENT,
		(f32*)ENTITY_DAMAGE_NUMBER_COLOR,
		ENTITY_DAMAGE_NUMBER_TIMER,
		ANIMATION_COLOR_CHANGE_MODE_STICK
	);

	component_animation_move_init
	(
		animationMove,
		(f32*)ENTITY_DAMAGE_NUMBER_MOVE_VELOCITY
	);

	component_animation_squash_init
	(
		animationSquash,
		base,
		ENTITY_DAMAGE_NUMBER_SQUASH_MULTIPLIER,
		ENTITY_DAMAGE_NUMBER_SQUASH_SPEED
	);

	component_animation_stretch_init
	(
		animationStretch,
		base,
		ENTITY_DAMAGE_NUMBER_STRETCH_MULTIPLIER,
		ENTITY_DAMAGE_NUMBER_STRETCH_SPEED
	);
}
