from gamedata.objects.base import BaseObject

class BackgroundDrawer(BaseObject):

    def __init__(self,x,y,game,w,h,sprite,ratio):
        super().__init__(x,y,game,w,h)
        self.ratio = ratio
        self.depth = -1
        self.fill(sprite) # Générer notre sprite de l'objet, le remplissant avec le sprite argument

    def fill(self,sprite):
        game = self.game
        self.tilew = sprite.get_width()
        self.tileh = sprite.get_height()
        nbw = round(self.rect[2]/self.tilew+1.5)
        nbh = round(self.rect[3]/self.tileh+1.5)
        sw = nbw*self.tileh
        sh = nbh*self.tileh
        self.sprite = game.pygame.Surface((sw,sh),game.pygame.SRCALPHA)

        for i in range(nbw):
            for j in range(nbh):
                self.sprite.blit(sprite,(i*self.tilew,j*self.tileh))

    def draw(self):
        game = self.game
        # Affichage du sprite de mannière répétée
        px = (self.rect[0]-game.globals["camerax"]*self.ratio)%self.tilew
        py = (self.rect[1]-game.globals["cameray"]*self.ratio)%self.tileh

        game.window.blit(self.sprite,(self.baserect[0]+px-self.tilew,self.baserect[1]+py-self.tileh))