diff gamemodel.py @ 61:2727d6b78978

Implemented creation of dynamic entities.
author KarstenBock@gmx.net
date Sun, 18 Sep 2011 16:26:12 +0200
parents ba85e5aff370
children e856b604b650
line wrap: on
line diff
--- a/gamemodel.py	Sun Sep 18 16:07:07 2011 +0200
+++ b/gamemodel.py	Sun Sep 18 16:26:12 2011 +0200
@@ -98,20 +98,15 @@
                                           self.engine.getVFS() 
                                           )
 
-    def checkAttributes(self, attributes):
+    def checkAttributes(self, attributes, template):
         """Checks for attributes that where not given in the map file
         and fills them with values from the object database
-        @param attributes: attributes to check
+        @param attributes: attributes to check        
         @type attributes: Dictionary
+        @param template: Template from which the values will be used
         @return: The modified attributes""" 
-        if attributes.has_key("object_type"):
-            class_name = attributes.pop("object_type")
-        else:
-            class_name = attributes["type"]
-        if not attributes.has_key("type"):
-            attributes["type"] = class_name
-        if self.object_db.has_key(class_name):
-            db_attributes = deepcopy(self.object_db[class_name])
+        if self.object_db.has_key(template):
+            db_attributes = deepcopy(self.object_db[template])
             for key in db_attributes.keys():
                 if attributes.has_key(key):
                     attributes[key] = attributes[key] or db_attributes[key]
@@ -158,7 +153,6 @@
         # create the extra data
         extra = {}
         extra['controller'] = self
-        attributes = self.checkAttributes(attributes)
         
         info = {}
         info.update(attributes)
@@ -386,14 +380,11 @@
         agent[unique_agent_id] = agent_values
         self.agents[namespace].update(agent)
         object_model = ""
-        if agent_values.has_key("ObjectModel"): 
-            object_model =  agent_values["ObjectModel"]
-        elif agent_values["ObjectType"] == "MapItem":
-            object_data = self.object_db[agent_values["ItemType"]]
-            object_model = object_data["gfx"] if object_data.has_key("gfx") \
-                        else "generic_item"
+        if agent_values["Entity"].has_key("fifeagent") \
+           and agent_values["Entity"]["fifeagent"].has_key("gfx"): 
+            object_model = agent_values["Entity"]["fifeagent"]["gfx"]
         else:
-            object_model = self.object_db[agent_values["ObjectType"]]["gfx"]
+            object_model = self.object_db[agent_values["Template"]]["fifeagent"]["gfx"]
         import_file = self.agent_import_files[object_model]
         loadImportFile(self.obj_loader, import_file, self.engine)
         
@@ -454,17 +445,12 @@
             new_map.load(map_file)    
 
     def createAgent(self, agent, inst_id, world):
-        object_type = agent["ObjectType"]
-        object_id = agent["ObjectModel"] \
-                                if agent.has_key("ObjectModel") \
-                                else None
-        if object_id == None:
-            if object_type == "MapItem":
-                object_data = self.object_db[agent["ItemType"]]
-                object_id = object_data["gfx"] if object_data.has_key("gfx") \
-                            else "generic_item"
-            else:
-                object_id = self.object_db[object_type]["gfx"]
+        entity_data = agent["Entity"]
+        if agent.has_key("Template"):
+            entity_data = self.checkAttributes(entity_data, agent["Template"])
+        object_id = agent["Entity"]["fifeagent"]["gfx"] \
+                                if agent["Entity"]["fifeagent"].has_key("gfx") \
+                                else "generic_item"
         map_obj = self.fife_model.getObject(str(object_id), "PARPG")
         if not map_obj:
             logging.warning("Object with inst_id={0}, ns=PARPG, "
@@ -474,7 +460,6 @@
         x_pos = agent["Position"][0]
         y_pos = agent["Position"][1]
         z_pos = agent["Position"][2] if len(agent["Position"]) == 3 \
-                                        else -0.1 if object_type == "MapItem" \
                                         else 0.0  
         stack_pos = agent["Stackposition"] if \
                         agent.has_key("StackPosition") \
@@ -496,65 +481,19 @@
 
         if (map_obj.getAction('default')):
             target = fife.Location(self.active_map.agent_layer)
-            inst.act('default', target, True)
+            inst.act('default', target, True)        
+
             
-        inst_dict = {}
-        inst_dict["identifier"] = inst_id
-        inst_dict["world"]= world
-        inst_dict["type"] = object_type
-        inst_dict["gfx"] = object_id
-        if agent.has_key("Open"):
-            inst_dict["is_open"] = parseBool(agent["Open"])
-        if agent.has_key("Locked"):
-            inst_dict["locked"] = parseBool(agent["Locked"])
-        if agent.has_key("ViewName"):
-            inst_dict["view_name"] = agent["ViewName"]
-            inst_dict["real_name"] = agent["RealName"] \
-                     if agent.has_key("RealName") else agent["ViewName"]
-        if agent.has_key("Text"):
-            inst_dict["desc"] = agent["Text"]
+        entity_data["fifeagent"]["identifier"] = inst_id
+        if entity_data["fifeagent"].has_key("behaviour"):
+            entity_data["fifeagent"]["behaviour"] = getattr(behaviours, entity_data["fifeagent"]["behaviour"])()
+        else:
+            entity_data["fifeagent"]["behaviour"] = behaviours.Base()
         if self.dialogues.has_key(inst_id):
-            inst_dict["dialogue"] = self.dialogues[inst_id]
-        if agent.has_key("TargetMap"):
-            inst_dict["target_map_name"] = agent["TargetMap"]
-        if agent.has_key("TargetPosition"):
-            inst_dict["target_x"] = agent["TargetPosition"][0]
-            inst_dict["target_y"] = agent["TargetPosition"][1]
-        if agent.has_key("Inventory"):
-            #TODO: Fix setting of inventory
-            #inventory = Inventory()
-            #inventory_objs = agent["Inventory"]
-            #for inventory_obj in inventory_objs:
-            #    self.createInventoryObject(inventory,
-            #                               inventory_obj 
-            #                               )
-            #inst_dict["inventory"] = inventory
-            pass
-
-        if agent.has_key("Items"):
-            container_objs = agent["Items"]
-            #TODO: Create inventory items
-            items = []#self.createContainerItems(container_objs)
-            inst_dict["items"] = items
-            
-        if agent.has_key("ItemType"):
-            if not agent.has_key("item"):
-                item_data = {}
-                item_data["type"] = agent["ItemType"]
-                item_data["ID"] = inst_id 
-                #TODO item_data = self.createContainerObject(item_data)
-            else:
-                item_data = agent["item"]
-            inst_dict["item"] = item_data
-            inst_dict["item_type"] = agent["ItemType"]
-        if agent.has_key("Behaviour"):
-            inst_dict["behaviour"] = getattr(behaviours, agent["Behaviour"])()
-        #TODO: Add real statistics and bulk
-        if object_type == "Character":
-            inst_dict["statistics"] = {}
-            inst_dict["max_bulk"] = 100
-
-        self.createMapObject(self.active_map.agent_layer, inst_dict)
+            entity_data["dialogue"] = {}
+            entity_data["dialogue"]["dialogue"] = self.dialogues[inst_id]
+        
+        self.createMapObject(self.active_map.agent_layer, entity_data, world)
     
     def placeAgents(self, world):
         """Places the current maps agents """
@@ -618,7 +557,7 @@
         self.active_map.makeActive()
         self.game_state.current_map_name = map_name
 
-    def createMapObject (self, layer, attributes):
+    def createMapObject (self, layer, attributes, world):
         """Create an object and add it to the current map.
            @type layer: fife.Layer
            @param layer: FIFE layer object exists in
@@ -630,13 +569,12 @@
         # create the extra data
         extra = {}
         if layer is not None:
-            extra['layer'] = layer
-        attributes = self.checkAttributes(attributes)
+            extra['fifeagent'] = {}
+            extra['fifeagent']['layer'] = layer
         
-        obj_type = attributes["type"]
-        obj = createEntity(attributes, extra)
+        obj = createEntity(attributes, world, extra)
         if obj:
-            self.addObject(layer, obj, obj_type) 
+            self.addObject(layer, obj) 
 
     def addPC(self, layer, player_char):
         """Add the PlayerCharacter to the map
@@ -655,7 +593,7 @@
         self.game_state.getObjectById("PlayerCharacter").fifeagent.behaviour.speed = self.settings.parpg.PCSpeed
 
 
-    def addObject(self, layer, obj, obj_type):
+    def addObject(self, layer, obj):
         """Adds an object to the map.
            @type layer: fife.Layer
            @param layer: FIFE layer object exists in
@@ -675,7 +613,7 @@
             obj.fifeagent.pos.Y = ref.Y
             obj.fifeagent.gfx = ref.gfx  
              
-        if obj_type == "Character":
+        if obj.fifeagent.behaviour:
             obj.fifeagent.behaviour.parent = obj
             fifeagent.setup_behaviour(obj.fifeagent)
             obj.fifeagent.behaviour.speed = self.settings.parpg.PCSpeed