import pyxel,random,math
from fonction import*
from sound import*
class Ennemi:
    def __init__(self,x,y):
        #attributs de base
        self.pv=100
        self.x=x*8
        self.y=y*8
        self.pos_x_spawn=self.x
        self.pos_y_spawn=self.y
        self.vitesse=0.9
        #gravité
        self.on_ground=False
        self.G=9.81
        self.vy=0
        self.dt=1/30
        self.impact=0
        self.vx=0
        self.force_saut=-5
        #sprite
        self.u=0
        self.v=16
        self.en_marche=False
        self.direction="gauche"
        #aleatoire
        self.des=0
        #distance
        self.distance=0
        self.sur_plateforme=False
        self.son=Sounds()
        #optimisation
        self.distance_affichage=150
        self.affiche=True
        
    def patrouille(self,j):
        new_x=self.x+self.vx
        x=new_x-j.camera_x
        y=self.y-j.camera_y
        x_j=j.x-j.camera_x
        y_j=j.y-j.camera_y
        self.distance=math.sqrt((x-x_j)**2+(y-y_j)**2)
        if pyxel.frame_count%50==0:
            self.des=random.randint(0,1)
        if self.distance>100:
            if self.des==0:
                new_x+=self.vitesse
            elif self.des==1:
                new_x-=self.vitesse 
            if collision_mur(new_x+3,self.y):
                if self.on_ground:
                    self.vy=self.force_saut
                    self.vx=3
            elif collision_mur(new_x-3,self.y):
                if self.on_ground:
                    self.vy=self.force_saut
                    self.vx=-3

        else:
            
            if x<x_j-4:
                if collision_mur(new_x+8,self.y):
                    self.en_marche=True
                    self.direction="droite"
                    if self.on_ground:
                        self.vy=self.force_saut
                        self.vx=3
                else:
                    new_x+=self.vitesse
                
            elif x>x_j+4:
                self.en_marche=True
                self.direction="gauche"
                if collision_mur(new_x-8,self.y):
                    if self.on_ground:
                        self.vy=self.force_saut
                        self.vx=-3
                else:
                    new_x-=self.vitesse
                
            else:
                if self.on_ground:
                    if y_j<=y:
                        self.vy=self.force_saut
                        self.vx=self.vitesse
                    else:
                        if self.sur_plateforme:
                            self.y+=10
        if not(collision_mur(new_x,self.y)):
            self.x=new_x
        else:
            if self.des==1:
                self.des=0
            else:
                self.des=1
        if self.des==1 or self.des==0:
            self.en_marche=True
        else:
            self.en_marche=False
        if self.des==1:
            self.direction="gauche"
        elif self.des==2:
            self.direction="droite"
        
        
                  
                
    def collision_sol(self):
        foot_x=int((self.x+3)//8)
        foot_y=int((self.y+8)//8)
        tete_x=int((self.x+3)//8)
        tete_y=int((self.y)//8)
        corps_droit_x=int((self.x+10)//8)
        corps_y=int((self.y)//8)
        corps_gauche_x=int((self.x-2)//8)

        tile_pied=pyxel.tilemap(1).pget(foot_x,foot_y)
        tile_tete=pyxel.tilemap(1).pget(tete_x,tete_y)
        tile_corps_droit=pyxel.tilemap(1).pget(corps_droit_x,corps_y)
        tile_corps_gauche=pyxel.tilemap(1).pget(corps_gauche_x,corps_y)
        if tile_pied in SOL_PLATEFORME or tile_pied in MUR or tile_pied in ECHELLE or tile_pied in SOL:
            self.y=(foot_y*8)-8
            self.on_ground=True
        else: 
            self.on_ground=False
        if tile_tete in MUR :
            self.vy=0.5
        if tile_pied in SOL_PLATEFORME:
            self.sur_plateforme=True
        else:
            self.sur_plateforme=False
        if tile_pied in TREMPLIN or tile_tete in TREMPLIN:
            #calcul de la propulsion de l'ennemi sur le tremplin en fonction de sa vitesse d'arrivée dessus
            self.vy=-1.3*abs(self.impact)
            self.impact=0
        if tile_pied in PIEGE_MONSTRE:
            self.pv-=5
            self.vy=-5
            self.vx=5
            
        if tile_corps_gauche in PIEGE_MONSTRE:
            self.pv-=5
            self.vy=self.force_saut
            self.vx=5
            
        if tile_corps_droit in PIEGE_MONSTRE:
            self.pv-=0.01
            self.vy=self.force_saut
            self.vx=-5
            
        
    def affichage_optimisation(self,j):
        new_x=self.x+self.vx
        x=new_x-j.camera_x
        y=self.y-j.camera_y
        x_j=j.x-j.camera_x
        y_j=j.y-j.camera_y
        self.distance=math.sqrt((x-x_j)**2+(y-y_j)**2) 
        if self.distance>self.distance_affichage:
            self.affiche=False
        else:
            self.affiche=True
    def repousse(self,autre):
        if collision(self.x,self.y,self.x+6,self.y+8,autre.x,autre.y,autre.x+6,autre.y+6):
            if autre.x>self.x: 
                if not(collision_mur(self.x+8,self.y)) and not(collision_mur(autre.x-8,autre.y)):
                    autre.x-=8
                    self.x+=8
            else:
                if not(collision_mur(self.x-8,self.y)) and not(collision_mur(autre.x+8,autre.y)):
                    autre.x+=8
                    self.x-=8
               
    def update(self):
        

        if not self.on_ground:
            self.vy += self.G * self.dt
            self.vx*=0.85
            if self.vy>=8:
                self.vy=8
            if self.y>1000:
                self.pv-=100  
        self.y += self.vy
        self.impact=self.vy
        self.collision_sol()
        if self.on_ground:
            self.vy = 0
            self.vx=0
            if self.impact >= 8:
                self.pv -= self.impact * 2
            elif  6<=self.impact <=7:
                self.pv -= self.impact * 1
            self.impact = 0
        self.gestion_sprite()
    def collision_avec_joueur(self,j):
        cx=j.camera_x
        cy=j.camera_y
        
        if collision(j.x-cx,j.y+1-cy,j.x+8-cx,j.y+8-cy,self.x-cx,self.y+1-cy,self.x+7-cx,self.y+8-cy):
            if j.touchable:
                j.pv-=6
                j.frame_invulnerabilité=30
            
    def gestion_sprite(self):
        if self.on_ground:
            if self.en_marche:
                if self.direction=="droite":
                    if pyxel.frame_count%10<5:
                        self.u=8
                        self.v=16
                    else:
                        self.u=0
                        self.v=16
                else:
                    if pyxel.frame_count%10<5:
                        self.u=7
                        self.v=24
                    else:
                        self.u=0
                        self.v=16
            else:
                self.u=0
                self.v=16
        else:
            self.u=16
            self.v=16
           
    def draw(self,j):
        pyxel.rect(self.x-j.camera_x-5,self.y-j.camera_y-3,20,1,10)
        pyxel.rect(self.x-j.camera_x-5,self.y-j.camera_y-3,self.pv/5,1,8)
        pyxel.blt(self.x-j.camera_x,self.y-j.camera_y,0,self.u,self.v,8,8,5)

        
                
                
        