Mercurial > fife-parpg
comparison demos/rpg/scripts/scene.py @ 546:8fee2d2286e9
Rewrote the object serializing routines to use a "template" idea for loading an object from disk. This allows for multiple objects to load the same base object template but be unique on the scene AND have different values. Useful for say more than one gold stack on the ground with different gold values. TODO: fix the "spawn" console command.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Thu, 03 Jun 2010 21:35:06 +0000 |
parents | cb7ec12214a9 |
children | e59ece21ab3e |
comparison
equal
deleted
inserted
replaced
545:bc7e3c3122d7 | 546:8fee2d2286e9 |
---|---|
22 # Free Software Foundation, Inc., | 22 # Free Software Foundation, Inc., |
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | 23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
24 # #################################################################### | 24 # #################################################################### |
25 # This is the rio de hola client for FIFE. | 25 # This is the rio de hola client for FIFE. |
26 | 26 |
27 import sys, os, re, math, random, shutil | 27 import sys, os, re, math, random, shutil, uuid |
28 | 28 |
29 from fife import fife | 29 from fife import fife |
30 from fife.extensions.loaders import loadMapFile | 30 from fife.extensions.loaders import loadMapFile |
31 from fife.extensions.loaders import loadImportFile | 31 from fife.extensions.loaders import loadImportFile |
32 from fife.extensions.fife_settings import Setting | 32 from fife.extensions.fife_settings import Setting |
53 self._player = None | 53 self._player = None |
54 self._objectlist = {} | 54 self._objectlist = {} |
55 | 55 |
56 self._objectsettings = None | 56 self._objectsettings = None |
57 self._modelsettings = None | 57 self._modelsettings = None |
58 | 58 self._questsettings = None |
59 def loadItem(self, itemid): | 59 |
60 def loadObject(self, objectname, objectid=None, valuedict=None): | |
61 if objectid: | |
62 identifier = objectid | |
63 else: | |
64 #identifier = uuid.uuid1() | |
65 identifier = "blkdjfkdj" | |
66 | |
60 try: | 67 try: |
61 itemdict = self._objectsettings.get("items", itemid, {}) | 68 objdict = self._modelsettings.get("objects", objectname, {}) |
62 modeldict = self._modelsettings.get("models", itemdict["typename"], {}) | 69 modeldict = self._modelsettings.get("models", objdict["modelname"], {}) |
63 print itemdict | 70 |
71 print objdict | |
64 print modeldict | 72 print modeldict |
73 | |
65 loadImportFile(modeldict["file"], self._gamecontroller.engine) | 74 loadImportFile(modeldict["file"], self._gamecontroller.engine) |
66 | 75 |
67 if modeldict["type"] == "GOLD": | 76 if objdict["type"] == "GOLD": |
68 newitem = GoldStack(self._gamecontroller, self.itemlayer, itemdict["typename"], modeldict["model"], itemid) | 77 newobject = GoldStack(self._gamecontroller, self.itemlayer, objdict["type"], objectname, modeldict["model"], identifier) |
69 newitem.value = itemdict["value"] | 78 elif objdict["type"] == "PORTAL": |
70 elif modeldict["type"] == "PORTAL": | 79 newobject = Portal(self._gamecontroller, self.itemlayer, objdict["type"], objectname, modeldict["model"], identifier) |
71 newitem = Portal(self._gamecontroller, self.itemlayer, itemdict["typename"], modeldict["model"], itemid) | 80 elif objdict["type"] == "QUESTGIVER": |
72 newitem.dest = itemdict["dest"] | 81 newobject = QuestGiver(self._gamecontroller, self.actorlayer, objdict["type"], objectname, modeldict["model"], identifier, True) |
82 | |
83 for quest in self._questsettings.get(identifier, "questlist", []): | |
84 questdict = self._questsettings.get(identifier, quest, {}) | |
85 | |
86 if questdict['type'] == "RETURN_ITEM": | |
87 questobj = ReturnItemQuest(newobject, questdict['name'], questdict['desc']) | |
88 for ritem in self._questsettings.get(quest+"_items", "itemlist", []): | |
89 itemdict = self._questsettings.get(quest+"_items", ritem, {}) | |
90 if itemdict["name"] == "GOLD_COINS": | |
91 questobj.addRequiredGold(int(itemdict['value'])) | |
92 else: | |
93 questobj.addRequiredItem(ritem) | |
94 else: | |
95 questobj = Quest(actor, questdict['name'], questdict['desc']) | |
96 | |
97 newobject.addQuest(questobj) | |
98 | |
99 elif objdict["type"] == "NPC": | |
100 newobject = Actor(self._gamecontroller, self.actorlayer, objdict["type"], objectname, modeldict["model"], identifier, True) | |
73 else: | 101 else: |
74 newitem = BaseItem(self._gamecontroller, self.itemlayer, itemdict["typename"], modeldict["model"], itemid) | 102 return None |
75 | 103 |
76 newitem.deserialize(self._objectsettings) | 104 if valuedict: |
77 | 105 newobject.deserialize(valuedict) |
106 else: | |
107 newobject.deserialize(objdict) | |
108 | |
78 except KeyError, e: | 109 except KeyError, e: |
79 raise ObjectNotFoundError | 110 raise ObjectNotFoundError |
80 | 111 |
81 return newitem | 112 return newobject |
82 | 113 |
83 def loadActor(self, actorid): | 114 def loadObjects(self, mapfilename): |
84 try: | 115 for obj in self._objectsettings.get("objects", "objectlist", []): |
85 objdict = self._objectsettings.get("npcs", actorid, {}) | 116 try: |
86 modeldict = self._modelsettings.get("models", objdict["typename"], {}) | 117 objdict = self._objectsettings.get("objects", obj, {}) |
87 | 118 newobj = self.loadObject(objdict["objectname"], obj, objdict) |
88 loadImportFile(modeldict["file"], self._gamecontroller.engine) | 119 self._gamecontroller.logger.log_debug("Loaded object: " + obj) |
120 except ObjectNotFoundError, e: | |
121 self._gamecontroller.logger.log_error("Error while loading object: " + obj) | |
122 continue | |
89 | 123 |
90 if modeldict["type"] == "QUESTGIVER": | |
91 actor = QuestGiver(self._gamecontroller, self.actorlayer, objdict["typename"], modeldict["model"], actorid, True) | |
92 questcount = self._modelsettings.get(actorid, "questcount", 0) | |
93 for x in range(1,questcount+1): | |
94 quest = "quest" + str(x) | |
95 questdict = self._modelsettings.get(actorid, quest, {}) | |
96 | |
97 if questdict['type'] == "RETURN_ITEM": | |
98 quest = ReturnItemQuest(actor, questdict['name'], questdict['desc']) | |
99 for ritem in questdict['items'].split(" , "): | |
100 if ritem == "GoldStack": | |
101 quest.addRequiredGold(int(questdict['value'])) | |
102 else: | |
103 quest.addRequiredItem(ritem) | |
104 else: | |
105 quest = Quest(actor, questdict['name'], questdict['desc']) | |
106 | |
107 actor.addQuest(quest) | |
108 | |
109 elif modeldict["type"] == "NPC": | |
110 actor = Actor(self._gamecontroller, self.actorlayer, objdict["typename"], modeldict["model"], npc, True) | |
111 | |
112 actor.setMapPosition(float(objdict["posx"]), float(objdict["posy"])) | |
113 | |
114 except KeyError, e: | |
115 raise ObjectNotFoundError | |
116 | |
117 return actor | |
118 | |
119 def loadItems(self, mapfilename): | |
120 for item in self._objectsettings.get("items", "itemlist", []): | |
121 try: | 124 try: |
122 newitem = self.loadItem(item) | 125 if newobj: |
123 self._gamecontroller.logger.log_debug("Loaded item: " + item) | 126 self.addObjectToScene(newobj) |
124 except ObjectNotFoundError, e: | 127 except ObjectAlreadyInSceneError, e: |
125 self._gamecontroller.logger.log_error("Error while loading item: " + item) | 128 self._gamecontroller.logger.log_error("Object already part of scene:" + obj) |
126 continue | 129 continue |
127 | 130 |
128 try: | |
129 self.addObjectToScene(newitem) | |
130 except ObjectAlreadyInSceneError, e: | |
131 self._gamecontroller.logger.log_error("Item already part of scene: " + newitem) | |
132 continue | |
133 | |
134 def loadActors(self, mapfilename): | |
135 for npc in self._objectsettings.get("npcs", "npclist", []): | |
136 try: | |
137 actor = self.loadActor(npc) | |
138 self._gamecontroller.logger.log_debug("Loaded actor: " + npc) | |
139 except ObjectNotFoundError, e: | |
140 self._gamecontroller.logger.log_error("Error while loading actor:" + npc) | |
141 continue | |
142 | |
143 try: | |
144 self.addObjectToScene(actor) | |
145 except ObjectAlreadyInSceneError, e: | |
146 self._gamecontroller.logger.log_error("Actor already part of scene:" + actor) | |
147 continue | |
148 | |
149 def createPlayerObject(self): | 131 def createPlayerObject(self): |
150 """ | 132 """ |
151 @todo: once we have all art assets this should be able to load one of 3 player models | 133 @todo: once we have all art assets this should be able to load one of 3 player models |
152 """ | 134 """ |
153 modeldict = self._modelsettings.get("models", "Player", {}) | 135 modeldict = self._modelsettings.get("models", "Player", {}) |
157 | 139 |
158 playerfilename = os.path.join("saves", "player_save.xml") | 140 playerfilename = os.path.join("saves", "player_save.xml") |
159 | 141 |
160 if os.path.isfile(playerfilename): | 142 if os.path.isfile(playerfilename): |
161 player_settings = Setting(settings_file=playerfilename, copy_dist=False) | 143 player_settings = Setting(settings_file=playerfilename, copy_dist=False) |
162 self._player.deserialize(player_settings) | 144 pvals = player_settings.get("player", "player", {}) |
145 self._player.deserialize(pvals) | |
163 | 146 |
164 def createScene(self, mapname): | 147 def createScene(self, mapname): |
165 mapfilename = os.path.join("maps", mapname + ".xml") | 148 mapfilename = os.path.join("maps", mapname + ".xml") |
166 | 149 |
167 if self._map: | 150 if self._map: |
175 objectfile = os.path.join("saves", mapname + "_save.xml") | 158 objectfile = os.path.join("saves", mapname + "_save.xml") |
176 else: | 159 else: |
177 objectfile = os.path.join("maps", mapname + "_objects.xml") | 160 objectfile = os.path.join("maps", mapname + "_objects.xml") |
178 | 161 |
179 modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml") | 162 modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml") |
180 | 163 questfile = self._gamecontroller.settings.get("RPG", "QuestFile", "maps/quests.xml") |
181 print objectfile | 164 |
182 self._objectsettings = Setting(app_name="",settings_file=objectfile) | 165 self._objectsettings = Setting(settings_file=objectfile) |
183 self._modelsettings = Setting(app_name="", settings_file=modelfile) | 166 self._modelsettings = Setting(settings_file=modelfile) |
167 self._questsettings = Setting(settings_file=questfile) | |
184 | 168 |
185 for cam in self._map.getCameras(): | 169 for cam in self._map.getCameras(): |
186 self._cameras[cam.getId()] = cam | 170 self._cameras[cam.getId()] = cam |
187 | 171 |
188 self._cameras[self._maincameraname].setZoom(self._gamecontroller.settings.get("RPG", "DefaultZoom", 1.0)) | 172 self._cameras[self._maincameraname].setZoom(self._gamecontroller.settings.get("RPG", "DefaultZoom", 1.0)) |
189 | 173 |
190 self._actorlayer = self._map.getLayer(self._gamecontroller.settings.get("RPG", "ActorLayer", "actor_layer")) | 174 self._actorlayer = self._map.getLayer(self._gamecontroller.settings.get("RPG", "ActorLayer", "actor_layer")) |
191 self._itemlayer = self._map.getLayer(self._gamecontroller.settings.get("RPG", "ItemLayer", "item_layer")) | 175 self._itemlayer = self._map.getLayer(self._gamecontroller.settings.get("RPG", "ItemLayer", "item_layer")) |
192 | 176 |
193 self.loadItems(mapfilename) | 177 #self.loadItems(mapfilename) |
194 self.loadActors(mapfilename) | 178 #self.loadActors(mapfilename) |
179 self.loadObjects(mapfilename) | |
195 | 180 |
196 #finally load the player | 181 #finally load the player |
197 self.createPlayerObject() | 182 self.createPlayerObject() |
198 | 183 |
199 def destroyScene(self): | 184 def destroyScene(self): |
222 self._actorlayer = None | 207 self._actorlayer = None |
223 self._itemlayer = None | 208 self._itemlayer = None |
224 | 209 |
225 self._objectsettings = None | 210 self._objectsettings = None |
226 self._modelsettings = None | 211 self._modelsettings = None |
212 self._questsettings = None | |
227 | 213 |
228 def getInstancesAt(self, clickpoint, layer): | 214 def getInstancesAt(self, clickpoint, layer): |
229 """ | 215 """ |
230 Query the main camera for instances on the specified layer. | 216 Query the main camera for instances on the specified layer. |
231 """ | 217 """ |
248 else: | 234 else: |
249 obj.destroy() | 235 obj.destroy() |
250 raise ObjectAlreadyInSceneError | 236 raise ObjectAlreadyInSceneError |
251 | 237 |
252 def getObject(self, objid): | 238 def getObject(self, objid): |
239 """ | |
240 @todo: throw ObjectNowFoundError | |
241 """ | |
253 if self._objectlist.has_key(objid): | 242 if self._objectlist.has_key(objid): |
254 return self._objectlist[objid] | 243 return self._objectlist[objid] |
255 else: | 244 else: |
256 return None | 245 return None |
257 | 246 |
260 del self._objectlist[obj.id] | 249 del self._objectlist[obj.id] |
261 | 250 |
262 def serialize(self): | 251 def serialize(self): |
263 filename = os.path.join("saves", self._mapname + "_save.xml") | 252 filename = os.path.join("saves", self._mapname + "_save.xml") |
264 playerfilename = os.path.join("saves", "player_save.xml") | 253 playerfilename = os.path.join("saves", "player_save.xml") |
265 settings = Setting(settings_file=filename, copy_dist=False) | 254 map_settings = Setting(settings_file=filename, copy_dist=False) |
266 player_settings = Setting(settings_file=playerfilename, copy_dist=False) | 255 player_settings = Setting(settings_file=playerfilename, copy_dist=False) |
267 | 256 |
268 itemlist = [] | 257 objectlist = [] |
269 npclist = [] | |
270 | 258 |
271 for obj in self._objectlist.values(): | 259 for obj in self._objectlist.values(): |
272 obj.serialize(settings) | 260 ovals = obj.serialize() |
273 module = getModuleByType(obj.type) | 261 map_settings.set("objects", obj.id, ovals) |
274 if module == "items": | 262 objectlist.append(obj.id) |
275 itemlist.append(obj.id) | 263 |
276 elif module == "npcs": | 264 map_settings.set("objects", "objectlist", objectlist) |
277 npclist.append(obj.id) | 265 |
278 | 266 pvals = self._player.serialize() |
279 settings.set("items", "itemlist", itemlist) | 267 player_settings.set("player", "player", pvals) |
280 settings.set("npcs", "npclist", npclist) | 268 |
281 | 269 map_settings.saveSettings() |
282 self._player.serialize(player_settings) | |
283 | |
284 settings.saveSettings() | |
285 player_settings.saveSettings() | 270 player_settings.saveSettings() |
286 | 271 |
287 def deserialize(self): | 272 def deserialize(self): |
288 pass | 273 if self._mapname: |
274 createScene(self._mapname) | |
289 | 275 |
290 def updateScene(self): | 276 def updateScene(self): |
291 pass | 277 pass |
292 | 278 |
293 def _getActorLayer(self): | 279 def _getActorLayer(self): |