Changeset 195:9d31cfc3afde


Ignore:
Timestamp:
Sep 4, 2009, 7:26:02 PM (11 years ago)
Author:
Adrianna Pińska <adrianna.pinska@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@196
Message:

refactor of layers drawn on chicken, to allow for extended functionality

Location:
gamelib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • gamelib/animal.py

    r187 r195  
    2929            self.pos = Position(tile_pos[0], tile_pos[1])
    3030        self.equipment = []
     31        self.accoutrements = []
    3132        self.abode = None
    3233        self.facing = 'left'
     
    7172
    7273    def redraw(self):
    73         self.image_left = self._image_left.copy()
    74         self.image_right = self._image_right.copy()
    75         self.equipment.sort(key=lambda x: x.DRAW_LAYER)
    76         for item in self.equipment:
    77             self.draw_equipment(item)
     74        layers = [(self._image_left.copy(), self._image_right.copy(), 0)]
     75        if hasattr(self, 'EQUIPMENT_IMAGE_ATTRIBUTE'):
     76            for item in self.accoutrements + self.equipment:
     77                images = item.images(self.EQUIPMENT_IMAGE_ATTRIBUTE)
     78                if images:
     79                    layers.append(images)
     80
     81        layers.sort(key=lambda l: l[2])
     82
     83        self.image_left = layers[0][0]
     84        self.image_right = layers[0][1]
     85        for l in layers[1:]:
     86            self.image_left.blit(l[0], (0,0))
     87            self.image_right.blit(l[1], (0,0))
     88
    7889        self._set_image_facing(self.facing)
    79 
    80     def draw_equipment(self, item):
    81         if not hasattr(self, 'EQUIPMENT_IMAGE_ATTRIBUTE'):
    82             return
    83         eq_image_attr = getattr(item, self.EQUIPMENT_IMAGE_ATTRIBUTE, 'None')
    84         if not eq_image_attr:
    85             return
    86         eq_image_left = imagecache.load_image(eq_image_attr)
    87         eq_image_right = imagecache.load_image(eq_image_attr, ("right_facing",))
    88         self.image_left.blit(eq_image_left, (0, 0))
    89         self.image_right.blit(eq_image_right, (0, 0))
    9090
    9191    def weapons(self):
  • gamelib/equipment.py

    r189 r195  
    33import random
    44import sound
     5import imagecache
    56
    67class Equipment(object):
     
    2122    def name(self):
    2223        return self._name
     24
     25    def images(self, eq_image_attr):
     26        eq_image_file = getattr(self, eq_image_attr, None)
     27        if not eq_image_file:
     28            return None
     29        eq_image_left = imagecache.load_image(eq_image_file)
     30        eq_image_right = imagecache.load_image(eq_image_file, ("right_facing",))
     31        return eq_image_left, eq_image_right, self.DRAW_LAYER
    2332
    2433class Weapon(Equipment):
     
    8493
    8594    def place(self, animal):
    86         """Give additional lives"""
    8795        for eq in animal.equipment:
    8896            if eq.NAME == self.NAME:
     
    115123    CHICKEN_IMAGE_FILE = 'sprites/kevlar.png'
    116124
     125class Accoutrement(Equipment):
     126    """Things which are not equipment, but are displayed in the same way"""
     127    IS_EQUIPMENT = False
     128    BUY_PRICE = 0
     129    SELL_PRICE = 0
     130
     131    def place(self, animal):
     132        for eq in animal.accoutrements:
     133            if eq.NAME == self.NAME:
     134                return False
     135        return True
     136
     137class Spotlight(Accoutrement):
     138    NAME = "spotlight"
     139    CHICKEN_IMAGE_FILE = 'sprites/select_chkn.png'
     140    DRAW_LAYER = -5
     141
     142class Nest(Accoutrement):
     143    NAME = "nest"
     144    CHICKEN_IMAGE_FILE = 'sprites/nest.png'
     145    DRAW_LAYER = 15
     146
    117147def is_equipment(obj):
    118148    """Return true if obj is a build class."""
Note: See TracChangeset for help on using the changeset viewer.