changeset 379:e0e284fcb288

Make a sound when an enemy is hit.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 30 Aug 2012 11:34:56 +0200
parents 11d895b6c0dc
children c25530efea3b
files pytouhou/game/enemy.py pytouhou/game/player.py pytouhou/games/eosd.py pytouhou/ui/gamerunner.py pytouhou/ui/music.py
diffstat 5 files changed, 45 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/game/enemy.py
+++ b/pytouhou/game/enemy.py
@@ -106,7 +106,7 @@ class Enemy(object):
             26: 'kira01',
             27: 'kira02'
         }[index]
-        self._game.enemy_sfx.play('%s.wav' % name)
+        self._game.sfx_player.play('%s.wav' % name)
 
 
     def set_bullet_attributes(self, type_, anim, sprite_idx_offset,
@@ -228,6 +228,7 @@ class Enemy(object):
     def die_anim(self):
         anim = {0: 3, 1: 4, 2: 5}[self.death_anim % 256] # The TB is wanted, if index isn’t in these values the original game crashs.
         self._game.new_effect((self.x, self.y), anim)
+        self._game.sfx_player.play('enep00.wav')
 
 
     def drop_particles(self, number, color):
@@ -312,6 +313,7 @@ class Enemy(object):
                 bullet.collide()
                 if self.damageable:
                     damages += bullet.damage
+                self._game.sfx_player.play('damage00.wav')
 
         # Check for enemy-laser collisions
         for laser in self._game.players_lasers:
@@ -326,6 +328,7 @@ class Enemy(object):
                     or ly < ey1):
                 if self.damageable:
                     damages += laser.damage
+                self._game.sfx_player.play('damage00.wav')
                 self.drop_particles(1, 1) #TODO: don’t call each frame.
 
         # Check for enemy-player collisions
--- a/pytouhou/game/player.py
+++ b/pytouhou/game/player.py
@@ -98,7 +98,7 @@ class Player(object):
 
 
     def play_sound(self, name):
-        self._game.player_sfx.play('%s.wav' % name)
+        self._game.sfx_player.play('%s.wav' % name)
 
 
     def collide(self):
--- a/pytouhou/games/eosd.py
+++ b/pytouhou/games/eosd.py
@@ -194,10 +194,13 @@ class EoSDInterface(object):
                 timeout_label.set_color('blue')
             elif timeout >= 10:
                 timeout_label.set_color('darkblue')
-            elif timeout >= 5:
-                timeout_label.set_color('purple')
             else:
-                timeout_label.set_color('red')
+                if timeout >= 5:
+                    timeout_label.set_color('purple')
+                else:
+                    timeout_label.set_color('red')
+                if (boss.timeout - boss.frame) % 60 == 0 and boss.timeout != 0:
+                    self.game.sfx_player.play('timeout.wav', volume=1.)
             timeout_label.set_text('%02d' % (timeout if timeout >= 0 else 0))
             timeout_label.changed = True
 
--- a/pytouhou/ui/gamerunner.py
+++ b/pytouhou/ui/gamerunner.py
@@ -29,7 +29,7 @@ from pytouhou.utils.helpers import get_l
 from pytouhou.utils.matrix import Matrix
 
 from .gamerenderer import GameRenderer
-from .music import MusicPlayer, SFXPlayer
+from .music import MusicPlayer, SFXPlayer, NullPlayer
 from .shaders.eosd import GameShader, BackgroundShader
 
 
@@ -75,9 +75,7 @@ class GameRunner(pyglet.window.Window, G
 
         game.music = MusicPlayer(game.resource_loader, bgms)
         game.music.play(0)
-
-        game.player_sfx = SFXPlayer(game.resource_loader)
-        game.enemy_sfx = SFXPlayer(game.resource_loader)
+        game.sfx_player = SFXPlayer(game.resource_loader) if not self.skip else NullPlayer()
 
 
     def set_input(self, replay=None):
@@ -177,6 +175,7 @@ class GameRunner(pyglet.window.Window, G
                     if self.skip:
                         self.set_input()
                         self.skip = False
+                        self.game.sfx_player = SFXPlayer(self.game.resource_loader)
 
             if self.save_keystates is not None:
                 self.save_keystates.append(keystate)
--- a/pytouhou/ui/music.py
+++ b/pytouhou/ui/music.py
@@ -124,35 +124,43 @@ class MusicPlayer(object):
 
 
 class SFXPlayer(object):
-    def __init__(self, loader):
+    def __init__(self, loader, volume=.42):
         self.loader = loader
+        self.players = {}
         self.sounds = {}
-
-        self.player = Player()
-        self.player.volume = .5
-
-        #'powerup.wav', 'graze.wav', 'timeout.wav', 'extend.wav', 'kira02.wav', 'kira01.wav',
-        #'kira00.wav', 'item00.wav', 'damage00.wav', 'nep00.wav', 'enep01.wav', 'lazer01.wav',
-        #'lazer00.wav', 'cat00.wav', 'gun00.wav', 'select00.wav', 'cancel00.wav', 'ok00.wav',
-        #'tan02.wav', 'tan01.wav', 'tan00.wav', 'power1.wav', 'power0.wav', 'pldead00.wav',
-        #'enep00.wav', 'plst00.wav')}
+        self.volume = volume
 
 
-    def __getitem__(self, name):
-        if not name in self.sounds:
-            self.sounds[name] = self.load_sound(name)
+    def get_player(self, name):
+        if name not in self.players:
+            self.players[name] = Player()
+            self.players[name].volume = self.volume
+        return self.players[name]
+
+
+    def get_sound(self, name):
+        if name not in self.sounds:
+            wave_file = self.loader.get_file(name)
+            self.sounds[name] = StaticSource(WaveSource(name, wave_file))
         return self.sounds[name]
 
 
-    def load_sound(self, name):
-        file = self.loader.get_file(name)
-        return StaticSource(WaveSource(name, file))
+    def play(self, name, volume=None):
+        sound = self.get_sound(name)
+        player = self.get_player(name)
+        if volume:
+            player.volume = volume
+        if player.playing:
+            player.next()
+        if sound:
+            player.queue(sound)
+        player.play()
+
+
+class NullPlayer(object):
+    def __init__(self, loader=None, bgms=None):
+        pass
+
 
     def play(self, name):
-        sound = self[name]
-        if self.player.playing:
-            self.player.next()
-        if sound:
-            self.player.queue(sound)
-        self.player.play()
-
+        pass