changeset 535:9fbe3dce925a

Added custom exceptions.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 31 May 2010 18:44:48 +0000
parents 65a92a2449d5
children 1afe46247ab1
files demos/rpg/scripts/actors/baseactor.py demos/rpg/scripts/gamecontroller.py demos/rpg/scripts/misc/__init__.py demos/rpg/scripts/misc/exceptions.py demos/rpg/scripts/scene.py
diffstat 4 files changed, 70 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/demos/rpg/scripts/actors/baseactor.py	Mon May 31 17:45:04 2010 +0000
+++ b/demos/rpg/scripts/actors/baseactor.py	Mon May 31 18:44:48 2010 +0000
@@ -34,7 +34,7 @@
 Actions = {'NONE':0,
 		   'PICKUP':1,
 		   'TALK':2,
-		   'HIT':3,
+		   'ATTACK':3,
 		   'OPEN':4,
 		   'ENTER':5}
 
--- a/demos/rpg/scripts/gamecontroller.py	Mon May 31 17:45:04 2010 +0000
+++ b/demos/rpg/scripts/gamecontroller.py	Mon May 31 18:44:48 2010 +0000
@@ -35,6 +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
 
 
 class KeyState(object):
@@ -210,20 +211,22 @@
 		if cmd[0] == "spawn":
 			result = "Usage: spawn [item|actor] [id] [posx] [posy]"
 			if len(cmd) != 5:
-				print len(cmd)
 				return result
 			else:
-				if cmd[1] == "item":
-					obj = self._scene.loadItem(cmd[2])
-				elif cmd[1] == "actor":
-					obj = self._scene.loadActor(cmd[2])
-				else:
-					return result
+				try:
+					if cmd[1] == "item":
+						obj = self._scene.loadItem(cmd[2])
+					elif cmd[1] == "actor":
+						obj = self._scene.loadActor(cmd[2])
+					else:
+						return result
+				except ObjectNotFoundError, e:
+					result = "Error while loading object: " + cmd[2]
+					obj = None
+					
 				if obj:
 					self._scene.addObjectToScene(obj)
 					result = "--OK--"
-				else:
-					result = "Error: Not Found!"
 			
 		return result
 		
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/rpg/scripts/misc/exceptions.py	Mon May 31 18:44:48 2010 +0000
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+
+# -*- coding: utf-8 -*-
+
+# ####################################################################
+#  Copyright (C) 2005-2010 by the FIFE team
+#  http://www.fifengine.net
+#  This file is part of FIFE.
+#
+#  FIFE is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU Lesser General Public
+#  License as published by the Free Software Foundation; either
+#  version 2.1 of the License, or (at your option) any later version.
+#
+#  This library is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#  Lesser General Public License for more details.
+#
+#  You should have received a copy of the GNU Lesser General Public
+#  License along with this library; if not, write to the
+#  Free Software Foundation, Inc.,
+#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+# ####################################################################
+# This is the rio de hola client for FIFE.
+
+
+class InvalidCommandError(Exception):
+	def __init__(self):
+		return
+		
+	def __str__(self):
+		print "","Command not found!"
+		
+class ObjectNotFoundError(Exception):
+	def __init__(self):
+		return
+		
+	def __str__(self):
+		print "","Object was not found!"
--- a/demos/rpg/scripts/scene.py	Mon May 31 17:45:04 2010 +0000
+++ b/demos/rpg/scripts/scene.py	Mon May 31 18:44:48 2010 +0000
@@ -37,6 +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
 
 class Scene(object):
 	def __init__(self, gamecontroller):
@@ -70,9 +71,8 @@
 			
 			newitem.setMapPosition(float(itemdict["posx"]), float(itemdict["posy"]))	
 			
-		except (KeyError) as e:
-			print "Error: ", e
-			newitem = None
+		except KeyError, e:
+			raise FileFormatError
 			
 		return newitem
 		
@@ -104,20 +104,29 @@
 
 			actor.setMapPosition(float(objdict["posx"]), float(objdict["posy"]))
 		
-		except (KeyError) as e:
-			print "Error: ", e
-			actor = None
+		except KeyError, e:
+			raise ObjectNotFoundError
 		
 		return actor
 
 	def loadItems(self, mapfilename):
 		for item in self._objectsettings.get("items", "itemlist", []):
-			newitem = self.loadItem(item)
+			try:
+				newitem = self.loadItem(item)
+			except ObjectNotFoundError, e:
+				print "Error while loading item:", item
+				continue
+				
 			self.addObjectToScene(newitem)
 						
 	def loadActors(self, mapfilename):
 		for npc in self._objectsettings.get("npcs", "npclist", []):
-			actor = self.loadActor(npc)
+			try:
+				actor = self.loadActor(npc)
+			except ObjectNotFoundError, e:
+				print "Error while loading actor:", actor
+				continue
+				
 			self.addObjectToScene(actor)
 			
 	def loadPlayer(self):