# 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 <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
#                         fadeout.py
#                     --------------------
#       date created : Thu Aug 9 2012
#          copyright : (C) 2012 Niels G. W. Serup
#      maintained by : Niels G. W. Serup <ns@metanohi.name>

"""
Fade out and in.
"""

import os
import pygame

class Fadeout(object):
    def __init__(self, game, function):
        self.__dict__.update(locals())
        self.game.objs.append(self)
        self.img = pygame.Surface(self.game.window.get_size())
        self._start_time = pygame.time.get_ticks()
        self._middle_time = self._start_time + 500
        self._end_time = self._middle_time + 500
        self.img.set_alpha(0)
        self.img.fill((0, 0, 0))
        self._has_run = False

    def update(self, e, t, dt):
        if t < self._middle_time:
            self.img.set_alpha(255 * (t - self._start_time) / 500)
        else:
            if not self._has_run:
                self.function()
                self._has_run = True
            if t < self._end_time:
                self.img.set_alpha(255 * (500 - (t - self._middle_time)) / 500)
            else:
                self.game.objs.remove(self)
                self.update = lambda *xs: None

    def draw(self, window):
        window.blit(self.img, (0, 0))

class Darkness(Fadeout):
    def __init__(self, game, darkness):
        self.__dict__.update(locals())
        self.set_darkness(darkness)

    def set_darkness(self, darkness):
        self.darkness = darkness
        self.img = pygame.Surface(self.game.window.get_size())
        self.img.set_alpha(int(darkness * 255))
        self.img.fill((0, 0, 0))

    def update(self, *xs):
        pass
