#include "entity_player.h"

/* Initializes a player entity. */
void
entity_player_init(ECS* ecs, u32 id, vec3 position, u32 spawnID)
{
	ComponentActionShoot* actionShoot;
	ComponentActionKeepInRectangle* actionKeepInRectangle;
	ComponentAmmo* ammo;
	ComponentCharacter* character;
	ComponentControlPlayer* controlPlayer;
	ComponentGameEntityType* gameEntityType;
	ComponentHealth* health;
	ComponentInvincibility* invincibility;
	ComponentRedirects* redirects;
	ComponentCircleCollision* circleCollision;
	ComponentStun* stun;
	ComponentGoodies* goodies;

	actionShoot = ecs_component_add(ecs, ECS_COMPONENT_ACTION_SHOOT, id);
	actionKeepInRectangle = ecs_component_add(ecs, ECS_COMPONENT_ACTION_KEEP_IN_RECTANGLE, id);
	ammo = ecs_component_add(ecs, ECS_COMPONENT_AMMO, id);
	character = ecs_component_add(ecs, ECS_COMPONENT_CHARACTER, id);
	controlPlayer = ecs_component_add(ecs, ECS_COMPONENT_CONTROL_PLAYER, id);
	gameEntityType = ecs_component_add(ecs, ECS_COMPONENT_GAME_ENTITY_TYPE, id); 
	invincibility = ecs_component_add(ecs, ECS_COMPONENT_INVINCIBILITY, id);
	redirects = ecs_component_add(ecs, ECS_COMPONENT_REDIRECTS, id);
	stun = ecs_component_add(ecs, ECS_COMPONENT_STUN, id);
	circleCollision = ecs_component_add(ecs, ECS_COMPONENT_CIRCLE_COLLISION, id);
	goodies = ecs_component_add(ecs, ECS_COMPONENT_GOODIES, id);

	ecs_component_add(ecs, ECS_COMPONENT_UPGRADES, id);
	ecs_component_add(ecs, ECS_COMPONENT_HEART, id);

	component_character_init
	(
		character,
		ecs,
		ecs->game->resources.textures[TEXTURE_PLAYER],
		(s32*)PLAYER_FRAME_SIZE,
		(s32*)PLAYER_ATLAS_SIZE,
		(f32*)PLAYER_SIZE,
		(f32*)PLAYER_PHYSICS_SIZE,
		position,
		PLAYER_FRICTION,
		PLAYER_VELOCITY_MAX,
		PLAYER_ALLEGIANCE,
		PLAYER_HEALTH_MAX,
		PLAYER_IS_BOUNCE,
		PLAYER_IS_AFFECTED_BY_GRAVITY,
		PLAYER_IS_SOLID
	);

	component_action_shoot_init
	(
		actionShoot,
		(ProjectileInit)entity_snake_init,
		PLAYER_ACTION_SHOOT_SPEED,
		PLAYER_ACTION_SHOOT_OFFSET,
		PLAYER_ACTION_SHOOT_HEIGHT
	);

	component_control_player_init
	(
		controlPlayer,
		ecs,
		PLAYER_CONTROL_MOVE_SPEED,
		PLAYER_CONTROL_REDIRECT_ANGLE_THRESHOLD,
		PLAYER_CONTROL_SHOOT_COOLDOWN,
		spawnID
	);

	component_ammo_init(ammo, PLAYER_AMMO_START);
	component_invincibility_init(invincibility, PLAYER_INVINCIBILITY_TIMER);
	component_stun_init(stun, PLAYER_STUN_TIMER);
	component_redirects_init(redirects, PLAYER_REDIRECTS_START);
	component_game_entity_type_init(gameEntityType, GAME_ENTITY_PLAYER);
	component_action_keep_in_rectangle_init(actionKeepInRectangle, PLAYER_ACTION_KEEP_IN_RECTANGLE);

	component_circle_collision_init(circleCollision, ecs, PLAYER_CIRCLE_RADIUS);

	entity_player_redirect_display_init(ecs, ecs_entity_add(ecs), id);
	entity_player_ammo_display_init(ecs, ecs_entity_add(ecs), id);
	entity_player_health_display_init(ecs, ecs_entity_add(ecs), id);

	goodies->value = 100000;
}
