changeset 480:85fa5fa486a8

Added a simple sound manager. Added music to the level.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 28 Apr 2010 17:28:29 +0000
parents ab28994820dd
children 1f37adc9a685
files demos/shooter/music/waynesmind2.ogg demos/shooter/scripts/scene.py demos/shooter/scripts/soundmanager.py demos/shooter/scripts/world.py
diffstat 4 files changed, 74 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
Binary file demos/shooter/music/waynesmind2.ogg has changed
--- a/demos/shooter/scripts/scene.py	Fri Apr 23 17:17:02 2010 +0000
+++ b/demos/shooter/scripts/scene.py	Wed Apr 28 17:28:29 2010 +0000
@@ -44,11 +44,12 @@
 	spaceobjects = property(_getObjects, _setObjects)
 
 class Scene(object):
-	def __init__(self, world, engine, objectLayer):
+	def __init__(self, world, engine, objectLayer, soundmanager):
 		self._engine = engine
 		self._world = world
 		self._model = engine.getModel()
 		self._layer = objectLayer
+		self._soundmanager = soundmanager
 		self._nodes = list()
 		
 		self._player = None
@@ -174,6 +175,8 @@
 		#and finally add the player to the scene
 		self.addObjectToScene(self._player)
 		
+		self._soundmanager.playSound("music/waynesmind2.ogg")
+		
 		self.startCamera()
 		
 	def pause(self, time):
@@ -189,9 +192,11 @@
 		
 	def gameOver(self):
 		self._gameover = True
+		self._soundmanager.stopSound("music/waynesmind2.ogg")		
 		self._world.gameOver()
 		
 	def endLevel(self):
+		self._soundmanager.stopSound("music/waynesmind2.ogg")
 		self._world.endLevel()
 		
 	def queueObjectForRemoval(self, obj):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/scripts/soundmanager.py	Wed Apr 28 17:28:29 2010 +0000
@@ -0,0 +1,63 @@
+# -*- coding: utf-8 -*-
+
+# ####################################################################
+#  Copyright (C) 2005-2009 by the FIFE team
+#  http://www.fifengine.de
+#  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
+# ####################################################################
+
+from fife import fife
+
+
+class SoundManager(object):
+	def __init__(self, engine):
+		self._engine = engine
+		
+		self._soundmanager = self._engine.getSoundManager()
+		self._soundmanager.init()
+		
+		self._emitters = {}
+
+	def playSound(self, soundname, volume=255):
+		if soundname in self._emitters:
+			self._emitters[soundname].stop()
+			self._emitters[soundname].play()
+		else:
+			#create an emitter for this sound
+			emitter = self._soundmanager.createEmitter()
+			clipid = self._engine.getSoundClipPool().addResourceFromFile(soundname)
+			emitter.setSoundClip(clipid)
+			emitter.setLooping(False)
+			
+			self._emitters[soundname] = emitter
+			self._emitters[soundname].thisown = 0
+			
+			self._emitters[soundname].play()
+			
+	def stopSound(self, soundname):
+		if soundname in self._emitters:
+			self._emitters[soundname].stop()
+			
+	def destroy(self):
+		todelete = {}
+		for emitter in self._emitters:
+			self._emitters[emitter].stop()
+			clipid = self._emitters[emitter].getID()
+			self._soundmanager.releaseEmitter(clipid)
+
+		self._emitters.clear()
\ No newline at end of file
--- a/demos/shooter/scripts/world.py	Fri Apr 23 17:17:02 2010 +0000
+++ b/demos/shooter/scripts/world.py	Wed Apr 28 17:28:29 2010 +0000
@@ -36,6 +36,7 @@
 from scripts.ships.shipbase import Ship
 from scripts.ships.player import Player
 from scripts.scene import Scene
+from scripts.soundmanager import SoundManager
 
 class World(EventListenerBase):
 	"""
@@ -64,6 +65,8 @@
 		self._pausedtime = 0
 		self._starttime = 0
 		
+		self._soundmanager = SoundManager(self._engine)
+		
 		self._mainmenu = MainMenu(self)
 		self.showMainMenu()
 		
@@ -103,6 +106,7 @@
 		
 	def quit(self):
 		self.reset()
+		self._soundmanager.destroy()
 		self._applictaion.requestQuit()
 		
 	def reset(self):
@@ -129,7 +133,7 @@
 		self.reset()
 		self._map = loadMapFile(self._filename, self._engine)
 
-		self._scene = Scene(self, self._engine, self._map.getLayer('objects'))
+		self._scene = Scene(self, self._engine, self._map.getLayer('objects'), self._soundmanager)
 		self._scene.initScene(self._map)
 
 		self.initCameras()