changeset 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
files demos/rpg/scripts/actors/baseactor.py demos/rpg/scripts/gamecontroller.py demos/rpg/scripts/guicontroller.py demos/rpg/scripts/misc/exceptions.py demos/rpg/scripts/rpg.py demos/rpg/scripts/scene.py
diffstat 6 files changed, 51 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/demos/rpg/scripts/actors/baseactor.py	Mon May 31 18:44:48 2010 +0000
+++ b/demos/rpg/scripts/actors/baseactor.py	Mon May 31 20:25:46 2010 +0000
@@ -52,8 +52,6 @@
 		self._dest = destobj
 		
 	def execute(self):
-		print "talking to: " + self._dest.instance.getId()
-		
 		if self._dest.type == GameObjectTypes["QUESTGIVER"]:
 			if self._dest.haveQuest():
 				if not self._dest.activequest:
--- a/demos/rpg/scripts/gamecontroller.py	Mon May 31 18:44:48 2010 +0000
+++ b/demos/rpg/scripts/gamecontroller.py	Mon May 31 20:25:46 2010 +0000
@@ -35,7 +35,7 @@
 from scripts.guicontroller import GUIController
 from scripts.actors.baseactor import TalkAction, PickUpItemAction
 from scripts.objects.baseobject import GameObjectTypes
-from scripts.misc.exceptions import ObjectNotFoundError
+from scripts.misc.exceptions import ObjectNotFoundError, ObjectAlreadyInSceneError
 
 
 class KeyState(object):
@@ -95,6 +95,9 @@
 			actor_instances = self._gamecontroller.scene.getInstancesAt(clickpoint, self._gamecontroller.scene.actorlayer)
 			item_instances = self._gamecontroller.scene.getInstancesAt(clickpoint, self._gamecontroller.scene.itemlayer)
 			if actor_instances:
+				if actor_instances[0].getId() == "player":
+					return
+					
 				obj = self._gamecontroller.scene.objectlist[actor_instances[0].getId()]
 				if obj.type == GameObjectTypes["QUESTGIVER"]:
 					action = TalkAction(self._gamecontroller.scene.player, obj)
@@ -108,11 +111,10 @@
 					
 
 		if (event.getButton() == fife.MouseEvent.RIGHT):
-			instances = self._gamecontroller.scene.getInstancesAt(clickpoint)
-			print "selected instances on actor layer: ", [i.getId() for i in instances]
+			instances = self._gamecontroller.scene.getInstancesAt(clickpoint, self._gamecontroller.scene.actorlayer)
 			if instances:
-				#do something
-				pass
+				self._gamecontroller.logger.log_debug("Selected instance on actor layer: " + instances[0].getId())
+
 				
 	def mouseReleased(self, event):
 		pass
@@ -128,10 +130,11 @@
 		actor_instances = self._gamecontroller.scene.getInstancesAt(pt, self._gamecontroller.scene.actorlayer)
 		item_instances = self._gamecontroller.scene.getInstancesAt(pt, self._gamecontroller.scene.itemlayer)
 		for i in actor_instances:
-			renderer.addOutlined(i, 173, 255, 47, 2)
+			if i.getId() != "player":
+				renderer.addOutlined(i, 173, 255, 47, 2)
 			
 		for j in item_instances:
-			renderer.addOutlined(j, 173, 255, 47, 2)
+			renderer.addOutlined(j, 255, 173, 47, 2)
 
 	def mouseEntered(self, event):
 		pass
@@ -221,12 +224,15 @@
 					else:
 						return result
 				except ObjectNotFoundError, e:
-					result = "Error while loading object: " + cmd[2]
+					result = "Error: Cannot load [" + cmd[2] + "].  It could not be found!"
 					obj = None
 					
 				if obj:
-					self._scene.addObjectToScene(obj)
-					result = "--OK--"
+					try:
+						self._scene.addObjectToScene(obj)
+						result = "--OK--"
+					except ObjectAlreadyInSceneError, e:
+						result = "Error: [" + cmd[2] + "] is already on the scene."
 			
 		return result
 		
@@ -282,6 +288,9 @@
 	
 	def _getInstanceRenderer(self):
 		return self._instancerenderer
+		
+	def _getLogManager(self):
+		return self._application.logger
 	
 	guicontroller = property(_getGUIController) 
 	engine = property(_getEngine)
@@ -289,3 +298,4 @@
 	scene = property(_getScene)
 	keystate = property(_getKeyState)
 	instancerenderer = property(_getInstanceRenderer)
+	logger = property(_getLogManager)
--- a/demos/rpg/scripts/guicontroller.py	Mon May 31 18:44:48 2010 +0000
+++ b/demos/rpg/scripts/guicontroller.py	Mon May 31 20:25:46 2010 +0000
@@ -95,7 +95,7 @@
 		self._widget.mapEvents(eventMap)
 		
 	def questAccepted(self):
-		print "quest has been accepted"
+		self._guicontroller._gamecontroller.logger.log_debug("Quest [" + self._quest.name + "] has been accepted")
 		self._questgiver.activateQuest(self._quest)
 		self._widget.hide()
 	
--- a/demos/rpg/scripts/misc/exceptions.py	Mon May 31 18:44:48 2010 +0000
+++ b/demos/rpg/scripts/misc/exceptions.py	Mon May 31 20:25:46 2010 +0000
@@ -38,3 +38,10 @@
 		
 	def __str__(self):
 		print "","Object was not found!"
+		
+class ObjectAlreadyInSceneError(Exception):
+	def __init__(self):
+		return
+		
+	def __str__(self):
+		print "","Object was already part of the scene!"
--- a/demos/rpg/scripts/rpg.py	Mon May 31 18:44:48 2010 +0000
+++ b/demos/rpg/scripts/rpg.py	Mon May 31 20:25:46 2010 +0000
@@ -152,3 +152,9 @@
 			self._gamecontroller.endGame()
 		else:
 			self._gamecontroller.pump()
+			
+			
+	def _getLogManager(self):
+		return self._log
+		
+	logger = property(_getLogManager)
--- 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()