changeset 569:3ec614e6fd4a

Replace monolithic sell equipment tool with a tool for each type of equipment.
author Simon Cross <hodgestar@gmail.com>
date Sat, 28 Nov 2009 19:59:46 +0000
parents e813365af567
children be47830a56b1
files gamelib/constants.py gamelib/equipment.py gamelib/gameboard.py gamelib/toolbar.py
diffstat 4 files changed, 36 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/gamelib/constants.py	Sat Nov 28 19:37:08 2009 +0000
+++ b/gamelib/constants.py	Sat Nov 28 19:59:46 2009 +0000
@@ -56,9 +56,6 @@
 TOOL_REPAIR_BUILDING = 5
 TOOL_PLACE_ANIMALS = 6
 TOOL_LOGGING = 7
-TOOL_SELL_EQUIPMENT = 8
-TOOL_BUY_BUILDING = 9
-TOOL_BUY_EQUIPMENT = 10
 
 NIGHT_LENGTH = 150
 
--- a/gamelib/equipment.py	Sat Nov 28 19:37:08 2009 +0000
+++ b/gamelib/equipment.py	Sat Nov 28 19:59:46 2009 +0000
@@ -230,7 +230,7 @@
     DRAW_LAYER = 14
 
 def is_equipment(obj):
-    """Return true if obj is a build class."""
+    """Return true if obj is an equipment class."""
     return getattr(obj, "IS_EQUIPMENT", False) and hasattr(obj, "NAME")
 
 def is_weapon(obj):
--- a/gamelib/gameboard.py	Sat Nov 28 19:37:08 2009 +0000
+++ b/gamelib/gameboard.py	Sat Nov 28 19:59:46 2009 +0000
@@ -279,8 +279,9 @@
             elif tool == constants.TOOL_SELL_EGG:
                 self.sell_egg(None)
                 return True
-            elif tool == constants.TOOL_SELL_EQUIPMENT:
-                self.sell_equipment(None)
+            elif toolbar.SellToolBar.is_equip_tool(tool):
+                equipment_cls = toolbar.SellToolBar.get_equip_cls(tool)
+                self.sell_equipment(None, equipment_cls)
                 return True
             elif equipment.is_equipment(tool):
                 self.buy_equipment(None, tool)
@@ -386,12 +387,13 @@
                 self.select_chicken(self.tv.screen_to_tile(e.pos))
         elif self.selected_tool == constants.TOOL_SELL_BUILDING:
             self.sell_building(self.tv.screen_to_tile(e.pos))
-        elif self.selected_tool == constants.TOOL_SELL_EQUIPMENT:
-            self.sell_equipment(self.tv.screen_to_tile(e.pos))
         elif self.selected_tool == constants.TOOL_REPAIR_BUILDING:
             self.repair_building(self.tv.screen_to_tile(e.pos))
         elif buildings.is_building(self.selected_tool):
             self.buy_building(self.tv.screen_to_tile(e.pos), self.selected_tool)
+        elif toolbar.SellToolBar.is_equip_tool(self.selected_tool):
+            equipment_cls = toolbar.SellToolBar.get_equip_cls(self.selected_tool)
+            self.sell_equipment(self.tv.screen_to_tile(e.pos), equipment_cls)
         elif equipment.is_equipment(self.selected_tool):
             if not self.selected_chickens:
                 # old selection behaviour
@@ -836,19 +838,16 @@
         self.add_wood(-building.repair_price())
         building.repair()
 
-    def sell_equipment(self, tile_pos):
+    def sell_equipment(self, tile_pos, equipment_cls):
         x, y = 0, 0
         def do_sell(chicken, update_button=None):
-            if not chicken.equipment:
-                return
-            elif len(chicken.equipment) == 1:
-                item = chicken.equipment[0]
+            items = [item for item in chicken.equipment
+                if isinstance(item, equipment_cls)]
+            for item in items:
                 self.add_cash(item.sell_price())
                 chicken.unequip(item)
                 if update_button:
                     update_button(chicken)
-            else:
-                self.open_equipment_dialog(chicken, x, y, update_button)
             return False
         if tile_pos:
             chicken = self.get_outside_chicken(tile_pos)
--- a/gamelib/toolbar.py	Sat Nov 28 19:37:08 2009 +0000
+++ b/gamelib/toolbar.py	Sat Nov 28 19:59:46 2009 +0000
@@ -386,15 +386,38 @@
         self.group = gui.Group(name='sell_toolbar', value=None)
         self.make_toolbar()
 
+    @staticmethod
+    def make_equip_tool(equipment_cls):
+        tool = ("sell_tool", equipment_cls)
+        return tool
+
+    @staticmethod
+    def is_equip_tool(tool):
+        return type(tool) == tuple and tool[0] == "sell_tool"
+
+    @staticmethod
+    def get_equip_cls(tool):
+        return tool[1]
+
     def make_toolbar(self):
         self.add_heading("Sell ...")
         self.add_tool_button("Chicken", constants.TOOL_SELL_CHICKEN,
                 self.gameboard.level.sell_price_chicken, cursors.cursors['sell'])
         self.add_tool_button("Egg", constants.TOOL_SELL_EGG,
                 self.gameboard.level.sell_price_egg, cursors.cursors['sell'])
-        self.add_tool_button("Equipment", constants.TOOL_SELL_EQUIPMENT,
-                None, cursors.cursors['sell'])
         self.add_spacer(15)
+
+        for equipment_cls in equipment.EQUIPMENT:
+            tool = self.make_equip_tool(equipment_cls)
+            self.add_tool_button(equipment_cls.NAME.title(),
+                    tool,
+                    equipment_cls.SELL_PRICE,
+                    cursors.cursors.get('sell', None))
+        self.add_spacer(15)
+
+        #self.add_tool_button("Equipment", constants.TOOL_SELL_EQUIPMENT,
+        #        None, cursors.cursors['sell'])
+
         self.add_tool('Done', self.add_default_toolbar)
 
     def add_default_toolbar(self):