Mercurial > parpg-core
comparison src/parpg/gamemodel.py @ 131:0ffebdca7ba3
Fixed Saving and Loading.
author | KarstenBock@gmx.net |
---|---|
date | Thu, 29 Sep 2011 18:09:56 +0200 |
parents | 9fcff924eb6f |
children | e28c13a4802a |
comparison
equal
deleted
inserted
replaced
130:9fcff924eb6f | 131:0ffebdca7ba3 |
---|---|
142 """Moves the object to a new map, or in a container | 142 """Moves the object to a new map, or in a container |
143 @param object_id: ID of the object | 143 @param object_id: ID of the object |
144 @type object_id: str | 144 @type object_id: str |
145 @param new_map: ID of the new map, or None | 145 @param new_map: ID of the new map, or None |
146 @type object_id: str """ | 146 @type object_id: str """ |
147 game_object = self.game_state.getObjectById(object_id) | 147 game_object = self.deleteObject(object_id) |
148 self.deleteObject(object_id) | |
149 self.game_state.addObject(object_id, new_map, game_object) | 148 self.game_state.addObject(object_id, new_map, game_object) |
150 | 149 |
151 def deleteObject(self, object_id): | 150 def deleteObject(self, object_id): |
152 """Removes an object from the game | 151 """Removes an object from the game |
153 @param object_id: ID of the object | 152 @param object_id: ID of the object |
154 @type object_id: str """ | 153 @type object_id: str """ |
155 del self.agents["All"][object_id] | 154 del self.agents["All"][object_id] |
156 self.game_state.deleteObject(object_id) | 155 return self.game_state.deleteObject(object_id) |
157 | 156 |
158 def save(self, path, filename): | 157 def save(self, path, filename): |
159 """Writes the saver to a file. | 158 """Writes the saver to a file. |
160 @type filename: string | 159 @type filename: string |
161 @param filename: the name of the file to write to | 160 @param filename: the name of the file to write to |
164 try: | 163 try: |
165 save_file = open(fname, 'w') | 164 save_file = open(fname, 'w') |
166 except(IOError): | 165 except(IOError): |
167 sys.stderr.write("Error: Can't create save game: " + fname + "\n") | 166 sys.stderr.write("Error: Can't create save game: " + fname + "\n") |
168 return | 167 return |
168 | |
169 save_state = {} | 169 save_state = {} |
170 save_state["Agents"] = {} | 170 save_state["Agents"] = self.agents |
171 for map_name in self.agents: | 171 save_state["Items"] = self.items |
172 if map_name == self.ALL_AGENTS_KEY: | |
173 continue | |
174 agents_dict = {} | |
175 for agent in self.agents[map_name]: | |
176 agent_obj = self.game_state.getObjectById(agent, map_name) | |
177 agent_inst = self.game_state.maps[map_name].\ | |
178 agent_layer.getInstance(agent) | |
179 agent_dict = self.agents[map_name][agent] | |
180 agent_dict.update(agent_obj.getStateForSaving()) | |
181 agent_dict["Rotation"] = agent_inst.getRotation() | |
182 agents_dict[agent] = agent_dict | |
183 save_state["Agents"][map_name] = agents_dict | |
184 agents_dict = {} | |
185 for agent in self.agents["All"]: | |
186 map_name = self.agents["All"][agent]["Map"] | |
187 agent_dict = self.agents["All"][agent] | |
188 agent_obj = None | |
189 if agent == "PlayerCharacter": | |
190 agent_obj = self.game_state.getObjectById("PlayerCharacter").fifeagent | |
191 else: | |
192 agent_obj = self.game_state.getObjectById(agent, map_name) | |
193 if agent_obj: | |
194 agent_inst = self.game_state.maps[map_name].\ | |
195 agent_layer.getInstance(agent) | |
196 agent_dict.update(agent_obj.getStateForSaving()) | |
197 agent_dict["Rotation"] = agent_inst.getRotation() | |
198 agent_dict["MapName"] = map_name | |
199 agents_dict[agent] = agent_dict | |
200 save_state["Agents"]["All"] = agents_dict | |
201 save_state["GameState"] = self.game_state.getStateForSaving() | 172 save_state["GameState"] = self.game_state.getStateForSaving() |
173 | |
202 yaml.dump(save_state, save_file) | 174 yaml.dump(save_state, save_file) |
203 | 175 |
204 save_file.close() | 176 save_file.close() |
205 | 177 |
206 def load(self, path, filename): | 178 def load(self, path, filename): |
223 maps = save_state["Agents"] | 195 maps = save_state["Agents"] |
224 for map_name in maps: | 196 for map_name in maps: |
225 for agent_name in maps[map_name]: | 197 for agent_name in maps[map_name]: |
226 agent = {agent_name:maps[map_name][agent_name]} | 198 agent = {agent_name:maps[map_name][agent_name]} |
227 self.addAgent(map_name, agent) | 199 self.addAgent(map_name, agent) |
228 | 200 self.items = save_state["Items"] |
229 # Load the current map | |
230 if self.game_state.current_map_name: | |
231 self.loadMap(self.game_state.current_map_name) | |
232 load_file.close() | |
233 | |
234 | |
235 # Recreate all the behaviours. These can't be saved because FIFE | |
236 # objects cannot be pickled | |
237 | |
238 self.placeAgents() | |
239 self.placePC() | |
240 | 201 |
241 # In most maps we'll create the PlayerCharacter Instance internally. | 202 load_file.close() |
242 # In these cases we need a target position | |
243 | 203 |
244 def teleport(self, agent, position): | 204 def teleport(self, agent, position): |
245 """Called when a an agent is moved instantly to a new position. | 205 """Called when a an agent is moved instantly to a new position. |
246 The setting of position may wan to be created as its own method down the road. | 206 The setting of position may wan to be created as its own method down the road. |
247 @type position: String Tuple | 207 @type position: String Tuple |