import pygame
import game
# definir la classe qui va gerer le projectile du joueur
class Projectile(pygame.sprite.Sprite):

    # definir le constucteur de cette classe
    def __init__(self, player):
        super().__init__()
        self.velocity = 1
        self.player = player
        self.image = pygame.image.load('assets/image_naruto/naruto_shuriken1.png')
        self.image = pygame.transform.scale(self.image, (30,  30))
        self.rect = self.image.get_rect()
        self.rect.y = player.rect.y + 15
        self.origin_image = self.image
        self.angle = 0
        self.a = 0

    def sens(self, player, player2):
        if player.rect.x < player2.rect.x:
            self.rect.x = player.rect.x + 70
        elif player.rect.x > player2.rect.x:
            self.rect.x = player.rect.x - 30

    def rotate(self, player, player2):
        # tourner le projectile en deplacement
        if player.rect.x < player2.rect.x:
            self.angle += -14
        if player.rect.x > player2.rect.x:
            self.angle += 14
        self.image = pygame.transform.rotozoom(self.origin_image, self.angle, 1)
        self.rect = self.image.get_rect(center=self.rect.center)

    def remove(self,):
        self.player.all_projectiles.remove(self)

    def move(self, player, player2):
        print(self.rect.x)
        if self.a == 0:
            self.sens(player, player2)
        self.a += 1
        if player.rect.x < player2.rect.x:
            self.rect.x += self.velocity
        if player.rect.x > player2.rect.x:
            self.rect.x -= self.velocity
        self.rotate(player, player2)

        # verifier si notre projectile n'est plus present sur l'ecran
        if self.rect.x > 1011 or self.rect.x < 0 or self.rect.x == player2.rect.x + player2.rect.width \
                or self.rect.x == player2.rect.x + player2.rect.width+1\
                or self.rect.x == player2.rect.x + player2.rect.width+2\
                or self.rect.x == player2.rect.x + player2.rect.width+3:
            # supprimer le projectile en dehors de l'ecran
            self.remove()
            print("projectile supprimer")
