changeset 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 4da56756a617
files demos/rpg/scripts/gamecontroller.py demos/rpg/scripts/scene.py
diffstat 2 files changed, 68 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/demos/rpg/scripts/gamecontroller.py	Fri May 28 20:10:33 2010 +0000
+++ b/demos/rpg/scripts/gamecontroller.py	Fri May 28 21:47:59 2010 +0000
@@ -201,14 +201,30 @@
 		result = ""
 		
 		args = command.split(" ")
+		cmd = []
 		for arg in args:
 			arg = arg.strip()
+			if arg != "":
+				cmd.append(arg)
+		
 	
-		if args[0] == "spawn":
-			if len(args) != 4:
-				result = "Usage: spawn [itemid] [posx] [posy]"
+		if cmd[0] == "spawn":
+			result = "Usage: spawn [item|actor] [id] [posx] [posy]"
+			if len(cmd) != 5:
+				print len(cmd)
+				return result
 			else:
-				result = "Success!"
+				if cmd[1] == "item":
+					obj = self._scene.loadItem(cmd[2])
+				elif cmd[1] == "actor":
+					obj = self._scene.loadActor(cmd[2])
+				else:
+					return result
+				if obj:
+					self._scene.addObjectToScene(obj)
+					result = "Success!"
+				else:
+					result = "Error: Not Found!"
 			
 		return result
 		
--- a/demos/rpg/scripts/scene.py	Fri May 28 20:10:33 2010 +0000
+++ b/demos/rpg/scripts/scene.py	Fri May 28 21:47:59 2010 +0000
@@ -55,60 +55,70 @@
 		self._modelsettings = None
 
 	def loadItem(self, itemid):
-		itemdict = self._objectsettings.get("items", itemid, {})
-		modeldict = self._modelsettings.get("models", itemdict["typename"], {})
-			
-		loadImportFile(modeldict["file"], self._gamecontroller.engine)
+		try:
+			itemdict = self._objectsettings.get("items", itemid, {})
+			modeldict = self._modelsettings.get("models", itemdict["typename"], {})	
+		
+			loadImportFile(modeldict["file"], self._gamecontroller.engine)
 		
-		if itemdict["typename"] == "GoldStack":
-			newitem = GoldStack(self._gamecontroller, modeldict["model"], itemid)
-			newitem.value = itemdict["value"]
-		else:
-			newitem = BaseItem(self._gamecontroller, modeldict["model"], itemid)
+			if itemdict["typename"] == "GoldStack":
+				newitem = GoldStack(self._gamecontroller, modeldict["model"], itemid)
+				newitem.value = itemdict["value"]
+			else:
+				newitem = BaseItem(self._gamecontroller, modeldict["model"], itemid)
 			
-		newitem.setMapPosition(float(itemdict["posx"]), float(itemdict["posy"]))	
-				
+			newitem.setMapPosition(float(itemdict["posx"]), float(itemdict["posy"]))	
+			
+		except (KeyError) as e:
+			print "Error: ", e
+			newitem = None
+			
 		return newitem
 		
 	def loadActor(self, actorid):
-		objdict = self._objectsettings.get("npcs", actorid, {})
-		modeldict = self._modelsettings.get("models", objdict["typename"], {})
-			
-		loadImportFile(modeldict["file"], self._gamecontroller.engine)
+		try:
+			objdict = self._objectsettings.get("npcs", actorid, {})
+			modeldict = self._modelsettings.get("models", objdict["typename"], {})
 			
-		if modeldict["type"] == "QUESTGIVER":
-			actor = QuestGiver(self._gamecontroller, modeldict["model"], actorid, True)
-			questcount = self._modelsettings.get(actorid, "questcount", 0)
-			for x in range(1,questcount+1):
-				quest = "quest" + str(x)
-				questdict = self._modelsettings.get(actorid, quest, {})
-				quest = Quest(actor, questdict['name'], questdict['desc'])
+			loadImportFile(modeldict["file"], self._gamecontroller.engine)
+				
+			if modeldict["type"] == "QUESTGIVER":
+				actor = QuestGiver(self._gamecontroller, modeldict["model"], actorid, True)
+				questcount = self._modelsettings.get(actorid, "questcount", 0)
+				for x in range(1,questcount+1):
+					quest = "quest" + str(x)
+					questdict = self._modelsettings.get(actorid, quest, {})
+					quest = Quest(actor, questdict['name'], questdict['desc'])
 					
-				for ritem in questdict['items'].split(" , "):
-					if ritem == "GoldStack":
-						quest.addRequiredGold(int(questdict['value']))
-					else:
-						quest.addRequiredItem(ritem)
-					
-				actor.addQuest(quest)
+					for ritem in questdict['items'].split(" , "):
+						if ritem == "GoldStack":
+							quest.addRequiredGold(int(questdict['value']))
+						else:
+							quest.addRequiredItem(ritem)
+						
+					actor.addQuest(quest)
 						
-		elif modeldict["type"] == "NPC":
-			actor = Actor(self._gamecontroller, modeldict["model"], npc, True)
+			elif modeldict["type"] == "NPC":
+				actor = Actor(self._gamecontroller, modeldict["model"], npc, True)
 
-		actor.setMapPosition(float(objdict["posx"]), float(objdict["posy"]))
+			actor.setMapPosition(float(objdict["posx"]), float(objdict["posy"]))
+		
+		except (KeyError) as e:
+			print "Error: ", e
+			actor = None
 		
 		return actor
 
 	def loadItems(self, mapfilename):
 		for item in self._objectsettings.get("items", "itemlist", []):
 			newitem = self.loadItem(item)
-			self._objectlist[newitem.instance.getId()] = newitem
-			
+			self.addObjectToScene(newitem)
+						
 	def loadActors(self, mapfilename):
 		for npc in self._objectsettings.get("npcs", "npclist", []):
 			actor = self.loadActor(npc)
-			self._objectlist[actor.instance.getId()] = actor		
-		
+			self.addObjectToScene(actor)
+			
 	def loadPlayer(self):
 		"""
 		@todo: once we have all art assets this should be able to load one of 3 player models
@@ -189,7 +199,8 @@
 		return location
 	
 	def addObjectToScene(self, obj):
-		self._objectlist[obj.id] = obj
+		if not self._objectlist.has_key(obj.id):
+			self._objectlist[obj.id] = obj
 	
 	def removeObjectFromScene(self, obj):
 		obj.destroy()