changeset 429:42777630956a

Add ammo to things serialized and deserialized.
author Simon Cross <hodgestar@gmail.com>
date Sat, 21 Nov 2009 16:28:27 +0000
parents a356e57529ea
children db7bb20d2336
files gamelib/animal.py gamelib/equipment.py
diffstat 2 files changed, 8 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/animal.py	Sat Nov 21 16:21:38 2009 +0000
+++ b/gamelib/animal.py	Sat Nov 21 16:28:27 2009 +0000
@@ -275,12 +275,8 @@
 
     def reload_weapon(self):
         """If we have a weapon that takes ammunition, reload it."""
-        if not self.weapons():
-            # Nothing to reload
-            return
         for weapon in self.weapons():
-            if hasattr(weapon, 'AMMUNITION'):
-                weapon.ammunition = weapon.AMMUNITION
+            weapon.refresh_ammo()
 
 class Egg(Animal):
     """An egg"""
--- a/gamelib/equipment.py	Sat Nov 21 16:21:38 2009 +0000
+++ b/gamelib/equipment.py	Sat Nov 21 16:28:27 2009 +0000
@@ -12,17 +12,20 @@
     DRAW_LAYER = 0
     UNDER_LIMB = False
     UNDER_EYE = False
+    AMMUNITION = None
 
     SIMPLIFY = [
         '_buy_price',
         '_sell_price',
         '_name',
+        'ammunition',
     ]
 
     def __init__(self):
         self._buy_price = self.BUY_PRICE
         self._sell_price = self.SELL_PRICE
         self._name = self.NAME
+        self.refresh_ammo()
 
     def make(cls):
         """Override default Simplifiable object creation."""
@@ -58,6 +61,9 @@
                 eq_image_right.blit(eye_right, (0,0))
         return eq_image_left, eq_image_right, self.DRAW_LAYER
 
+    def refresh_ammo(self):
+        self.ammunition = getattr(self, 'AMMUNITION', None)
+
 class Weapon(Equipment):
     IS_WEAPON = True
     DRAW_LAYER = 10
@@ -77,7 +83,7 @@
 
     def hit(self, gameboard, wielder, target):
         """Is the potentially unlucky target actually unlucky?"""
-        if hasattr(self, 'AMMUNITION'):
+        if self.ammunition is not None:
             if self.ammunition <= 0:
                 # Out of ammunition, so we don't get to shoot.
                 return