diff src/parpg/gamemodel.py @ 162:79c186b69603

Split code off from createInventoryItems to createItemByID and createItemByType. Entities with a containable component will now get the "item_type" field set to the Template, if that field is not present.
author KarstenBock@gmx.net
date Sat, 08 Oct 2011 14:18:16 +0200
parents d1f9652d0651
children 097782ae9c77
line wrap: on
line diff
--- a/src/parpg/gamemodel.py	Fri Oct 07 18:15:09 2011 +0200
+++ b/src/parpg/gamemodel.py	Sat Oct 08 14:18:16 2011 +0200
@@ -371,8 +371,10 @@
             return None
         entity_data = deepcopy(agent["Entity"])
         entity_data["fifeagent"] = {}
+        template = None
         if agent.has_key("Template"):
-            entity_data = self.checkAttributes(entity_data, agent["Template"])
+            template = agent["Template"]
+            entity_data = self.checkAttributes(entity_data, template)
         object_id = (entity_data["graphics"]["gfx"] 
                      if entity_data.has_key("graphics") and 
                      entity_data["graphics"].has_key("gfx") 
@@ -418,13 +420,18 @@
             entity_data["fifeagent"]["behaviour"] = behaviours.Base()
         if self.dialogues.has_key(inst_id):
             entity_data["dialogue"] = {}
-            entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id]           
+            entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id]
+        if (entity_data.has_key("containable") and not 
+            entity_data["containable"].has_key("item_type")
+            ):
+            entity_data["containable"]["item_type"] = template
+            
                        
         obj = self.createMapObject(self.active_map.agent_layer, entity_data, inst_id, world)
 
         if agent.has_key("Inventory"):
             inv = agent["Inventory"]
-            self.create_inventory_items(inv, obj, world)
+            self.createInventoryItems(inv, obj, world)
 
         if agent.has_key("Equipment"):
             for slot, data in agent["Equipment"].iteritems():
@@ -434,7 +441,7 @@
                     item_data = {}
                     item_data = self.checkAttributes(item_data, item_type)
                     if item_data.has_key("containable") and item_data.has_key("equipable"):
-                        item = self.create_item(
+                        item = self.createItem(
                             self.createUniqueID(data["ID"]), 
                             item_data, world, item_type)
                     else:
@@ -446,14 +453,14 @@
                     else:
                         item_data = self.items[identifier]["Entity"]
                         item_type = item_data["containable"]["item_type"]
-                        item = self.create_item(identifier, item_data,
+                        item = self.createItem(identifier, item_data,
                                                 world, item_type)
                 equip.equip(obj.equip, item.equipable, slot)
         if (obj.fifeagent and (obj.lockable and not obj.lockable.closed)):
             obj.fifeagent.behaviour.animate("opened", repeating=True)
         return obj
 
-    def create_inventory_items(self, inv, obj, world):
+    def createInventoryItems(self, inv, obj, world):
         slots = inv["Slots"]
         obj.container.children = list()
         for x in xrange(slots):
@@ -464,31 +471,37 @@
             slot = data["Slot"] if data.has_key("Slot") else -1
             if data.has_key("type"):
                 item_type = data["type"]
-                item_data = {}
-                item_data = self.checkAttributes(item_data, item_type)
-                if item_data.has_key("containable"):
-                    item = self.create_item(
-                        self.createUniqueID(data["ID"]), 
-                        item_data, world, item_type)
-                else:
-                    raise Exception("Item %s is not containable." % item_type)
+                item = self.createItemByType(item_type, data["ID"], world)
             else:
                 identifier = data["ID"]
-                if self.game_state.hasObject(identifier):
-                    item = self.game_state.getObjectById(identifier)
-                else:
-                    agent_data = self.items[identifier]
-                    item_data = agent_data["Entity"]
-                    item_type = item_data["containable"]["item_type"]
-                    item = self.create_item(identifier, item_data,
-                                            world, item_type)
-                    if item.container and agent_data.has_key("Inventory"):
-                        self.create_inventory_items(agent_data["Inventory"],
-                                                    item, world)
+                item = self.createItemByID(world, identifier)
                     
             container.put_item(obj.container, item.containable, slot)
 
-    def create_item(self, identifier, item_data, world, item_type):
+    def createItemByID(self, world, identifier):
+        if self.game_state.hasObject(identifier):
+            item = self.game_state.getObjectById(identifier)
+        else:
+            agent_data = self.items[identifier]
+            item_data = agent_data["Entity"]
+            item_type = item_data["containable"]["item_type"]
+            item = self.createItem(identifier, item_data,
+                                    world, item_type)
+            if item.container and agent_data.has_key("Inventory"):
+                self.createInventoryItems(agent_data["Inventory"],
+                                            item, world)
+        return item
+
+    def createItemByType(self, item_type, identifier, world):
+        item_data = {}
+        item_data = self.checkAttributes(item_data, item_type)
+        if item_data.has_key("containable"):
+            return self.createItem( self.createUniqueID(identifier), 
+                                    item_data, world, item_type)
+        else:
+            raise Exception("Item %s is not containable." % item_type)
+
+    def createItem(self, identifier, item_data, world, item_type):
         if not item_data["description"].has_key("view_name"):
             item_data["description"]["view_name"] = (
              item_data["description"]["real_name"])