import display, spriteloader, pygame, random, sys
class Knight:
    def __init__(self, location, inventory):
        self.location = location
        self.location.w = 32
        self.location.h = 32
        self.speed = [0,0]
        sprite = pygame.image.load("Knight2.png")
        sprite = sprite.convert_alpha()
        self.sprites = spriteloader.get_sprites(sprite,32,32)
        self.frame = 0
        self.num_frames = 3
        self.frame_delay = 0
        self.frame_delay_max = 3
        self.inventory = inventory
        self.health = 100
        self.attacking = False
    def update(self,level, elapsed):
        if self.health <= 0:
            sys.exit()
        self.location.move_ip(self.speed)
        for inanimate in level.inanimates:
            if inanimate.colliderect(self.location):
                if inanimate.__doc__=="tele":
                    self.location.topleft = (random.randrange(0,4096,32),random.randrange(0,4096,32))
                elif inanimate.__doc__=="Item":
                    self.inventory.add_item( inanimate )
                elif abs(self.location.bottom - inanimate.location.top) <= 16:
                    inanimate.touched_from_top(self)
                    if inanimate.is_solid():
                        self.location.bottom = inanimate.location.top
                        self.speed[1] = 0
                #touchedting the bottom of the inanimate going up
                elif abs(self.location.top - inanimate.location.bottom) <= 16:
                    inanimate.touched_from_bottom(self)
                    if inanimate.is_solid():
                        self.location.top = inanimate.location.bottom
                        self.speed[1] = 0
                #touchedting the sides
                elif abs(self.location.right - inanimate.location.left) <= 16:
                    inanimate.touched_from_left(self)
                    if inanimate.is_solid():
                        self.location.right = inanimate.location.left
                        self.speed[0] = 0
                else:
                    inanimate.touched_from_right(self)
                    if inanimate.is_solid():
                        self.location.left = inanimate.location.right
                        self.speed[0] = 0
        
        #TODO: Collision detection with stuff in level
    def draw(self, screen, screen_rect):
        #TODO: choose which sprite to draw depending on our speed/animation stuffs
        if self.speed[0] != 0 or self.speed[1] != 0:
            self.frame_delay += 1
            if self.frame_delay >= self.frame_delay_max:
                self.frame_delay = 0
                self.frame += 1
                if self.frame >= self.num_frames:
                    self.frame = 0
        offset = 0
        if self.speed[0] > 0:
            offset = 2
        if self.speed[0] < 0:
            offset = 1
        if self.speed[1] < 0:
            offset = 3
        if self.speed[1] > 0:
            offset = 0
        location = self.sprites[self.frame+offset * self.num_frames].get_bounding_rect()
        location.center = self.location.center
        self.location = location
        screen.blit(self.sprites[self.frame + offset * self.num_frames], display.abs_to_screen(self.location, screen_rect))
        pygame.draw.rect( screen, (200,200,200), pygame.Rect( (10,10),(self.health,10)) )
