diff 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
line wrap: on
line diff
--- a/demos/rpg/scripts/scene.py	Mon May 31 18:44:48 2010 +0000
+++ b/demos/rpg/scripts/scene.py	Mon May 31 20:25:46 2010 +0000
@@ -37,7 +37,7 @@
 from scripts.actors.player import Player
 from scripts.objects.baseobject import GameObjectTypes
 from scripts.objects.items import BaseItem, GoldStack
-from scripts.misc.exceptions import ObjectNotFoundError
+from scripts.misc.exceptions import ObjectNotFoundError, ObjectAlreadyInSceneError
 
 class Scene(object):
 	def __init__(self, gamecontroller):
@@ -72,7 +72,7 @@
 			newitem.setMapPosition(float(itemdict["posx"]), float(itemdict["posy"]))	
 			
 		except KeyError, e:
-			raise FileFormatError
+			raise ObjectNotFoundError
 			
 		return newitem
 		
@@ -114,20 +114,26 @@
 			try:
 				newitem = self.loadItem(item)
 			except ObjectNotFoundError, e:
-				print "Error while loading item:", item
+				self._gamecontroller.logger.log_error("Error while loading item: " + item)
 				continue
-				
-			self.addObjectToScene(newitem)
+			
+			try:
+				self.addObjectToScene(newitem)
+			except ObjectAlreadyInSceneError, e:
+				self._gamecontroller.logger.log_error("Item already part of scene: " + newitem)
 						
 	def loadActors(self, mapfilename):
 		for npc in self._objectsettings.get("npcs", "npclist", []):
 			try:
 				actor = self.loadActor(npc)
 			except ObjectNotFoundError, e:
-				print "Error while loading actor:", actor
+				self._gamecontroller.logger.log_error("Error while loading actor:" + actor)
 				continue
-				
-			self.addObjectToScene(actor)
+			
+			try:
+				self.addObjectToScene(actor)
+			except ObjectAlreadyInSceneError, e:
+				self._gamecontroller.logger.log_error("Actor already part of scene:" + actor)
 			
 	def loadPlayer(self):
 		"""
@@ -211,6 +217,9 @@
 	def addObjectToScene(self, obj):
 		if not self._objectlist.has_key(obj.id):
 			self._objectlist[obj.id] = obj
+		else:
+			obj.destroy()
+			raise ObjectAlreadyInSceneError
 	
 	def removeObjectFromScene(self, obj):
 		obj.destroy()