
#include <oslib/oslib.h>


//structure pour la gestion des parametre de la balle
typedef struct	ball_s2
{
  OSL_OBJECT	*obj;
  float		vx; // Vitesse en x
  float		vy; // Vitesse en y
  float gravite;
}ball_t2;


ball_t2 ball;



//Initialise le jeux
void InitGame3()
{

	ball.obj = oslCreateSprite(oslLoadImageFile("media/balle.png", OSL_IN_RAM, OSL_PF_5551),0, 7 ,30,30,12);
	oslAnimationPlay(ball.obj);
	//possition de la balle
	ball.obj->sprite->x = OSL_SCREEN_WIDTH/2;
	ball.obj->sprite->y = OSL_SCREEN_HEIGHT/2;
	ball.vx = 0.f;
	ball.vy = 0.f;
	//graviter du personnage
	ball.gravite = 0.15f;
}


void ToucheBalleRebond()
{
 oslReadKeys();


 if (osl_keys->held.up)
 {
	ball.vy = -15.f;
 }

 if (osl_keys->held.down)
 {
	ball.vy = 15.f;
 }

 
 if (osl_keys->held.left)
 {
	ball.vx = -15.f;
 }

 if (osl_keys->held.right)
 {
	ball.vx = 15.f;
 }



}



void BalleRebond()
{

	InitGame3();


	while (!osl_quit)
	{
		oslStartDrawing();
		oslCls();

		ToucheBalleRebond();

				
		// Maintenant la position fixe de la balle sera changée par rapport à la vitesse...
		ball.obj->sprite->x += ball.vx;
		ball.obj->sprite->y += ball.vy;

		//dessine l'animation tres legere de la balle
		oslDrawImageAnime(ball.obj);

		//verifie si la balle sort des bords, si oui on inverse la vitesse
		if (ball.vx < 0 && ball.obj->sprite->x <0){ ball.vx = -ball.vx; ball.obj->sprite->x = 0.f;}
		else if ((ball.obj->sprite->x + ball.obj->Animation.width)+ball.vx > OSL_SCREEN_WIDTH){ball.vx = -ball.vx;ball.obj->sprite->x = OSL_SCREEN_WIDTH - ball.obj->Animation.width;}

		//la meme chose pour les y
		if (ball.vy < 0 && ball.obj->sprite->y <0){ ball.vy = -ball.vy; ball.obj->sprite->y = 0.f;}
		else if ((ball.obj->sprite->y + ball.obj->Animation.height)+ball.vy > OSL_SCREEN_HEIGHT){ball.vy = -ball.vy;ball.obj->sprite->y  = OSL_SCREEN_HEIGHT - ball.obj->Animation.height;}


		//utilise un systemede graviter simple pour feiner l'objet
		if (ball.vx < 0.5f)ball.vx += ball.gravite;
		else if (ball.vx > 0.5f)ball.vx -= ball.gravite;
		else ball.vx = 0.f;

		if (ball.vy < 0.5f)ball.vy += ball.gravite;
		else if (ball.vy > 0.5f)ball.vy -= ball.gravite;
		else ball.vy = 0.f;


		oslSynchronization();
		if (osl_keys->pressed.start)break;

	}


}



