# HG changeset patch # User Neil Muller # Date 1282731676 -7200 # Node ID 5845a3ed4dadf44989bab39c6a91ec91372383db # Parent 0db92b3b5833f5e2156ac8c000108c2d753ece72 Inventory interaction can (surprisingly) affect the inventory diff -r 0db92b3b5833 -r 5845a3ed4dad gamelib/gamescreen.py --- a/gamelib/gamescreen.py Wed Aug 25 12:03:08 2010 +0200 +++ b/gamelib/gamescreen.py Wed Aug 25 12:21:16 2010 +0200 @@ -39,7 +39,7 @@ self.unselect() else: if self.state.tool: - result = self.state.inventory[item_no].interact(self.state.tool) + result = self.state.inventory[item_no].interact(self.state.tool, self.state) if result: result.process(self.scene_widget) else: diff -r 0db92b3b5833 -r 5845a3ed4dad gamelib/scenes/cryo.py --- a/gamelib/scenes/cryo.py Wed Aug 25 12:03:08 2010 +0200 +++ b/gamelib/scenes/cryo.py Wed Aug 25 12:21:16 2010 +0200 @@ -84,9 +84,6 @@ INVENTORY_IMAGE = "titanium_femur.png" CURSOR = CursorSprite('titanium_femur_cursor.png', 47, 3) - def interact_with_full_can(self, tool): - return Result("You club the can with the femur. It doesn't help") - class CryoUnitAlpha(Thing): "Cryo unit containing titanium leg." diff -r 0db92b3b5833 -r 5845a3ed4dad gamelib/scenes/mess.py --- a/gamelib/scenes/mess.py Wed Aug 25 12:03:08 2010 +0200 +++ b/gamelib/scenes/mess.py Wed Aug 25 12:21:16 2010 +0200 @@ -30,13 +30,23 @@ INVENTORY_IMAGE = "empty_can.png" CURSOR = CursorSprite('empty_can_cursor.png', 47, 3) - class FullCan(Item): "Found on the shelf." INVENTORY_IMAGE = "full_can.png" CURSOR = CursorSprite('full_can_cursor.png', 47, 3) + def interact_with_titanium_leg(self, tool, state): + state.replace_inventory_item(self, DentedCan("dented_can")) + return Result("You club the can with the femur. The can gets dented, but doesn't open.") + + +class DentedCan(FullCan): + "A can banged on with the femur" + + def interact_with_titanium_leg(self, tool, inventory): + return Result("You club the can with the femur. The dents shift around, but it still doesn't open.") + class TubeFragments(Item): "Old tubes that need repair." diff -r 0db92b3b5833 -r 5845a3ed4dad gamelib/state.py --- a/gamelib/state.py Wed Aug 25 12:03:08 2010 +0200 +++ b/gamelib/state.py Wed Aug 25 12:21:16 2010 +0200 @@ -120,6 +120,17 @@ if self.tool == self.items[name]: self.set_tool(None) + def replace_inventory_item(self, old_item, new_item): + """Try to replace an item in the inventory with a new one""" + try: + index = self.inventory.index(old_item) + self.inventory[index] = new_item + if self.tool == old_item: + self.set_tool(new_item) + except ValueError: + return False + return True + def set_tool(self, item): self.tool = item if item is None: @@ -551,16 +562,16 @@ def get_inventory_image(self): return self.inventory_image - def interact(self, tool): + def interact(self, tool, state): handler = getattr(self, 'interact_with_' + tool.name, None) inverse_handler = getattr(tool, 'interact_with_' + self.name, None) if handler is not None: - return handler(tool) + return handler(tool, state) elif inverse_handler is not None: - return inverse_handler(self) + return inverse_handler(self, state) else: - return self.interact_default(tool) + return self.interact_default(tool, state) - def interact_default(self, tool): + def interact_default(self, tool, state): return Result("That doesn't do anything useful")