Mercurial > fife-parpg
comparison demos/rpg/scripts/scene.py @ 531:801746c5bb9a
Added the spawn console command.
Introduced some exception handling when loading items and objects. Will have to do more of this as the project progresses.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Fri, 28 May 2010 21:47:59 +0000 |
parents | ea26e7b6f56c |
children | 65a92a2449d5 |
comparison
equal
deleted
inserted
replaced
530:ea26e7b6f56c | 531:801746c5bb9a |
---|---|
53 | 53 |
54 self._objectsettings = None | 54 self._objectsettings = None |
55 self._modelsettings = None | 55 self._modelsettings = None |
56 | 56 |
57 def loadItem(self, itemid): | 57 def loadItem(self, itemid): |
58 itemdict = self._objectsettings.get("items", itemid, {}) | 58 try: |
59 modeldict = self._modelsettings.get("models", itemdict["typename"], {}) | 59 itemdict = self._objectsettings.get("items", itemid, {}) |
60 | 60 modeldict = self._modelsettings.get("models", itemdict["typename"], {}) |
61 loadImportFile(modeldict["file"], self._gamecontroller.engine) | 61 |
62 | 62 loadImportFile(modeldict["file"], self._gamecontroller.engine) |
63 if itemdict["typename"] == "GoldStack": | 63 |
64 newitem = GoldStack(self._gamecontroller, modeldict["model"], itemid) | 64 if itemdict["typename"] == "GoldStack": |
65 newitem.value = itemdict["value"] | 65 newitem = GoldStack(self._gamecontroller, modeldict["model"], itemid) |
66 else: | 66 newitem.value = itemdict["value"] |
67 newitem = BaseItem(self._gamecontroller, modeldict["model"], itemid) | 67 else: |
68 | 68 newitem = BaseItem(self._gamecontroller, modeldict["model"], itemid) |
69 newitem.setMapPosition(float(itemdict["posx"]), float(itemdict["posy"])) | 69 |
70 newitem.setMapPosition(float(itemdict["posx"]), float(itemdict["posy"])) | |
71 | |
72 except (KeyError) as e: | |
73 print "Error: ", e | |
74 newitem = None | |
75 | |
76 return newitem | |
77 | |
78 def loadActor(self, actorid): | |
79 try: | |
80 objdict = self._objectsettings.get("npcs", actorid, {}) | |
81 modeldict = self._modelsettings.get("models", objdict["typename"], {}) | |
82 | |
83 loadImportFile(modeldict["file"], self._gamecontroller.engine) | |
70 | 84 |
71 return newitem | 85 if modeldict["type"] == "QUESTGIVER": |
72 | 86 actor = QuestGiver(self._gamecontroller, modeldict["model"], actorid, True) |
73 def loadActor(self, actorid): | 87 questcount = self._modelsettings.get(actorid, "questcount", 0) |
74 objdict = self._objectsettings.get("npcs", actorid, {}) | 88 for x in range(1,questcount+1): |
75 modeldict = self._modelsettings.get("models", objdict["typename"], {}) | 89 quest = "quest" + str(x) |
76 | 90 questdict = self._modelsettings.get(actorid, quest, {}) |
77 loadImportFile(modeldict["file"], self._gamecontroller.engine) | 91 quest = Quest(actor, questdict['name'], questdict['desc']) |
78 | |
79 if modeldict["type"] == "QUESTGIVER": | |
80 actor = QuestGiver(self._gamecontroller, modeldict["model"], actorid, True) | |
81 questcount = self._modelsettings.get(actorid, "questcount", 0) | |
82 for x in range(1,questcount+1): | |
83 quest = "quest" + str(x) | |
84 questdict = self._modelsettings.get(actorid, quest, {}) | |
85 quest = Quest(actor, questdict['name'], questdict['desc']) | |
86 | 92 |
87 for ritem in questdict['items'].split(" , "): | 93 for ritem in questdict['items'].split(" , "): |
88 if ritem == "GoldStack": | 94 if ritem == "GoldStack": |
89 quest.addRequiredGold(int(questdict['value'])) | 95 quest.addRequiredGold(int(questdict['value'])) |
90 else: | 96 else: |
91 quest.addRequiredItem(ritem) | 97 quest.addRequiredItem(ritem) |
92 | |
93 actor.addQuest(quest) | |
94 | 98 |
95 elif modeldict["type"] == "NPC": | 99 actor.addQuest(quest) |
96 actor = Actor(self._gamecontroller, modeldict["model"], npc, True) | 100 |
97 | 101 elif modeldict["type"] == "NPC": |
98 actor.setMapPosition(float(objdict["posx"]), float(objdict["posy"])) | 102 actor = Actor(self._gamecontroller, modeldict["model"], npc, True) |
103 | |
104 actor.setMapPosition(float(objdict["posx"]), float(objdict["posy"])) | |
105 | |
106 except (KeyError) as e: | |
107 print "Error: ", e | |
108 actor = None | |
99 | 109 |
100 return actor | 110 return actor |
101 | 111 |
102 def loadItems(self, mapfilename): | 112 def loadItems(self, mapfilename): |
103 for item in self._objectsettings.get("items", "itemlist", []): | 113 for item in self._objectsettings.get("items", "itemlist", []): |
104 newitem = self.loadItem(item) | 114 newitem = self.loadItem(item) |
105 self._objectlist[newitem.instance.getId()] = newitem | 115 self.addObjectToScene(newitem) |
106 | 116 |
107 def loadActors(self, mapfilename): | 117 def loadActors(self, mapfilename): |
108 for npc in self._objectsettings.get("npcs", "npclist", []): | 118 for npc in self._objectsettings.get("npcs", "npclist", []): |
109 actor = self.loadActor(npc) | 119 actor = self.loadActor(npc) |
110 self._objectlist[actor.instance.getId()] = actor | 120 self.addObjectToScene(actor) |
111 | 121 |
112 def loadPlayer(self): | 122 def loadPlayer(self): |
113 """ | 123 """ |
114 @todo: once we have all art assets this should be able to load one of 3 player models | 124 @todo: once we have all art assets this should be able to load one of 3 player models |
115 """ | 125 """ |
116 modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml") | 126 modelfile = self._gamecontroller.settings.get("RPG", "AllObjectFile", "maps/allobjects.xml") |
187 location = fife.Location(self._actorlayer) | 197 location = fife.Location(self._actorlayer) |
188 location.setMapCoordinates(target_mapcoord) | 198 location.setMapCoordinates(target_mapcoord) |
189 return location | 199 return location |
190 | 200 |
191 def addObjectToScene(self, obj): | 201 def addObjectToScene(self, obj): |
192 self._objectlist[obj.id] = obj | 202 if not self._objectlist.has_key(obj.id): |
203 self._objectlist[obj.id] = obj | |
193 | 204 |
194 def removeObjectFromScene(self, obj): | 205 def removeObjectFromScene(self, obj): |
195 obj.destroy() | 206 obj.destroy() |
196 del self._objectlist[obj.id] | 207 del self._objectlist[obj.id] |
197 | 208 |