diff src/parpg/gamemodel.py @ 105:7829eb185d6f

(Re)added setting of inventory and equipment in the object files.
author KarstenBock@gmx.net
date Thu, 22 Sep 2011 16:18:07 +0200
parents 0f659c7675f6
children f3ace79da781
line wrap: on
line diff
--- a/src/parpg/gamemodel.py	Thu Sep 22 14:39:00 2011 +0200
+++ b/src/parpg/gamemodel.py	Thu Sep 22 16:18:07 2011 +0200
@@ -31,7 +31,7 @@
 from parpg.dialogueparsers import YamlDialogueParser, DialogueFormatError
 from parpg.entities import createEntity
 from parpg import behaviours
-from parpg.components import fifeagent
+from parpg.components import fifeagent, container, equip
 
 try:
     import xml.etree.cElementTree as ElementTree
@@ -133,65 +133,6 @@
             
             ID = ID + "_" + str(id_number)
         return ID
-
-    def createContainerItems(self, container_objs):
-        """Create the items of a container from a dictionary
-        @param container_objs: Dictionary containing the items
-        @type container_objs: dict"""
-        items = []
-        for container_obj in container_objs:
-            items.append(self.createContainerObject(container_obj))
-        
-        return items
-
-    def createContainerObject(self, attributes):
-        """Create an object that can be stored in 
-        an container and return it
-        @param attributes: Dictionary of all object attributes
-        @type attributes: Dictionary
-        @return: The created object """
-        # create the extra data
-        extra = {}
-        extra['controller'] = self
-        
-        info = {}
-        info.update(attributes)
-        info.update(extra)
-        ID = info.pop("id") if info.has_key("id") else info.pop("ID")
-        if not info.has_key("item_type"):
-            info["item_type"] = info["type"]
-        ID = self.createUniqueID(ID)
-        if info.has_key("attributes"):
-            attributes = info["attributes"]
-            if "Container" in attributes:
-                info["actions"]["Open"] = ""
-                if info.has_key("Items"):
-                    inventory_objs = info["Items"]
-                    info["items"] = self.createContainerItems(inventory_objs)
-                
-                new_item = CarryableContainer(ID = ID, **info) 
-            else:
-                new_item = CarryableItem(ID = ID, **info) 
-        else:
-            new_item = CarryableItem(ID = ID, **info) 
-        self.game_state.addObject(None, new_item)
-        return new_item
-      
-    def createInventoryObject(self, container, attributes):
-        """Create an inventory object and place it into a container
-           @type container: base.Container
-           @param container: Container where the item is on
-           @type attributes: Dictionary
-           @param attributes: Dictionary of all object attributes
-           @return: None"""
-        index = attributes.pop("index") if attributes.has_key("index") else None
-        slot = attributes.pop("slot") if attributes.has_key("slot") else None
-        obj = self.createContainerObject(attributes)        
-        #obj = createObject(attributes, extra)
-        if slot:
-            container.moveItemToSlot(obj, slot)
-        else:
-            container.placeItem(obj, index)
     
     def deleteObject(self, object_id):
         """Removes an object from the game
@@ -491,10 +432,42 @@
             entity_data["fifeagent"]["behaviour"] = behaviours.Base()
         if self.dialogues.has_key(inst_id):
             entity_data["dialogue"] = {}
-            entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id]
-        
-        self.createMapObject(self.active_map.agent_layer, entity_data, world)
-    
+            entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id]           
+                       
+        obj = self.createMapObject(self.active_map.agent_layer, entity_data, world)
+
+        if agent.has_key("Inventory"):
+            inv = agent["Inventory"]
+            slots = inv["Slots"]
+            obj.container.children = list()
+            for x in xrange(slots):
+                obj.container.children.append(None)
+            items = inv["Items"] if inv.has_key("Items") else list()
+            for data in items:
+                item_data = {}
+                item_data = self.checkAttributes(item_data, data["type"])
+                if item_data.has_key("containable"):
+                    item = createEntity(item_data, world, None)
+                    self.game_state.addObject(self.createUniqueID(data["ID"]), None, item)
+                    container.put_item(obj.container, item.containable)
+                else:
+                    raise Exception("Item %s is not containable." % data["type"])
+
+        if agent.has_key("Equipment"):
+            for slot, data in agent["Equipment"].iteritems():
+                item_data = {}
+                item_data = self.checkAttributes(item_data, data["type"])
+                if item_data.has_key("containable") and item_data.has_key("equipable"):
+                    if not item_data["equipable"].has_key("image"):
+                        item_data["equipable"]["image"]=item_data["containable"]["image"]
+                    if not item_data["containable"].has_key("image"):
+                        item_data["containable"]["image"]=item_data["equipable"]["image"]
+                    item = createEntity(item_data, world, None)
+                    self.game_state.addObject(self.createUniqueID(data["ID"]), None, item)
+                    equip.equip(obj.equip, item.equipable, slot)
+                else:
+                    raise Exception("Item %s is not containable or equipable." % data["type"])
+
     def placeAgents(self, world):
         """Places the current maps agents """
         if not self.active_map:
@@ -565,7 +538,7 @@
            @param attributes: Dictionary of all object attributes
            @type instance: fife.Instance
            @param instance: FIFE instance corresponding to the object
-           @return: None"""
+           @return: The created object"""
         # create the extra data
         extra = {}
         if layer is not None:
@@ -574,7 +547,8 @@
         
         obj = createEntity(attributes, world, extra)
         if obj:
-            self.addObject(layer, obj) 
+            self.addObject(layer, obj)
+        return obj
 
     def addPC(self, layer, player_char):
         """Add the PlayerCharacter to the map
@@ -606,7 +580,7 @@
                                             self.game_state.current_map_name) 
         if ref is None:
             # no, add it to the game state
-            self.game_state.addObject(self.game_state.current_map_name, obj)
+            self.game_state.addObject(obj.fifeagent.identifier, self.game_state.current_map_name, obj)
         else:
             # yes, use the current game state data
             obj.fifeagent.pos.X = ref.X