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

"""
A laser for drawing.
"""

import pygame

import worldobject
import robotgame.logic.lasermirror as lm

class Laser(worldobject.WorldObject):
    def __init__(self, level, line, first_laser=False):
        self.__dict__.update(locals())
        (self.x0, self.y0), (self.x1, self.y1) = line
        self.x0d, self.y0d, self.x1d, self.y1d \
            = self.x0 * 64, (self.y0 + 4) * 48, self.x1 * 64, (self.y1 + 4) * 48
        if self.y0d > self.y1d:
            self.y0d, self.y1d = self.y1d, self.y0d
        if self.x0d > self.x1d:
            self.x0d, self.x1d = self.x1d, self.x0d
        worldobject.WorldObject.__init__(self, level, self.x0d,
                                         max(self.y0d, self.y1d))

        x0de = 31
        y0de = -48
        x1de = 31
        y1de = -48

        if self.x0 < 0:
            x0de += 32
        if self.y0 < 0:
            y0de += 24
        if self.x1d >= self.level.size[0]:
            x1de -= 32
        if self.first_laser and self.y0 == 0 and self.y0 != self.y1:
            y0de += 6
        if self.level.playfield.get((self.x0, self.y0)) is lm.Target and self.x0 < self.x1:
            x0de += 20
        if self.level.playfield.get((self.x1, self.y1)) is lm.Target and self.x0 < self.x1:
            x1de -= 20
        self.x0d += x0de
        self.y0d += y0de
        self.x1d += x1de
        self.y1d += y1de

        self.start_dark = 0

    def update(self, e, t, dt):
        self.start_dark = (t % 200) / 100
        worldobject.WorldObject.update(self, e, t, dt)

    def draw(self, window):
        colors = [(155, 0, 0), (255, 0, 0)]
        c = self.start_dark
        if self.x0d != self.x1d:
            length = self.x1d - self.x0d
            for i in range(0, length, 8):
                x0d = self.x0d + i
                pygame.draw.line(window, colors[c],
                                 (x0d - self.level.camera_x,
                                  self.y0d - self.level.camera_y),
                                 (x0d + min(8, length - i) - self.level.camera_x,
                                  self.y1d - self.level.camera_y), 2)
                c ^= 1
        else:
            length = self.y1d - self.y0d
            for i in range(0, length, 8):
                y0d = self.y0d + i
                pygame.draw.line(window, colors[c],
                                 (self.x0d - self.level.camera_x,
                                  y0d - self.level.camera_y),
                                 (self.x0d - self.level.camera_x,
                                  y0d + min(8, length - i) - self.level.camera_y), 2)
                c ^= 1
            
