class EffectSprite(pygame.sprite.Sprite):
    def __init__(self, pathToSprite, spriteWidths, top, height, group=None):
        pygame.sprite.Sprite.__init__(self, group)
        #ss = spritesheet.spritesheet('sprites/Swords_blade.png')
        ss = spritesheet.spritesheet(pathToSprite)


        w,h = ss.get_dimensions()
        effectSurf = ss.image_at((0,0,59,120),MEGAMAN_SPRITE_COLOR)

        #self.swordEffectStrip = SpriteStripAnim('sprites/Swords_blade.png',(0,0,500/6,120),6,MEGAMAN_SPRITE_COLOR,True,2)
        self.effectStrip = SpriteStripAnim(pathToSprite,(0,0,500/len(spriteWidths),120), len(spriteWidths),MEGAMAN_SPRITE_COLOR,True,2)
        # w1 = 58
        # w2 = 78
        # w3 = 134
        # w4 = 103
        # w5 = 81
        # w6 = 49
        # rects = [(0,0,w1,90),(w1,0,w2,90), (w1+w2,0,w3,90),
        #          (w1+w2+w3,0,w4,90),(w1+w2+w3+w4,0,w5,90),
        #          (w1+w2+w3+w4+w5,0,w6,90)]

        rects = []
        widthSum = 0
        for w in spriteWidths:
            newRect = (widthSum, top, w, height)
            rects.append(newRect)
            widthSum += w

        self.effectStrip.images = ss.images_at(rects, MEGAMAN_SPRITE_COLOR)
        #--------------------------------- CKqqwong's spritesheet
                
        # charactorSurf = pygame.Surface( (64,64) )
        # charactorSurf = charactorSurf.convert_alpha()
        # charactorSurf.fill((0,0,0,0)) #make transparent
        # pygame.draw.circle( charactorSurf, (255,0,0), (32,32), 32 )

        self.defImage = effectSurf #keep track of default image to reset after movement
        self.image = effectSurf
        self.rect  = effectSurf.get_rect()


        self.actionFramesLeft = 0
        self.extras = None #for overlaying sprites on top

    def update(self):
        #attack updates
        if self.extras and (self.actionFramesLeft == 0):
            self.extras = None
            #self.image = self.defImage
            self.kill()
        elif self.extras:
            self.image = self.effectStrip.next()
            self.actionFramesLeft -= 1
    def __init__(self, charactor, marker, group=None):
        pygame.sprite.Sprite.__init__(self, group)

        self.marker = marker
        self.charactor = charactor
        self.idNum = self.charactor.idNum
        
        #--------------------------------- BLai's spritesheet
        ss = spritesheet.spritesheet('sprites/move.png')
        w,h = ss.get_dimensions()

        """charactorSurf = ss.image_at((0,0,w/4,h), MEGAMAN_SPRITE_COLOR)
        self.moveStrip = SpriteStripAnim('sprites/move.png', (0,0,w/4,h), 4, MEGAMAN_SPRITE_COLOR, True, 2)
        """
        
        atkSS = spritesheet.spritesheet('sprites/basic_attack.png')
        atkW, atkH = atkSS.get_dimensions()
        self.basicAttackStrip = SpriteStripAnim('sprites/basic_attack.png', (0,0,atkW/5,atkH), 5, MEGAMAN_SPRITE_COLOR, True, 2)


        #--------------------------------- CKwong's spritesheet
        
        ### sprite sheets have been rescaled to twice bigger
        if self.marker == 2:
            baseSS = spritesheet.spritesheet('sprites/fronts_backs/basic_shoot_front.png')
            baseW, baseH = baseSS.get_dimensions()
            charactorSurf = baseSS.image_at((0,0,69,120),MEGAMAN_SPRITE_COLOR)

        else:
            baseSS = spritesheet.spritesheet('sprites/base_layer.png')
            baseW, baseH = baseSS.get_dimensions()
            charactorSurf = baseSS.image_at((0,0,108,108),MEGAMAN_SPRITE_COLOR)
        self.moveStrip = SpriteStripAnim('sprites/base_layer.png',(0,485,372/4,120),4, MEGAMAN_SPRITE_COLOR, True,2)
        
        #self.swordAttackStrip = SpriteStripAnim('sprites/base_layer.png',(0,610,389/4,120),4,MEGAMAN_SPRITE_COLOR,True,2)
        

        # charactorSurf = pygame.Surface( (64,64) )
        # charactorSurf = charactorSurf.convert_alpha()
        # charactorSurf.fill((0,0,0,0)) #make transparent
        # pygame.draw.circle( charactorSurf, (255,0,0), (32,32), 32 )
        self.defImage = charactorSurf #keep track of default image to reset after movement
        self.image = charactorSurf
        self.rect  = self.image.get_rect()


        self.actionFramesLeft = 0
        self.moveTo = None
        self.attack = None

        #---------------------------charging animations

        chargeSS = spritesheet.spritesheet("sprites/AreaGrab.png")

        chargeW, chargeH = chargeSS.get_dimensions()

        chargeRects = []
        chargeWidthSum = 0
        for w in range(4):
            newRect = (chargeWidthSum, 0, 38, 40)
            chargeRects.append(newRect)
            chargeWidthSum += w

        self.chargeImages = chargeSS.images_at(chargeRects, MEGAMAN_SPRITE_COLOR)
        self.chargeIndex = 0
 def updateAttackStrip(self, path, numFrames, width):
     tempSS = spritesheet.spritesheet(path)
     tempW, tempH = tempSS.get_dimensions()
     self.attackStrip = SpriteStripAnim(path,(0, 0,width,tempH),numFrames,MEGAMAN_SPRITE_COLOR,True,2)
class CharactorSprite(pygame.sprite.Sprite):
    def __init__(self, charactor, marker, group=None):
        pygame.sprite.Sprite.__init__(self, group)

        self.marker = marker
        self.charactor = charactor
        self.idNum = self.charactor.idNum
        
        #--------------------------------- BLai's spritesheet
        ss = spritesheet.spritesheet('sprites/move.png')
        w,h = ss.get_dimensions()

        """charactorSurf = ss.image_at((0,0,w/4,h), MEGAMAN_SPRITE_COLOR)
        self.moveStrip = SpriteStripAnim('sprites/move.png', (0,0,w/4,h), 4, MEGAMAN_SPRITE_COLOR, True, 2)
        """
        
        atkSS = spritesheet.spritesheet('sprites/basic_attack.png')
        atkW, atkH = atkSS.get_dimensions()
        self.basicAttackStrip = SpriteStripAnim('sprites/basic_attack.png', (0,0,atkW/5,atkH), 5, MEGAMAN_SPRITE_COLOR, True, 2)


        #--------------------------------- CKwong's spritesheet
        
        ### sprite sheets have been rescaled to twice bigger
        if self.marker == 2:
            baseSS = spritesheet.spritesheet('sprites/fronts_backs/basic_shoot_front.png')
            baseW, baseH = baseSS.get_dimensions()
            charactorSurf = baseSS.image_at((0,0,69,120),MEGAMAN_SPRITE_COLOR)

        else:
            baseSS = spritesheet.spritesheet('sprites/base_layer.png')
            baseW, baseH = baseSS.get_dimensions()
            charactorSurf = baseSS.image_at((0,0,108,108),MEGAMAN_SPRITE_COLOR)
        self.moveStrip = SpriteStripAnim('sprites/base_layer.png',(0,485,372/4,120),4, MEGAMAN_SPRITE_COLOR, True,2)
        
        #self.swordAttackStrip = SpriteStripAnim('sprites/base_layer.png',(0,610,389/4,120),4,MEGAMAN_SPRITE_COLOR,True,2)
        

        # charactorSurf = pygame.Surface( (64,64) )
        # charactorSurf = charactorSurf.convert_alpha()
        # charactorSurf.fill((0,0,0,0)) #make transparent
        # pygame.draw.circle( charactorSurf, (255,0,0), (32,32), 32 )
        self.defImage = charactorSurf #keep track of default image to reset after movement
        self.image = charactorSurf
        self.rect  = self.image.get_rect()


        self.actionFramesLeft = 0
        self.moveTo = None
        self.attack = None

        #---------------------------charging animations

        chargeSS = spritesheet.spritesheet("sprites/AreaGrab.png")

        chargeW, chargeH = chargeSS.get_dimensions()

        chargeRects = []
        chargeWidthSum = 0
        for w in range(4):
            newRect = (chargeWidthSum, 0, 38, 40)
            chargeRects.append(newRect)
            chargeWidthSum += w

        self.chargeImages = chargeSS.images_at(chargeRects, MEGAMAN_SPRITE_COLOR)
        self.chargeIndex = 0


    def updateAttackStrip(self, path, numFrames, width):
        tempSS = spritesheet.spritesheet(path)
        tempW, tempH = tempSS.get_dimensions()
        self.attackStrip = SpriteStripAnim(path,(0, 0,width,tempH),numFrames,MEGAMAN_SPRITE_COLOR,True,2)

    def resize(self):
        if self.marker <= 1:
            charRow = self.charactor.sector.idNum / NUM_COLS
            baseW, baseH = self.image.get_rect().width*3/4, self.image.get_rect().height*3/4
            self.image = trans.scale(self.image,(baseW + (baseW/3) * charRow, baseH + (baseH/3) * charRow))
        elif self.marker == 2: #red closer, first player closer
            charCol = self.charactor.sector.idNum % NUM_COLS
            baseW, baseH = self.image.get_rect().width*3/4, self.image.get_rect().height*3/4
            self.image = trans.scale(self.image,(baseW + (baseW/3) * (NUM_COLS - charCol), baseH + (baseH/3) * (NUM_COLS - charCol)))
        elif self.marker == 3: #blue closer, 
            charCol = self.charactor.sector.idNum % NUM_COLS
            baseW, baseH = self.image.get_rect().width*3/4, self.image.get_rect().height*3/4
            self.image = trans.scale(self.image,(baseW + (baseW/3) * charCol, baseH + (baseH/3) * charCol))



    #----------------------------------------------------------------------
    def update(self):
        #movement updates
        changed = False
        if self.moveTo and (self.actionFramesLeft == 0):
            self.image = self.defImage.copy()
            self.resize()
            self.rect = self.image.get_rect()
            if self.marker == 1:
                self.rect.midtop = self.moveTo
            else:
                self.rect.midbottom = self.moveTo
            self.moveTo = None
            changed = True

        elif self.moveTo:
            self.image = self.moveStrip.next().copy()
            self.resize()
            self.actionFramesLeft -= 1
            changed = True


        #attack updates
        elif self.attack and (self.actionFramesLeft == 0):
            self.image = self.defImage.copy()
            self.resize()
            self.attack = None
            changed = True
        
        elif self.attack:
            #self.image = self.basicAttackStrip.next()
            self.image = self.attackStrip.next().copy()
            self.resize()
            self.actionFramesLeft -= 1
            changed = True

        if self.charactor.charging:
            chargingImage = self.chargeImages[self.chargeIndex]
            self.image.blit(chargingImage, self.image.get_rect().center)
            self.chargeIndex = (self.chargeIndex + 1) % len(self.chargeImages)


        #mirror character on right side
        if self.idNum == 2 and changed:
            self.image = trans.flip(self.image, True, False)


        if self.marker == 1 and changed:
            self.image = trans.flip(self.image, False, True)