changeset 490:939a4dc12ca1

Starting to add some comments. Cleaned up some old commented out code. The SoundManager now creates a new SoundClip rather than only creating one per FIFE SoundEmitter.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 05 May 2010 21:39:31 +0000
parents 0324a3533988
children c4168eb47a44
files demos/shooter/run.py demos/shooter/scripts/soundmanager.py demos/shooter/scripts/weapons.py demos/shooter/scripts/world.py
diffstat 4 files changed, 58 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/demos/shooter/run.py	Wed May 05 14:29:37 2010 +0000
+++ b/demos/shooter/run.py	Wed May 05 21:39:31 2010 +0000
@@ -55,7 +55,6 @@
 		keystr = evt.getKey().getAsString().lower()
 		consumed = False
 		if keyval == fife.Key.ESCAPE:
-			#self._quit = True
 			self._world.showMainMenu()
 			evt.consume()
 
@@ -68,6 +67,8 @@
 	def __init__(self):
 		super(Shooter,self).__init__()
 		pychan.init(self.engine, debug=False)
+		
+		#This is requred if you want to use modal dialog boxes
 		pychan.setupModalExecution(self.mainLoop,self.breakFromMainLoop)
 		
 		self._world = world.World(self, self.engine)
@@ -82,7 +83,9 @@
 	def loadSettings(self):
 		"""
 		Load the settings from a python file and load them into the engine.
-		Called in the ApplicationBase constructor.
+		Called in the ApplicationBase constructor.  I hard coded all the
+		settings in here to remove the complexity of loading settings from
+		an XML file which would be out of scope of this demo.
 		"""
 
 		engineSetting = self.engine.getSettings()
@@ -108,8 +111,7 @@
 		"""
 		Initialize the LogManager.
 		"""
-		#LogModules = list()
-		#LogModules.append("controller")
+
 		LogModules = ["controller",]
 		self._log = fifelog.LogManager(self.engine, 1, 0)
 		if LogModules:
--- a/demos/shooter/scripts/soundmanager.py	Wed May 05 14:29:37 2010 +0000
+++ b/demos/shooter/scripts/soundmanager.py	Wed May 05 21:39:31 2010 +0000
@@ -27,6 +27,13 @@
 from fife.extensions.pychan.tools import callbackWithArguments as cbwa
 
 class SoundClip(object):
+	"""
+	SoundClip
+
+	This class stores an instance of a FIFE SoundEmitter class along 
+	with some information about a sound clip (like gain and if its
+	looping).  All instances of SoundClip should be created by SoundManager.
+	"""
 	def __init__(self, soundmanager, clipid, soundname, emitter):
 		self._soundmanager = soundmanager
 		self._name = soundname
@@ -93,29 +100,40 @@
 	duration = property(_getDuration, _setDuration)
 
 class SoundManager(object):
+	"""
+	SoundManger
+
+	This class manages and plays all the sounds of the game.  
+	It creates SoundClips and ensures that there is only one 
+	FIFE SoundEmitter per unique sound.
+	"""
 	def __init__(self, engine):
 		self._engine = engine
 		
 		self._fifesoundmanager = self._engine.getSoundManager()
 		self._fifesoundmanager.init()
 		
-		self._emitters = []
 		self._loadedclips = {}
 		
 	def loadSoundClip(self, filename):
+		"""
+		Returns a valid SoundClip instance.
+		"""
 		if not self._loadedclips.has_key(filename):
 			clipid = self._engine.getSoundClipPool().addResourceFromFile(filename)
 			fifeemitter = self._fifesoundmanager.createEmitter()
 			fifeemitter.thisown = 0
 			fifeemitter.setSoundClip(clipid)
 			
-			time = fifeemitter.getDuration()
-				
-			self._loadedclips[filename] = SoundClip(self, clipid, filename, fifeemitter)
-			self._loadedclips[filename].duration = time
-			self._emitters.append(fifeemitter)
+			self._loadedclips[filename] = fifeemitter
+			clip = SoundClip(self, clipid, filename, fifeemitter)
+			clip.duration = fifeemitter.getDuration()
+		else:
+			fifeemitter = self._loadedclips[filename]
+			clip = SoundClip(self, fifeemitter.getID(), filename, fifeemitter)
+			clip.duration = fifeemitter.getDuration()
 		
-		return self._loadedclips[filename]
+		return clip
 		
 	def playClip(self, clip):
 		if clip.fifeemitter:
@@ -126,12 +144,13 @@
 					
 				if clip.looping:
 					repeat = 0
-					def real_callback(c, e):
+					def real_callback(c, e, g):
 						c()
 						e.stop()
+						e.setGain(g)
 						e.play()
 
-					clip.callback = cbwa(real_callback, clip.callback, clip.fifeemitter)
+					clip.callback = cbwa(real_callback, clip.callback, clip.fifeemitter, clip.gain)
 
 				else:
 					repeat = 1
@@ -140,7 +159,6 @@
 				clip.timer.start()
 				
 			clip.fifeemitter.setGain(clip.gain)
-			#clip.fifeemitter.setLooping(clip.looping)
 			clip.fifeemitter.play()	
 		else:
 			clip = self.loadSoundClip(clip.name)
@@ -155,19 +173,14 @@
 			clip.timer = None
 
 	def stopAllSounds(self):
-		for clip in self._loadedclips.values():	
-			self.stopClip(clip)
+		for emitter in self._loadedclips.values():	
+			emitter.stop()
 			
 	def destroy(self):
 		self.stopAllSounds()
 	
-		for emitter in self._emitters[:]:
+		for emitter in self._loadedclips.values():
 			self._fifesoundmanager.releaseEmitter(emitter.getID())
-			self._emitters.remove(emitter)
+			emitter = None
 		
-		for clip in self._loadedclips.values():
-			clip.fifeemitter = None
-		
-			
-		self._emitters = list()
 		self._loadedclips.clear()
--- a/demos/shooter/scripts/weapons.py	Wed May 05 14:29:37 2010 +0000
+++ b/demos/shooter/scripts/weapons.py	Wed May 05 21:39:31 2010 +0000
@@ -27,6 +27,13 @@
 from scripts.soundmanager import *
 
 class Projectile(SpaceObject):
+	"""
+	Projectile
+
+	This is the entity that weapons fire.  Projectiles have an owner
+	and a time to live.  They are also what cause damage to ships
+	and other entities.
+	"""
 	def __init__(self, scene, owner, projectileName, timeToLive):
 		super(Projectile, self).__init__(scene, projectileName, False)
 
@@ -85,6 +92,14 @@
 	damage = property(_getDamage, _setDamage)
 	
 class Weapon(object):
+	"""
+	Weapon
+	
+	This class is a super class and is meant to be inherited and
+	not used directly.  You should implement fire() in the sub-
+	class.  The Weapon class spawns Projectile(s) and fires them
+	in the specified direction at a specified fire rate.
+	"""
 	def __init__(self, scene, ship, firerate):
 		self._scene = scene
 		self._model = self._scene.model
--- a/demos/shooter/scripts/world.py	Wed May 05 14:29:37 2010 +0000
+++ b/demos/shooter/scripts/world.py	Wed May 05 21:39:31 2010 +0000
@@ -50,7 +50,8 @@
 
 	"""
 	def __init__(self, app, engine):
-		super(World, self).__init__(engine, regMouse=True, regKeys=True)
+		super(World, self).__init__(engine, regKeys=True)
+
 		self._applictaion = app
 		self._engine = engine
 		self._timemanager = engine.getTimeManager()
@@ -251,7 +252,8 @@
 		for cam in self._map.getCameras():
 			if cam.getId() == 'main':
 				self.cameras['main'] = cam
-				
+		
+		#pass the camera to the scene as the scene controls the cameras position
 		self._scene.attachCamera(self.cameras['main'])
 		
 	def resetKeys(self):
@@ -297,23 +299,6 @@
 		elif keyval in (fife.Key.LEFT_CONTROL, fife.Key.RIGHT_CONTROL):
 			self._keystate['CTRL'] = False
 
-	def mouseWheelMovedUp(self, evt):
-		if self._keystate['CTRL']:
-			self.cameras['main'].setZoom(self.cameras['main'].getZoom() * 1.05)
-
-	def mouseWheelMovedDown(self, evt):
-		if self._keystate['CTRL']:
-			self.cameras['main'].setZoom(self.cameras['main'].getZoom() / 1.05)
-
-	def mousePressed(self, evt):
-		if evt.isConsumedByWidgets():
-			return
-
-		clickpoint = fife.ScreenPoint(evt.getX(), evt.getY())
-
-	def mouseMoved(self, evt):
-		pt = fife.ScreenPoint(evt.getX(), evt.getY())
-
 	def pump(self):
 		"""
 		Called every frame.
@@ -322,7 +307,7 @@
 		if self._genericrenderer:
 			self._genericrenderer.removeAll("quads")
 
-		
+		#scene hasn't been initialized.  Nothing to do.
 		if not self._scene:
 			return
 		
@@ -333,7 +318,6 @@
 				
 			self._scene.update(self._timemanager.getTime() - self._starttime, self._keystate)
 		
-		
 			#update the HUD
 			avgframe = self._timemanager.getAverageFrameTime()
 			if avgframe > 0: