# This file is part of A Robot's Conundrum.
#
# A Robot's Conundrum 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.
#
# A Robot's Conundrum 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
# A Robot's Conundrum.  If not, see <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
#                      weight_indicator.py
#                     --------------------
#       date created : Thu Nov 15 2012
#          copyright : (C) 2012 Niels G. W. Serup
#      maintained by : Niels G. W. Serup <ngws@metanohi.name>

"""
A weight indicator for drawing.
"""

import pygame

import worldobject

class WeightIndicator(worldobject.WorldObject):
    def __init__(self, level, x0, y0, state=0, 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.frame = self.state_to_frame(self.state)

    def state_to_frame(self, state):
        return state * 9 + 9

    def change_state(self, new_state):
        if self.in_rotation or new_state == self.state:
            return
        self.in_rotation = True
        self.start_frame = self.frame
        self.goal_state = new_state
        self.goal_frame = self.state_to_frame(new_state)
        self.start_time = pygame.time.get_ticks()
        
    def update(self, e, t, dt):
        if self.in_rotation:
            d = (t - self.start_time) / 1000.0
            self.frame = min(18, int(self.start_frame
                                     + (self.goal_frame - self.start_frame)
                                     * d))
            if self.frame == self.goal_frame:
                self.in_rotation = False
                self.state = self.goal_state
                for link in self.links:
                    link(self.state)

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

    def draw(self, window):
        self.img = self.level.imgs['weight_indicator'][self.frame]
        window.blit(self.img, (self.xd - 32 - self.level.camera_x,
                               self.yd - self.img.get_size()[1] + 24
                               - self.level.camera_y))
