from display import abs_to_screen
import pygame
class level:
    """
    Class handling all aspects of a level
    """
    def __init__(self, name, width,height,startpos, endrect, inanimates, map,animates):
        self.name = name
        self.inanimates = inanimates
        self.width = width
        self.height = height
        self.startpos = startpos
        self.endrect = endrect
        self.map = map
        self.animates = animates
    def draw(self,screen, view_rect):
        #draw the background
        self.map.draw(screen,view_rect)
        #draw all the inanimates in the level
        for inanimate in self.inanimates:
            inanimate.draw(screen,view_rect)
        for animate in self.animates:
            animate.draw(screen,view_rect)
def level_from_file(fname):
    """
    create a level from a file(using my hacked up easy format)
    currently the file supports the following: '0' for nothing, 'g' for a ground block
    's' for the start pos, 'e' for the end pos
    currently images are hard coded
    currently input validation isn't in, oh well
    """

    from map import Map
    from block import Block
    from teleportblock import Teleportblock
    from door import Door
    from Chest import Chest
    from item import Item
    from animate import Animate
    file = open(fname,'r')
    name = "level"
    #load the name(first line is the name)
    background = Map(file.readline().strip())
    #next line contains 'width,height' of the level
    width,height = file.readline().split(',')
    width = int(width)
    height = int(height)
    #place holders for everything else we need to load
    endrect = None
    startpos = None
    inanimates = []
    animates = []
    y = 0
    #parse stuffs!
    #each x,y in the file is a 32x32 block on the screen, so everything is converted
    for line in file.readlines():
        split = line.split(' ')
        x = 0
        for c in split:
            c = c.strip()
            if c == 's':
                startpos = (x*32,y*32)
            elif c == 'e':
                endrect = pygame.Rect((x*32,y*32),(32,32))
            elif c == 'b':
                inanimates.append(Block(pygame.Rect((x*32,y*32),(32,32))))
            elif c == 'd':
                inanimates.append(Door(pygame.Rect((x*32,y*32),(32,32))))
            elif c == 't':
                inanimates.append(Teleportblock(pygame.Rect((x*32,y*32),(32,32))))
            elif c == 'c':
                inanimates.append(Chest(pygame.Rect((x*32,y*32),(32,32))))
            elif c == 'w':
                inanimates.append(Item(pygame.Rect((x*32,y*32),(32,32)), "sword.png", "sword"))
            elif c == 'D':
                animates.append(Animate(pygame.Rect((x*32,y*32),(32,32)), "Dragon.png", "dragon"))
            x += 1
        y += 1
    file.close()
    return level(name,width,height,startpos,endrect,inanimates,background,animates)
