diff gamemodel.py @ 88:0411a4bcceee

Fixed moving between maps.
author KarstenBock@gmx.net
date Mon, 26 Sep 2011 15:44:42 +0200
parents a9cc5559ec2a
children 939984cff702
line wrap: on
line diff
--- a/gamemodel.py	Sat Sep 24 16:48:06 2011 +0200
+++ b/gamemodel.py	Mon Sep 26 15:44:42 2011 +0200
@@ -31,6 +31,7 @@
 from parpg.dialogueparsers import YamlDialogueParser, DialogueFormatError
 from parpg.entities import createEntity
 from parpg import behaviours
+from parpg import components
 from parpg.components import fifeagent, container, equip
 
 try:
@@ -398,17 +399,17 @@
             new_map.load(map_file)    
 
     def createAgent(self, agent, inst_id, world):
-        entity_data = agent["Entity"]
+        entity_data = deepcopy(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") \
+        object_id = entity_data["fifeagent"]["gfx"] \
+                                if entity_data["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, "
                                   "could not be found. "
-                                  "Omitting...".format(str(obj_id)))
+                                  "Omitting...".format(str(object_id)))
 
         x_pos = agent["Position"][0]
         y_pos = agent["Position"][1]
@@ -599,7 +600,7 @@
             # yes, use the current game state data
             obj.fifeagent.pos.X = ref.X
             obj.fifeagent.pos.Y = ref.Y
-            obj.fifeagent.gfx = ref.gfx  
+            obj.fifeagent.gfx = ref.gfx
              
         if obj.fifeagent.behaviour:
             obj.fifeagent.behaviour.parent = obj
@@ -656,6 +657,54 @@
         for object_info in database:
             self.object_db.update(object_info)
 
+    def updateObjectDB(self, world):
+        """Updates the values in the object database with the worlds values"""
+        
+        for entity in world.entities:
+            identifier = entity.general.identifier
+            agent_data = {}
+            map_id = self.game_state.getMapOfObject(identifier)
+            if not map_id:
+                continue
+            if self.agents[self.ALL_AGENTS_KEY].has_key(identifier):
+                agent_data = self.agents[self.ALL_AGENTS_KEY][identifier]
+            else:
+                agent_data = self.agents[map_id][identifier]
+            
+            entity_data = {}
+            entity_data["general"] = {"identifier": identifier}
+            for name, component in components.components.iteritems():
+                if hasattr(entity, name):
+                    comp_data = {}
+                    if name == "fifeagent":
+                        if agent_data["Entity"].has_key("fifeagent"):
+                            comp_data = agent_data["Entity"]["fifeagent"]
+                        if entity.fifeagent.layer:
+                            layer = entity.fifeagent.layer
+                            inst = layer.getInstance(identifier)
+                            loc = inst.getLocation().getExactLayerCoordinates()
+                            agent_data["Position"] = (loc.x, loc.y, loc.z)
+                            agent_data["Map"] = map_id
+                            agent_data["Rotation"]  = inst.getRotation()
+                    elif name == "container":
+                        #TODO: Save Inventory
+                        pass
+                    elif name == "equip":
+                        #TODO: Save Equipment
+                        pass                        
+                    else:
+                        comp_vals = getattr(entity, name)
+                        for field in component.fields:
+                            try:
+                                comp_data[field] = getattr(comp_vals, field)
+                            except AttributeError:                            
+                                #The entity doesn't have this specific value,
+                                #ignore it
+                                pass
+                    entity_data[name] = comp_data
+            agent_data["Entity"] = entity_data
+            
+        
     def getAgentImportFiles(self):
         """Searches the agents directory for import files """
         filepaths = locateFiles("*.xml", self.objects_directory)