Source

Fat x Fast / FatxFast / gui / hud / minimapframe.py

Full commit
# This file is part of FatxFast.                                                
#                                                                               
#    FatxFast 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.                                        
#                                                                               
#    FatxFast 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 FatxFast.  If not, see <http://www.gnu.org/licenses/>.          
                                                                                
# Versioning based on:                                                          
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage         
__author__ = "dryatu (c) 2013"                                                  
__version__ = "1.2.4" 

from FatxFast.gui.hud.frame import Frame
from FatxFast.camera import Camera
from FatxFast.tilemap.editor.tile import TILEWIDTH, TILEHEIGHT
from FatxFast.graphics import SCREENSIZE
import pygame

class MiniMapFrame(Frame):
    """Creates a progression bar based on the turbo left on the supplied player.
    Base image for the progression bar has to be supplied."""

    _player_colors = [(200,150,0), (150,50,150), (0,200,0)]

    def __init__(self, game, flipped=False, loaded=False,**kwargs):
        super(MiniMapFrame, self).__init__(game, **kwargs)
        self._loaded = loaded
        self._temp_surf = pygame.Surface((0,0), pygame.SRCALPHA)
        self.loaded = loaded
        self.camera = Camera((0,0))
        if loaded:
            self.load_minimap()
        # Flipped: True = Center up right, False Right Corner
        self.flipped = flipped
        self.fullscreen = kwargs.get("fullscreen")

    def _calculate_size(self):
        new_rect = pygame.rect.Rect((0,0),(0,0))
        new_rect.width = self.rect.width #self._border.width
        new_rect.height = self.rect.height #self._border.width
        new_rect.x = self.rect.x + self._border.width
        new_rect.y = self.rect.y + self._border.width
        return new_rect

    def draw(self, surface):
        if not self._loaded:
            return

        surface.blit(self._temp_surf, self.rect.topleft)
        surface.blit(self.overlay_surf, self.rect.topleft)
        player_rect = pygame.rect.Rect((0,0), 
            (10, 10))

        for index, player in zip(xrange(
                0,len(self.game.players)), self.game.players):
            if index >= len(self._player_colors):
                index = len(self._player_colors)-1
            player_rect.width = 2+ (player.rect.width/\
                self._map_to_minimap_width_ratio())
            player_rect.height = 2+ (player.rect.height/\
                self._map_to_minimap_height_ratio())
            player_rect.x = self.rect.x+(player.rect.x/\
                self._map_to_minimap_width_ratio())
            player_rect.y = self.rect.y+(player.rect.y/\
                self._map_to_minimap_height_ratio())

            # Do some revealing of the map
            # TODO: Add a more dynamic shadow revealing
            shadow_overlay_rect = player_rect.copy()
            shadow_overlay_rect.size = (player_rect.width*2, 
                player_rect.height*2)
            shadow_overlay_rect.center = player_rect.center
            shadow_overlay_rect.move_ip(-self.rect.x, -self.rect.y)
            self.overlay_surf.fill((0,0,0,0), shadow_overlay_rect)
            surface.fill(self._player_colors[index], player_rect)

        super(MiniMapFrame, self).draw(surface)

    def _map_to_minimap_width_ratio(self):
        return (self.camera.rect.width/float(self.rect.width))

    def _map_to_minimap_height_ratio(self):
        return (self.camera.rect.height/float(self.rect.height))

    def load_minimap(self):
        self.tilemap = self.game.tilemap
        self.camera.rect.size = (self.tilemap.width*TILEWIDTH, 
                self.tilemap.height*TILEHEIGHT)
        self._set_camera_zoom()
        self.tilemap.draw_map(self._temp_surf, self.camera)

        self._temp_surf = pygame.transform.scale(self._temp_surf, 
            self._calculate_size().size)

        if not self.loaded:
            self.overlay_surf = pygame.Surface(self._temp_surf.get_size(), 
                pygame.SRCALPHA)
            self.overlay_surf.fill((0,0,0,255))
        else:
            self.overlay_surf =  pygame.transform.scale(self.overlay_surf,
                self._calculate_size().size)
        self._loaded = True

    def reset_minimap(self):
        template = "flippedminimap" if self.flipped else "minimap"
        self.__init__(self.game, flipped=self.flipped, 
                loaded=False, fullscreen=False,
                **self.fetch_template(template))
        self.load_minimap()

    def _set_camera_zoom(self):
        # Optimize to cache a resized surface
        if self._temp_surf.get_size() != self.camera.rect.size:
            self._temp_surf = pygame.Surface(self.camera.rect.size)


    def toggle_fullscreen(self):
        if self.fullscreen:
            template = "flippedminimap" if self.flipped else "minimap"
            self.__init__(self.game, flipped=self.flipped, 
                loaded=True, fullscreen=False,
                **self.fetch_template(template))
        else:
            self.__init__(self.game, flipped=self.flipped, 
                loaded=True, fullscreen=True,
                **self.fetch_template("fullmap"))

    def toggle_position(self):
        if self.flipped:
            self.__init__(self.game, flipped=False, loaded=True,
                **self.fetch_template("minimap"))
        else:
            self.__init__(self.game, flipped=True, loaded=True, 
                **self.fetch_template("flippedminimap"))