changeset 195:9d31cfc3afde

refactor of layers drawn on chicken, to allow for extended functionality
author Adrianna Pińska <adrianna.pinska@gmail.com>
date Fri, 04 Sep 2009 19:26:02 +0000
parents 5ec222ca07cd
children edcb5edfa0ff
files gamelib/animal.py gamelib/equipment.py
diffstat 2 files changed, 47 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/animal.py	Fri Sep 04 18:45:45 2009 +0000
+++ b/gamelib/animal.py	Fri Sep 04 19:26:02 2009 +0000
@@ -28,6 +28,7 @@
         else:
             self.pos = Position(tile_pos[0], tile_pos[1])
         self.equipment = []
+        self.accoutrements = []
         self.abode = None
         self.facing = 'left'
 
@@ -70,23 +71,22 @@
         self.redraw()
 
     def redraw(self):
-        self.image_left = self._image_left.copy()
-        self.image_right = self._image_right.copy()
-        self.equipment.sort(key=lambda x: x.DRAW_LAYER)
-        for item in self.equipment:
-            self.draw_equipment(item)
-        self._set_image_facing(self.facing)
+        layers = [(self._image_left.copy(), self._image_right.copy(), 0)]
+        if hasattr(self, 'EQUIPMENT_IMAGE_ATTRIBUTE'):
+            for item in self.accoutrements + self.equipment:
+                images = item.images(self.EQUIPMENT_IMAGE_ATTRIBUTE)
+                if images:
+                    layers.append(images)
 
-    def draw_equipment(self, item):
-        if not hasattr(self, 'EQUIPMENT_IMAGE_ATTRIBUTE'):
-            return
-        eq_image_attr = getattr(item, self.EQUIPMENT_IMAGE_ATTRIBUTE, 'None')
-        if not eq_image_attr:
-            return
-        eq_image_left = imagecache.load_image(eq_image_attr)
-        eq_image_right = imagecache.load_image(eq_image_attr, ("right_facing",))
-        self.image_left.blit(eq_image_left, (0, 0))
-        self.image_right.blit(eq_image_right, (0, 0))
+        layers.sort(key=lambda l: l[2])
+
+        self.image_left = layers[0][0]
+        self.image_right = layers[0][1]
+        for l in layers[1:]:
+            self.image_left.blit(l[0], (0,0))
+            self.image_right.blit(l[1], (0,0))
+
+        self._set_image_facing(self.facing)
 
     def weapons(self):
         return [e for e in self.equipment if equipment.is_weapon(e)]
--- a/gamelib/equipment.py	Fri Sep 04 18:45:45 2009 +0000
+++ b/gamelib/equipment.py	Fri Sep 04 19:26:02 2009 +0000
@@ -2,6 +2,7 @@
 
 import random
 import sound
+import imagecache
 
 class Equipment(object):
     IS_EQUIPMENT = True
@@ -21,6 +22,14 @@
     def name(self):
         return self._name
 
+    def images(self, eq_image_attr):
+        eq_image_file = getattr(self, eq_image_attr, None)
+        if not eq_image_file:
+            return None
+        eq_image_left = imagecache.load_image(eq_image_file)
+        eq_image_right = imagecache.load_image(eq_image_file, ("right_facing",))
+        return eq_image_left, eq_image_right, self.DRAW_LAYER
+
 class Weapon(Equipment):
     IS_WEAPON = True
     DRAW_LAYER = 10
@@ -83,7 +92,6 @@
         self.hitpoints = self.STARTING_HITPOINTS
 
     def place(self, animal):
-        """Give additional lives"""
         for eq in animal.equipment:
             if eq.NAME == self.NAME:
                 return False
@@ -114,6 +122,28 @@
 
     CHICKEN_IMAGE_FILE = 'sprites/kevlar.png'
 
+class Accoutrement(Equipment):
+    """Things which are not equipment, but are displayed in the same way"""
+    IS_EQUIPMENT = False
+    BUY_PRICE = 0
+    SELL_PRICE = 0
+
+    def place(self, animal):
+        for eq in animal.accoutrements:
+            if eq.NAME == self.NAME:
+                return False
+        return True
+
+class Spotlight(Accoutrement):
+    NAME = "spotlight"
+    CHICKEN_IMAGE_FILE = 'sprites/select_chkn.png'
+    DRAW_LAYER = -5
+
+class Nest(Accoutrement):
+    NAME = "nest"
+    CHICKEN_IMAGE_FILE = 'sprites/nest.png'
+    DRAW_LAYER = 15
+
 def is_equipment(obj):
     """Return true if obj is a build class."""
     return getattr(obj, "IS_EQUIPMENT", False) and hasattr(obj, "NAME")