Changeset 204:636c3fafa32d


Ignore:
Timestamp:
Sep 4, 2009, 8:31:16 PM (11 years ago)
Author:
Simon Cross <hodgestar@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@205
Message:

Add counts to buildings.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gamelib/buildings.py

    r199 r204  
    22
    33from pgu.vid import Sprite
     4from pygame.locals import SRCALPHA
     5import pygame
     6import pygame.font
    47
    58import imagecache
    69import tiles
     10import constants
     11
     12import warnings
     13warnings.filterwarnings("ignore", "os.popen3 is deprecated.")
    714
    815class Place(object):
     
    1421        self.offset = offset
    1522
    16     def set_occupant(self, occupant):
    17         self.clear_occupant()
     23    def set_occupant(self, occupant, _update=True):
     24        self.clear_occupant(_update=False)
    1825        self.occupant = occupant
    1926        self.occupant.abode = self
    20 
    21     def clear_occupant(self):
     27        if _update:
     28            self.building.update_occupant_count()
     29
     30    def clear_occupant(self, _update=True):
    2231        if self.occupant is not None:
    2332            self.occupant.abode = None
    2433            self.occupant = None
     34        if _update:
     35            self.building.update_occupant_count()
    2536
    2637    def get_pos(self):
     
    6677        self._sell_price = self.SELL_PRICE
    6778        self._sun_on = True
     79        self._font = pygame.font.SysFont('Vera', 30, bold=True)
     80        self._font_image = pygame.Surface(self.day_image.get_size(), flags=SRCALPHA)
     81        self._font_image.fill((0, 0, 0, 0))
    6882
    6983        self._floors = []
     
    7892            self._floors.append(floor)
    7993
     94        # 0: the main iamge
     95        # 1: above, -1: below
     96        self.draw_stack = {"main": (0, self.day_image)}
     97
    8098        # Create the building somewhere far off screen
    8199        Sprite.__init__(self, self.day_image, (-1000, -1000))
     100
     101    def _redraw(self):
     102        items = self.draw_stack.values()
     103        items.sort(key=lambda x: x[0])
     104        image = items.pop(0)[1].copy()
     105        for _lvl, overlay in items:
     106            image.blit(overlay, (0, 0))
     107        self.setimage(image)
     108
     109    def _replace_main(self, new_main):
     110        self.draw_stack["main"] = (0, new_main)
     111        self._redraw()
    82112
    83113    def tile_positions(self):
     
    138168    def selected(self, selected):
    139169        if selected:
    140             self.setimage(self.selected_image)
     170            self._replace_main(self.selected_image)
    141171        else:
    142172            self.sun(self._sun_on)
     
    145175        self._sun_on = sun_on
    146176        if sun_on:
    147             self.setimage(self.day_image)
     177            self._replace_main(self.day_image)
    148178        else:
    149             self.setimage(self.night_image)
     179            self._replace_main(self.night_image)
     180
     181    def update_occupant_count(self):
     182        count = len(list(self.occupants()))
     183        if count == 0:
     184            if "count" in self.draw_stack:
     185                del self.draw_stack["count"]
     186        else:
     187            image = self._font_image.copy()
     188            text = self._font.render(str(count), True, constants.FG_COLOR)
     189            w, h = image.get_size()
     190            x, y = text.get_size()
     191            image.blit(text, (w - x, h - y))
     192            self.draw_stack["count"] = (1, image)
     193        self._redraw()
    150194
    151195    def floors(self):
Note: See TracChangeset for help on using the changeset viewer.