#include "GestionPlayer.h"

#include <ConfigFile.hpp>
#include <Input.hpp>
#include <ResourceLoader.hpp>
#include <Texture.hpp>

GestionPlayer::GestionPlayer()
{
	sprite_player_ptr = Sprite::_new();
	
	texture_player_ptr.instance();
	texture_player_ptr->_new();
}

void GestionPlayer::_register_methods()
{
	Godot::print("register Player...");
	register_method("_process", &GestionPlayer::_process);
	register_method("_init", &GestionPlayer::_init);
	register_method("_ready", &GestionPlayer::_ready);
	Godot::print("register Player OK!");

}

void GestionPlayer::_process(float delta)
{
	traitementInput();
	p.velocity = move_and_slide(p.velocity);

}

void GestionPlayer::_init()
{
	createPlayer();
}

void GestionPlayer::_ready()
{
	setupPlayer();
}

void GestionPlayer::createPlayer()
{
	add_child(sprite_player_ptr);
}

void GestionPlayer::setupPlayer()
{
	//Chargement de la texture
	//Godot::print("SetUp de Texture");
	texture_player_ptr = ResourceLoader::get_singleton()->load("res://Character/Hero Knight/HeroKnight/Layer 1_sprite_01.png");

	//setup du sprite
	//Godot::print("SetUp de Sprite");
	sprite_player_ptr->set_texture(texture_player_ptr);
}

void GestionPlayer::traitementInput()
{
	p.velocity = Vector2(0, 0);
	FLOOR = Vector2(0, -1);

	Input* i = Input::get_singleton();

	if (i->is_action_pressed("ui_left"))
		gauche();
	if (i->is_action_pressed("ui_right"))
		droit();
	if (i->is_action_pressed("ui_select"))
		saut();

	if (is_on_floor()) {
		on_ground = true;
		p.velocity.y += 0;
	}
	else {
		on_ground = false;
		p.velocity.y += gravity;
	}
}

void GestionPlayer::setPosition(int x, int y)
{
	Transform2D t;
	Vector2 v;
	Size2 s;

	//setup de la size
	s.x = 1;
	s.y = 1;
	v.x = x;
	v.y = y;
	t.set_origin(v);
	t.scale(s);

	set_transform(t);
}

void GestionPlayer::droit()
{
	Godot::print("Deplacement a droite");
	p.velocity.x += speed;
}

void GestionPlayer::gauche()
{
	Godot::print("Deplacement a gauche");
	p.velocity.x -= speed;
}

void GestionPlayer::bas()
{
}

void GestionPlayer::saut()
{
	p.velocity.y = power_jump;
}

void GestionPlayer::idle()
{
	p.velocity.x = 0.0;
}

void GestionPlayer::attack()
{
}
