#include "component_game_object.h"

/* DEPENDENCIES: Sprite, Physics */

/* Sets game_object component info. */
/* This assumes a one-frame sprite. */
void
component_game_object_init
(
	ComponentGameObject* self,
	ECS* ecs,
	Texture texture,
	vec2 size,
	vec2 physicsSize,
	vec3 position,
	vec3 offset,
	vec4 color,
	f32 friction,
	f32 velocityMax
)
{
	ivec2 atlasSize;
	ivec2 frameSize;

	frameSize[0] = (s32)texture.size[0];
	frameSize[1] = (s32)texture.size[1];

	glm_ivec2_one(atlasSize);

	component_game_object_atlas_init
	(
		self,
		ecs,
		texture,
		frameSize,
		atlasSize,
		size,
		physicsSize,
		position,
		offset,
		color,
		friction,
		velocityMax
	);
}

/* Sets game_object component info, with an atlas. */
void
component_game_object_atlas_init
(
	ComponentGameObject* self,
	ECS* ecs,
	Texture texture,
	ivec2 frameSize,
	ivec2 atlasSize,
	vec2 size,
	vec2 physicsSize,
	vec3 position,
	vec3 offset,
	vec4 color,
	f32 friction,
	f32 velocityMax
)
{
	ComponentSprite* sprite;
	ComponentPhysics* physics;
	
	sprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->component.id);
	physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);

	component_sprite_atlas_init
	(
		sprite, 
		texture, 
		frameSize, 
		atlasSize, 
		size, 
		position, 
		offset, 
		color
	);

	component_physics_init(physics, physicsSize, position, velocityMax, friction);
}

/* Runs on game object add. */
void
component_game_object_add(ComponentGameObject* self, ECS* ecs)
{
	ecs_component_add(ecs, ECS_COMPONENT_PHYSICS, self->component.id);
	ecs_component_add(ecs, ECS_COMPONENT_SPRITE, self->component.id);
}

/* Runs on game object add. */
void
component_game_object_delete(ComponentGameObject* self, ECS* ecs)
{
	ecs_component_delete(ecs, ECS_COMPONENT_SPRITE, self->component.id);
	ecs_component_delete(ecs, ECS_COMPONENT_PHYSICS, self->component.id);
}

/* Ticks game_object component. */
void
component_game_object_tick(ComponentGameObject* self, ECS* ecs)
{
	ComponentSprite* sprite;
	ComponentPhysics* physics;

	sprite = ecs_component_get(ecs, ECS_COMPONENT_SPRITE, self->component.id);
	physics = ecs_component_get(ecs, ECS_COMPONENT_PHYSICS, self->component.id);

	glm_vec3_copy(physics->position, sprite->position);
}
