comparison demos/rpg/scripts/scene.py @ 567:9152ed2b5bb8

Created SimpleXMLSerializer which makes loading and saving variables to an XML file simple. In the process I removed the the XML code from the Settings class. It now uses SimpleXMLSerializer to load and save settings. I have also updated the RPG demo to use the SimpleXMLSerializer for loading and saving game specific data.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 28 Jun 2010 18:41:23 +0000
parents f85762e634c5
children e3140f01749d
comparison
equal deleted inserted replaced
566:90d369c788c0 567:9152ed2b5bb8
26 import sys, os, re, math, random, shutil, uuid 26 import sys, os, re, math, random, shutil, uuid
27 27
28 from fife import fife 28 from fife import fife
29 from fife.extensions.loaders import loadMapFile 29 from fife.extensions.loaders import loadMapFile
30 from fife.extensions.loaders import loadImportFile 30 from fife.extensions.loaders import loadImportFile
31 from fife.extensions.fife_settings import Setting 31 from fife.extensions.serializers.simplexml import SimpleXMLSerializer
32 32
33 from scripts.actors.baseactor import Actor 33 from scripts.actors.baseactor import Actor
34 from scripts.actors.questgiver import QuestGiver 34 from scripts.actors.questgiver import QuestGiver
35 from scripts.quests.basequest import Quest, ReturnItemQuest, QuestTypes 35 from scripts.quests.basequest import Quest, ReturnItemQuest, QuestTypes
36 from scripts.actors.player import Player 36 from scripts.actors.player import Player
118 self._player = Player(self._gamecontroller, self.actorlayer, "warrior") 118 self._player = Player(self._gamecontroller, self.actorlayer, "warrior")
119 119
120 playerfilename = os.path.join("saves", "player_save.xml") 120 playerfilename = os.path.join("saves", "player_save.xml")
121 121
122 if os.path.isfile(playerfilename): 122 if os.path.isfile(playerfilename):
123 player_settings = Setting(settings_file=playerfilename, copy_dist=False) 123 player_settings = SimpleXMLSerializer(playerfilename)
124 pvals = player_settings.get("player", "player", {}) 124 pvals = player_settings.get("player", "player", {})
125 self._player.deserialize(pvals) 125 self._player.deserialize(pvals)
126 126
127 def createScene(self, mapname): 127 def createScene(self, mapname):
128 mapfilename = os.path.join("maps", mapname + ".xml") 128 mapfilename = os.path.join("maps", mapname + ".xml")
140 objectfile = os.path.join("maps", mapname + "_objects.xml") 140 objectfile = os.path.join("maps", mapname + "_objects.xml")
141 141
142 modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml") 142 modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml")
143 questfile = self._gamecontroller.settings.get("RPG", "QuestFile", "maps/quests.xml") 143 questfile = self._gamecontroller.settings.get("RPG", "QuestFile", "maps/quests.xml")
144 144
145 self._objectsettings = Setting(settings_file=objectfile) 145 self._objectsettings = SimpleXMLSerializer(objectfile)
146 self._modelsettings = Setting(settings_file=modelfile) 146 self._modelsettings = SimpleXMLSerializer(modelfile)
147 147
148 for cam in self._map.getCameras(): 148 for cam in self._map.getCameras():
149 self._cameras[cam.getId()] = cam 149 self._cameras[cam.getId()] = cam
150 150
151 self._cameras[self._maincameraname].setZoom(self._gamecontroller.settings.get("RPG", "DefaultZoom", 1.0)) 151 self._cameras[self._maincameraname].setZoom(self._gamecontroller.settings.get("RPG", "DefaultZoom", 1.0))
234 raise ObjectNotFoundError(obj.id + " could not be removed from the scene as it was not found in the scene.") 234 raise ObjectNotFoundError(obj.id + " could not be removed from the scene as it was not found in the scene.")
235 235
236 def serialize(self): 236 def serialize(self):
237 filename = os.path.join("saves", self._mapname + "_save.xml") 237 filename = os.path.join("saves", self._mapname + "_save.xml")
238 playerfilename = os.path.join("saves", "player_save.xml") 238 playerfilename = os.path.join("saves", "player_save.xml")
239 map_settings = Setting(settings_file=filename, copy_dist=False) 239 map_settings = SimpleXMLSerializer(filename)
240 player_settings = Setting(settings_file=playerfilename, copy_dist=False) 240 player_settings = SimpleXMLSerializer(playerfilename)
241 241
242 objectlist = [] 242 objectlist = []
243 243
244 for obj in self._objectlist.values(): 244 for obj in self._objectlist.values():
245 ovals = obj.serialize() 245 ovals = obj.serialize()
249 map_settings.set("objects", "objectlist", objectlist) 249 map_settings.set("objects", "objectlist", objectlist)
250 250
251 pvals = self._player.serialize() 251 pvals = self._player.serialize()
252 player_settings.set("player", "player", pvals) 252 player_settings.set("player", "player", pvals)
253 253
254 map_settings.saveSettings() 254 map_settings.save()
255 player_settings.saveSettings() 255 player_settings.save()
256 256
257 def deserialize(self): 257 def deserialize(self):
258 if self._mapname: 258 if self._mapname:
259 createScene(self._mapname) 259 createScene(self._mapname)
260 260