#include <stdio.h>
#include <stdlib.h>
#include <allegro.h>
#include "background.h"
#include "collision.h"
#include "jennyfer.h"
#include "objet.h"




void init_dep (Depart *depart )
{
    depart->verif=5;
    depart->gagnee=5;
}
void load_eau(water *Eau )
{
    int i;
    char buf[50];
    Eau->EAU[5] = load_bitmap("/Users/karim/Desktop/Nouveau dossier1/eau5.bmp", NULL);

}
void init_water(water *Eau)
{

    Eau->xm = 530;
    Eau->ym = 600;
}

void afficher_water(water *Eau,BITMAP *Buffer,BITMAP *Background,jennyfer *jenn,BITMAP *Backgroundcol)
{

    if (collision_sabela(*jenn,Backgroundcol))
        jenn->i=1 ;
    rest(1) ;

    int compteur =0 ;
    compteur++ ;
    if (Eau->ym >250&&jenn->i==1/*&&(compteur%8==1)*/)
    {
        Eau->ym= Eau->ym-1;
    }
    blit(Eau->EAU[5],Buffer,0,0,Eau->xm,Eau->ym,259,375) ;
    printf("%d \n" ,Eau->ym ) ;
}

void prendre_clef(jennyfer *jenn,BITMAP *Backgroundcol)
{
    int collision;
    int xc,yc ;
    xc=jenn->x;
    yc=jenn->y + (jenn->left[0]->h /2);
    collision=get_pixel(Backgroundcol,xc,yc) ;
    if(collision_clef_left(*jenn,Backgroundcol) || collision_clef_right(*jenn,Backgroundcol))
    {
        (jenn->clef)=1 ;

        ;
    }
}
void draw_clef(jennyfer *jenn,BITMAP *Buffer,BITMAP *Background)
{
    if (jenn->clef==0)
    {
        draw_sprite(Buffer,jenn->CLEF,380,30) ;
    }
    if ((jenn->clef)==1)
    {
        draw_sprite(Buffer,jenn->CLEF,jenn->xb,15);
    }

}
void prendre_couteau(jennyfer *jenn,BITMAP *Backgroundcol)
{
    if(collision_couteau_left(*jenn,Backgroundcol) || collision_couteau_right(*jenn,Backgroundcol))
    {
        (jenn->couteau)=1 ;
        printf(" coutrau %d \n ",(jenn->couteau));
    }
}
void draw_couteau(jennyfer *jenn,BITMAP *Buffer,BITMAP *Background)
{
    if (jenn->couteau==0)
    {

        draw_sprite(Buffer,jenn->COUTEAU,2080,330);
    }
    else if ((jenn->couteau)==1)
    {

        draw_sprite (Buffer,jenn->COUTEAU,(jenn->xb)+70,15);
    }

}
void prendre_objet  (jennyfer *jenn,BITMAP *Backgroundcol,BITMAP *Buffer,BITMAP *Background)
{
    prendre_clef(jenn,Backgroundcol);
    draw_clef(&jenn,Buffer,Background);;
    draw_couteau(&jenn,Buffer,Background);
    prendre_couteau(jenn,Backgroundcol);
    prendre_enreg(jenn,Backgroundcol);
    draw_enreg(&jenn,Buffer,Background);
    enregistrer_info(jenn,Backgroundcol);

}


void load_depart (Depart *depart)
{
    depart->go=load_bitmap("/Users/karim/Desktop/Nouveau dossier1/go.bmp", NULL);
    depart->image=create_bitmap(156, 220);
}

void depar (  Depart *depart ,BITMAP *Buffer,BITMAP *Background )
{
    int  i  ;

    for(i=0; i<624; i+=156)
    {
        blit (depart->go,depart->image,i,0,0,0,156,244) ;
        draw_sprite(Buffer,Background,0, 0);
        draw_sprite(Buffer, depart->image,350, 150);


        draw_sprite(screen, Buffer,0, 0);
        clear_bitmap(Buffer) ;

        rest(1000) ;

    }
}
void loadwinorlost (Depart *depart)
{
    depart->win = load_bitmap("/Users/karim/Desktop/Nouveau dossier1/youwin.bmp",NULL)  ;
    depart->lost = load_bitmap("/Users/karim/Desktop/Nouveau dossier1/gameover.bmp",NULL) ;

}
void win (Depart *depart,BITMAP *Buffer,jennyfer *jenn)
{

    if (jenn->x>=2300 && jenn->y<50 && jenn->clef==1)
    {

        depart->gagnee=0 ;

    }


}
void verif_lost(Cafar *cafar,BITMAP *Buffer,jennyfer *jenn,Depart *depart)
{
    int vie,verif ;

    if(bounding_box_collision(jenn->x,jenn->y,35, 126,cafar->x,cafar->y,75,45)==1)
    {
        jenn->vie -=5 ;

    }
    if (jenn->vie<10 )
    {
        depart->verif=1 ;
    }

}
//draw_sprite(Buffer,depart->win,200,250) ;


void load_cafar(Cafar *cafar)
{
    cafar->cafar_droite= load_bitmap("/Users/karim/Desktop/Nouveau dossier1/cafarD.bmp", NULL);
    cafar->cafar_gauche= load_bitmap("/Users/karim/Desktop/Nouveau dossier1/cafarG.bmp", NULL);
}
void move_cafar(Cafar *cafar)
{
    if(cafar->x<=1250)
        cafar->dir=1 ;
    if(cafar->x>=1350)
        cafar->dir=2  ;

    if (cafar->dir==1 )
    {
        cafar->x ++ ;

    }
    if (cafar->dir==2 )
    {
        cafar->x -- ;

    }

}
void sawer_cafar (Cafar *cafar,BITMAP *Buffer )
{
    if (cafar->dir==2 )
    {

        draw_sprite(Buffer, cafar->cafar_gauche,cafar->x, cafar->y);
    }
    if (cafar->dir==1 )
    {

        draw_sprite(Buffer, cafar->cafar_droite,cafar->x, cafar->y);
    }

}
void init_cafar (Cafar *cafar)
{
    cafar->y=100 ;
    cafar->dir=1 ;
    cafar->x=1311 ;
}
void init_pas (Paserelle *p)
{
    p->y=270 ;
    p->dir=1 ;
    p->x=1620 ;
}
void init_pas2 (Paserelle *p2)
{
    p2->y=189 ;
    p2->dir=1 ;
    p2->x=2200 ;
}
void load_pass(Paserelle *p)
{
    p->img= load_bitmap("/Users/karim/Desktop/Nouveau dossier1/platforme.bmp", NULL);

}
void move_pass(Paserelle *p)
{
    if(p->x<=1600)
        p->dir=1 ;
    if(p->x>=1850)
        p->dir=2 ;

    if (p->dir==1 )
    {
        p->x ++ ;

    }
    if (p->dir==2 )
    {
        p->x -- ;

    }

}
void move_pass2(Paserelle *p2)
{
    if(p2->x<=1900)
        p2->dir=1 ;
    if(p2->x>=2200)
        p2->dir=2 ;

    if (p2->dir==1 )
    {
        p2->x ++ ;

    }
    if (p2->dir==2 )
    {
        p2->x -- ;

    }

}
void sawer_pass (Paserelle *p,BITMAP *Buffer2,BITMAP *Buffer )
{
    if (p->dir==2 )
    {

        blit( p->img,Buffer2,0, 0,0,0,100,100);
        blit( Buffer2,Buffer,0, 0,p->x,p->y,100,20);


    }
    if (p->dir==1 )
    {

        blit( p->img,Buffer2,0, 0,0,0,100,100);
        blit( Buffer2,Buffer,0, 0,p->x,p->y,100,20);
        //draw_sprite(Buffer,Buffer2 ,p->x, p->y);
    }

}

void enregistrer_info(jennyfer jenn,BITMAP *Backgroundcol)
{
if (jenn.enreg==1)
{
    FILE* fichier ;
    fichier=fopen("partie.txt","a") ;

    if (fichier!=NULL)
    {

            fprintf(fichier,"position : \n x= %d y=%d  \n",jenn.x,jenn.y);
            fprintf(fichier," accesoir : \n couteau : %d \n clé : %d \n",jenn.couteau,jenn.clef) ;
            fprintf(fichier," vie :%d",jenn.vie) ;

    }
    fclose(fichier) ;
}
}
void prendre_enreg(jennyfer *jenn,BITMAP *Backgroundcol)
{
    if(collision_enreg_left(*jenn,Backgroundcol) || collision_enreg_right(*jenn,Backgroundcol))
    {
        (jenn->enreg)=1 ;
        printf(" enreg %d \n ",(jenn->enreg));
    }
}
void draw_enreg(jennyfer *jenn,BITMAP *Buffer,BITMAP *Background)
{

    if (jenn->enreg==0)
    {

        draw_sprite(Buffer,jenn->ENREG,1360,220);
    }
    else if ((jenn->enreg)==1)
    {

        draw_sprite (Buffer,jenn->ENREG,(jenn->xb),500);
    }

}
  /*void recharger_info (char chem[],jennyfer *jenn)
{
  if (cliquer charger partie )
  FILE* fichier ;

  fichier=fopen(chem,"r") ;
  if (fichier=NULL)
  {
      printf("erreur \n") ;
  }
  else
  {
      while(fscanf(fichier,"%d %d %d %d",jenn->x,jenn->y,jenn->clef,jenn->couteau,jenn->vie)!=EOF)
      {
          fscanf(fichier,"%d %d %d %d",jenn->x,jenn->y,jenn->clef,jenn->couteau,jenn->vie) ;
          printf("%d %d %d %d",jenn->x,jenn->y,jenn->clef,jenn->couteau,jenn->vie) ;
      }
  }
 }
*/


