diff gamemodel.py @ 101:824f3068ef2a

Fixed Saving and Loading.
author KarstenBock@gmx.net
date Thu, 29 Sep 2011 18:09:56 +0200
parents 485d866a847f
children dc573ac08355
line wrap: on
line diff
--- a/gamemodel.py	Wed Sep 28 15:01:04 2011 +0200
+++ b/gamemodel.py	Thu Sep 29 18:09:56 2011 +0200
@@ -144,8 +144,7 @@
         @type object_id: str 
         @param new_map: ID of the new map, or None
         @type object_id: str """
-        game_object = self.game_state.getObjectById(object_id)
-        self.deleteObject(object_id)
+        game_object = self.deleteObject(object_id)
         self.game_state.addObject(object_id, new_map, game_object)
     
     def deleteObject(self, object_id):
@@ -153,7 +152,7 @@
         @param object_id: ID of the object
         @type object_id: str """
         del self.agents["All"][object_id]
-        self.game_state.deleteObject(object_id)
+        return self.game_state.deleteObject(object_id)
         
     def save(self, path, filename):
         """Writes the saver to a file.
@@ -166,39 +165,12 @@
         except(IOError):
             sys.stderr.write("Error: Can't create save game: " + fname + "\n")
             return
+
         save_state = {}
-        save_state["Agents"] = {}
-        for map_name in self.agents:
-            if map_name == self.ALL_AGENTS_KEY:
-                continue
-            agents_dict = {}
-            for agent in self.agents[map_name]:
-                agent_obj = self.game_state.getObjectById(agent, map_name)
-                agent_inst = self.game_state.maps[map_name].\
-                                    agent_layer.getInstance(agent)
-                agent_dict = self.agents[map_name][agent]
-                agent_dict.update(agent_obj.getStateForSaving())
-                agent_dict["Rotation"] = agent_inst.getRotation()
-                agents_dict[agent] = agent_dict
-            save_state["Agents"][map_name] = agents_dict
-        agents_dict = {}
-        for agent in self.agents["All"]:
-            map_name = self.agents["All"][agent]["Map"]
-            agent_dict = self.agents["All"][agent]
-            agent_obj = None
-            if agent == "PlayerCharacter":
-                agent_obj = self.game_state.getObjectById("PlayerCharacter").fifeagent
-            else:
-                agent_obj = self.game_state.getObjectById(agent, map_name)
-            if agent_obj:
-                agent_inst = self.game_state.maps[map_name].\
-                                    agent_layer.getInstance(agent)
-                agent_dict.update(agent_obj.getStateForSaving())
-                agent_dict["Rotation"] = agent_inst.getRotation()
-                agent_dict["MapName"] = map_name
-            agents_dict[agent] = agent_dict
-        save_state["Agents"]["All"] = agents_dict
+        save_state["Agents"] = self.agents
+        save_state["Items"] = self.items
         save_state["GameState"] = self.game_state.getStateForSaving()
+        
         yaml.dump(save_state, save_file)
         
         save_file.close()       
@@ -225,21 +197,9 @@
             for agent_name in maps[map_name]:
                 agent = {agent_name:maps[map_name][agent_name]}
                 self.addAgent(map_name, agent)
-                
-        # Load the current map
-        if self.game_state.current_map_name:
-            self.loadMap(self.game_state.current_map_name)         
-        load_file.close()
-        
-
-        # Recreate all the behaviours. These can't be saved because FIFE
-        # objects cannot be pickled
-        
-        self.placeAgents()
-        self.placePC()
+        self.items = save_state["Items"]               
       
-        # In most maps we'll create the PlayerCharacter Instance internally. 
-        # In these cases we need a target position
+        load_file.close()             
          
     def teleport(self, agent, position):
         """Called when a an agent is moved instantly to a new position.