# 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/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
#                          mirror.py
#                     --------------------
#       date created : Fri Aug 10 2012
#          copyright : (C) 2012 Niels G. W. Serup
#      maintained by : Niels G. W. Serup <ns@metanohi.name>

"""
A generic mirror for drawing.
"""

import pygame

import worldobject

class Mirror(worldobject.WorldObject):
    def __init__(self, level, x0, y0, left_up=True, links=[]):
        self.__dict__.update(locals())
        self.xd, self.yd = self.x0 * 64, (self.y0 + 4) * 48
        worldobject.WorldObject.__init__(self, level, self.xd, self.yd)

        self.in_rotation = False
        self.left_up_aim = self.left_up

        self.frame = 7
        self.anim_speed = 12
        self._half_anim_len = len(self.level.imgs['mirror']) / 2

    def rotate(self):
        if self.in_rotation:
            return
        self.in_rotation = True
        self.left_up_aim = not self.left_up_aim
        self.frame = 0

    def update(self, e, t, dt):
        if self.in_rotation:
            top = self._half_anim_len - 1
            self.frame = min(self.frame + self.anim_speed * dt, top)
            if self.frame == top:
                self.in_rotation = False
                self.left_up = not self.left_up
                for link in self.links:
                    link(self.left_up)

        worldobject.WorldObject.update(self, e, t, dt)

    def draw(self, window):
        fn = self.frame + 1 if not self.left_up_aim else self.frame + self._half_anim_len
        self.img = self.level.imgs['mirror'][int(fn)]
        window.blit(self.img, (self.xd - 32 - self.level.camera_x,
                               self.yd - self.img.get_size()[1] + 24
                               - self.level.camera_y))
