#include "component_action_on_height_damage_disable.h"

/* DEPENDENCIES: GameObject, ActionDamage */

/* Sets on_height_damage_disable component info. */
void
component_action_on_height_damage_disable_init(ComponentActionOnHeightDamageDisable* self, f32 height)
{
	self->height = height;
}

/* Ticks on_height_damage_disable component. */
void
component_action_on_height_damage_disable_tick(ComponentActionOnHeightDamageDisable* self, ECS* ecs)
{
	ComponentGameObject* gameObject;
	ComponentActionDamage* actionDamage;
	ComponentHealth* health;

	gameObject = ecs_component_get(ecs, ECS_COMPONENT_GAME_OBJECT, self->component.id);
	actionDamage = ecs_component_get(ecs, ECS_COMPONENT_ACTION_DAMAGE, self->component.id);
	health = ecs_component_get(ecs, ECS_COMPONENT_HEALTH, self->component.id);

	if (gameObject->height > self->height)
	{
		actionDamage->component.isDisabled = true;
		health->component.isDisabled = true;
		self->isGrounded = false;
	}
	else
	{
		self->isGrounded = true;

		if (self->isGroundedPrevious != self->isGrounded)
		{
			sound_channel_stop(SOUND_SPECIAL);
			sound_play(&ecs->game->resources.sounds[SOUND_CRUMBLER_SMASH], SOUND_NO_PRIORITY);
		}

		actionDamage->component.isDisabled = false;
		health->component.isDisabled = false;
	}

	self->isGroundedPrevious = self->isGrounded;
}
