diff gamelib/equipment.py @ 109:48019afde338

Equipment purchasing and some toolbar tweaks.
author Jeremy Thurgood <firxen@gmail.com>
date Wed, 02 Sep 2009 18:46:10 +0000
parents f5d56688943b
children 4c2fbab20abe
line wrap: on
line diff
--- a/gamelib/equipment.py	Wed Sep 02 18:42:00 2009 +0000
+++ b/gamelib/equipment.py	Wed Sep 02 18:46:10 2009 +0000
@@ -4,27 +4,64 @@
 import sound
 
 class Equipment(object):
-    is_weapon = False
+    IS_EQUIPMENT = True
+
+    def __init__(self):
+        self._buy_price = self.BUY_PRICE
+        self._sell_price = self.SELL_PRICE
+        self._name = self.NAME
+
+    def buy_price(self):
+        return self._buy_price
+
+    def sell_price(self):
+        return self._sell_price
+
+    def name(self):
+        return self._name
 
 class Weapon(Equipment):
-    is_weapon = True
+    IS_WEAPON = True
 
     def in_range(self, gameboard, wielder, target):
         """Can the lucky wielder hit the potentially unlucky target with this?"""
-        return False
+        return wielder.pos.dist(target.pos) <= self.RANGE
 
     def hit(self, gameboard, wielder, target):
         """Is the potentially unlucky target actually unlucky?"""
-        return False
+        if hasattr(self, 'HIT_SOUND'):
+            sound.play_sound(self.HIT_SOUND)
+        roll = random.randint(1, 100)
+        return roll > self.BASE_HIT + self.RANGE_MODIFIER*wielder.pos.dist(target.pos)
+
+    def place(self, animal):
+        for eq in animal.equipment:
+            if self.NAME == eq.NAME:
+                return False
+        return True
 
 class Rifle(Weapon):
-    def in_range(self, gameboard, wielder, target):
-        """For now, we ignore terrain and just assume we can hit
-        anything that isn't too far away."""
-        return wielder.pos.dist(target.pos) <= 3
+    NAME = "rifle"
+    BUY_PRICE = 20
+    SELL_PRICE = 15
+
+    RANGE = 3
+    BASE_HIT = 50
+    RANGE_MODIFIER = 10
+    HIT_SOUND = "fire-rifle.ogg"
 
-    def hit(self, gameboard, wielder, target):
-        """Closer is more accurate."""
-        sound.play_sound("fire-rifle.ogg")
-        return random.randint(1, 100) > 60 + 10*wielder.pos.dist(target.pos)
+def is_equipment(obj):
+    """Return true if obj is a build class."""
+    return getattr(obj, "IS_EQUIPMENT", False) and hasattr(obj, "NAME")
+
+def is_weapon(obj):
+    return is_equipment(obj) and getattr(obj, 'IS_WEAPON', False)
 
+EQUIPMENT = []
+for name in dir():
+    obj = eval(name)
+    try:
+        if is_equipment(obj):
+            EQUIPMENT.append(obj)
+    except TypeError:
+        pass