#include "ecs.h"

static void _ecs_function(ECS* self, ECSFunctionType type);

/* Executes a function upon an ECS. */
/* If a component does not have the function specfied, skips. */
static void
_ecs_function(ECS* self, ECSFunctionType type)
{
	for (s32 i = 0; i < ECS_COMPONENT_COUNT; i++)
	{
		ECSComponentList* list;

		list = &self->lists[i];

		ecs_component_list_function(list, type);
	}
}

/* Ticks the ECS. */
void
ecs_tick(ECS* self)
{
	_ecs_function(self, ECS_FUNCTION_TICK);
}

/* Draws the ECS. */
void
ecs_draw(ECS* self)
{
	_ecs_function(self, ECS_FUNCTION_DRAW);
}

/* Initializes ECS. */
void
ecs_init(ECS* self, Game* game)
{
	self->game = game;

	for (s32 i = 0 ; i < ECS_COMPONENT_COUNT; i++)
		ecs_component_list_init(&self->lists[i], self, ECS_COMPONENT_INFO[i]);
}

/* Clears ECS (removes all components, but does not free the greater ECS struct). */
void
ecs_clear(ECS* self)
{
	for (s32 i = 0 ; i < ECS_COMPONENT_COUNT; i++)
		ecs_component_list_clear(&self->lists[i]);
}

/* Frees ECS. */
void
ecs_free(ECS* self)
{
	for (s32 i = 0 ; i < ECS_COMPONENT_COUNT; i++)
		ecs_component_list_free(&self->lists[i]);

	memset(self, '\0', sizeof(ECS));
}
