#include "component_heart.h"

/* Determines current heart state. */
void
_state_set(ComponentHeart* self, ECS* ecs)
{
	HeartState previousState;

	previousState = self->state;

	if (self->concurrentUses >= COMPONENT_HEART_CONCURRENT_USES_HEAVY)
		self->state = HEART_STATE_HEAVY;
	else if (self->concurrentUses >= COMPONENT_HEART_CONCURRENT_USES_MEDIUM)
		self->state = HEART_STATE_MEDIUM;
	else
		self->state = HEART_STATE_LIGHT;

	if (self->state != previousState)
	{
		if (self->state == HEART_STATE_HEAVY)
			music_play(&ecs->game->resources.music[MUSIC_COMBAT_THREE], true);
		else if (self->state == HEART_STATE_MEDIUM)
			music_play(&ecs->game->resources.music[MUSIC_COMBAT_TWO], true);
	}
}

/* Ticks heart component. */
void
component_heart_tick(ComponentHeart* self, ECS* ecs)
{
	ComponentGoodies* goodies;

	goodies = ecs_component_get(ecs, ECS_COMPONENT_GOODIES, self->component.id);

	if (self->concurrentUses > 1)
		self->multiplier = self->uses + self->concurrentUses;
	else
		self->multiplier = self->uses + 1;

	if (goodies)
		goodies->multiplier = self->multiplier;

	_state_set(self, ecs);
}
