# This file is part of ROBOTGAME
#
# ROBOTGAME is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# ROBOTGAME is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# ROBOTGAME.  If not, see .
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
#                           level.py
#                     --------------------
#       date created : Tue Aug 7 2012
#          copyright : (C) 2012 Sakse Dalum
#      maintained by : Sakse Dalum 
"""
A generic level.
"""
import pygame
import itertools
import player
import fadeout
class Level(object):
    def __init__(self, game, graphics_dir, size=(0, 0), paused=False):
        self.__dict__.update(locals())
        self.tiles = []
        self.objects = []
        self.bottom_objects = [] # Special tiles
        self.imgs = game.loader.imgs
        self.reverse_imgs = (
            dict([(val, key) for (key, val)
                  in self.imgs.iteritems() if not isinstance(val, list)]))
        self.camera_x = 0
        self.camera_y = 0
        self.player = player.Player(self, 0, 0)
        self.objects.append(self.player)
        self.darkness = fadeout.Darkness(self.game, 0)
        self.load()
    def load(self):
        pass
    def set_darkness(self, darkness):
        self.darkness.set_darkness(darkness)
    def draw_background(self):
        self.background = pygame.Surface(self.size)
        for tile in self.tiles:
            tile.draw(self.background)
    def restart(self):
        pass
    def update(self, e, t, dt):
        if not self.paused:
            for obj in self.objects:
                obj.update(e, t, dt)
                if self.player.is_moving:
                    obj.update_alpha()
        screen_size = self.game.window.get_size()
        self.camera_x = max(0, min(self.size[0] - screen_size[0],
                                   (self.player.x - screen_size[0] / 2 + 32)))
        self.camera_y = max(0, min(self.size[1] - screen_size[1] - 48,
                                   (self.player.y - screen_size[1] / 2 - 24)))
    def _positions(self):
        return ((x, y) for (y, x) in itertools.product(xrange(self.dimensions[1]),
                                                       xrange(self.dimensions[0])))
        
    def _blit_background(self, window):
        window.blit(self.background, (0 - self.camera_x, 0 - self.camera_y))
    def _sorted_objs(self, objs=None):
        return sorted(self.objects if objs is None else objs,
                      key=lambda obj: (obj.y + obj.z))
    def draw(self, window):
        self._blit_background(window)
        for obj in self._sorted_objs(self.bottom_objects):
            try:
                obj.draw(window)
            except IndexError:
                print("Skipping frames ...")
        for obj in self._sorted_objs():
            try:
                obj.draw(window)
            except IndexError:
                print("Skipping frames ...")
        self.darkness.draw(window)