changeset 170:371b17bc9113

Added ReplaceItemAction, to dialogueactions, which replaces a single item in the players inventory.
author KarstenBock@gmx.net
date Sun, 09 Oct 2011 14:39:02 +0200
parents 87073af1d2d2
children 7f03365c098d
files src/parpg/dialogueactions.py
diffstat 1 files changed, 35 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/parpg/dialogueactions.py	Sun Oct 09 14:38:54 2011 +0200
+++ b/src/parpg/dialogueactions.py	Sun Oct 09 14:39:02 2011 +0200
@@ -153,7 +153,8 @@
             item = container.take_item(game_state['npc'].container, item_type)
             if (item):
                 container.put_item(game_state['pc'].container, item)
-                print("{0} gave you the {1}".format(game_state['npc'].description.view_name,
+                print("{0} gave you the {1}".format(game_state['npc'].
+                                                    description.view_name,
                                                     item_type))
             else:
                 print("{0} doesn't have the {1}".format(game_state['npc'].
@@ -190,6 +191,39 @@
 DialogueAction.registerAction(GiveStuffAction)
 
 
+class ReplaceItemAction(InventoryAction):
+    """
+    L{InventoryAction} used to replace an item with another in the player's
+    inventory.
+    """
+    
+    keyword = 'replace_item'
+    
+    def __call__(self, game_state):
+        """
+        Take an item from the player and place another at its place.
+        
+        @param game_state: variables and functions that make up the current
+            game state.
+        @type game_state: dict of objects
+        """
+        old_type = self.item_types[0]
+        new_type = self.item_types[1]
+        item = container.take_item(game_state['pc'].container, old_type)
+        if item:
+            model = game_state['model']
+            new_item = model.createItemByType(new_type, new_type, 
+                                              item.entity.world)
+            container.put_item(game_state['pc'].container, 
+                               new_item.containable, item.slot)
+            model.deleteObject(item.entity.general.identifier)
+            item.delete()
+            print("{0} took the {1} and gave you the {2}".format(
+                game_state['npc'].description.view_name, old_type, item_type))
+        else:
+            print("You don't have the {0}".format(old_type))
+DialogueAction.registerAction(ReplaceItemAction)
+        
 class QuestAction(DialogueAction):
     """
     Abstract base class for quest-related L{DialogueActions<DialogueAction>}.