comparison demos/rpg/scripts/scene.py @ 536:1afe46247ab1

Some misc code cleanup. Started using the log manager instead of printing directly to console. Added ObjectAlreadyInSceneError exception.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 31 May 2010 20:25:46 +0000
parents 9fbe3dce925a
children 764510a6d2f9
comparison
equal deleted inserted replaced
535:9fbe3dce925a 536:1afe46247ab1
35 from scripts.actors.questgiver import QuestGiver 35 from scripts.actors.questgiver import QuestGiver
36 from scripts.quests.basequest import Quest 36 from scripts.quests.basequest import Quest
37 from scripts.actors.player import Player 37 from scripts.actors.player import Player
38 from scripts.objects.baseobject import GameObjectTypes 38 from scripts.objects.baseobject import GameObjectTypes
39 from scripts.objects.items import BaseItem, GoldStack 39 from scripts.objects.items import BaseItem, GoldStack
40 from scripts.misc.exceptions import ObjectNotFoundError 40 from scripts.misc.exceptions import ObjectNotFoundError, ObjectAlreadyInSceneError
41 41
42 class Scene(object): 42 class Scene(object):
43 def __init__(self, gamecontroller): 43 def __init__(self, gamecontroller):
44 self._gamecontroller = gamecontroller 44 self._gamecontroller = gamecontroller
45 45
70 newitem = BaseItem(self._gamecontroller, modeldict["model"], itemid) 70 newitem = BaseItem(self._gamecontroller, modeldict["model"], itemid)
71 71
72 newitem.setMapPosition(float(itemdict["posx"]), float(itemdict["posy"])) 72 newitem.setMapPosition(float(itemdict["posx"]), float(itemdict["posy"]))
73 73
74 except KeyError, e: 74 except KeyError, e:
75 raise FileFormatError 75 raise ObjectNotFoundError
76 76
77 return newitem 77 return newitem
78 78
79 def loadActor(self, actorid): 79 def loadActor(self, actorid):
80 try: 80 try:
112 def loadItems(self, mapfilename): 112 def loadItems(self, mapfilename):
113 for item in self._objectsettings.get("items", "itemlist", []): 113 for item in self._objectsettings.get("items", "itemlist", []):
114 try: 114 try:
115 newitem = self.loadItem(item) 115 newitem = self.loadItem(item)
116 except ObjectNotFoundError, e: 116 except ObjectNotFoundError, e:
117 print "Error while loading item:", item 117 self._gamecontroller.logger.log_error("Error while loading item: " + item)
118 continue 118 continue
119 119
120 self.addObjectToScene(newitem) 120 try:
121 self.addObjectToScene(newitem)
122 except ObjectAlreadyInSceneError, e:
123 self._gamecontroller.logger.log_error("Item already part of scene: " + newitem)
121 124
122 def loadActors(self, mapfilename): 125 def loadActors(self, mapfilename):
123 for npc in self._objectsettings.get("npcs", "npclist", []): 126 for npc in self._objectsettings.get("npcs", "npclist", []):
124 try: 127 try:
125 actor = self.loadActor(npc) 128 actor = self.loadActor(npc)
126 except ObjectNotFoundError, e: 129 except ObjectNotFoundError, e:
127 print "Error while loading actor:", actor 130 self._gamecontroller.logger.log_error("Error while loading actor:" + actor)
128 continue 131 continue
129 132
130 self.addObjectToScene(actor) 133 try:
134 self.addObjectToScene(actor)
135 except ObjectAlreadyInSceneError, e:
136 self._gamecontroller.logger.log_error("Actor already part of scene:" + actor)
131 137
132 def loadPlayer(self): 138 def loadPlayer(self):
133 """ 139 """
134 @todo: once we have all art assets this should be able to load one of 3 player models 140 @todo: once we have all art assets this should be able to load one of 3 player models
135 """ 141 """
209 return location 215 return location
210 216
211 def addObjectToScene(self, obj): 217 def addObjectToScene(self, obj):
212 if not self._objectlist.has_key(obj.id): 218 if not self._objectlist.has_key(obj.id):
213 self._objectlist[obj.id] = obj 219 self._objectlist[obj.id] = obj
220 else:
221 obj.destroy()
222 raise ObjectAlreadyInSceneError
214 223
215 def removeObjectFromScene(self, obj): 224 def removeObjectFromScene(self, obj):
216 obj.destroy() 225 obj.destroy()
217 del self._objectlist[obj.id] 226 del self._objectlist[obj.id]
218 227