Commits

stqn committed fc9786a

Sounds get lower volume when objects are far from the view.

  • Participants
  • Parent commits 927f12e

Comments (0)

Files changed (7)

File RWKC_linux.cbp

 		<Unit filename="src/configfile.d" />
 		<Unit filename="src/game.d" />
 		<Unit filename="src/gameconstants.d" />
+		<Unit filename="src/gamemanager.d" />
 		<Unit filename="src/gameobject.d" />
 		<Unit filename="src/gameobjectmanager.d" />
 		<Unit filename="src/gameobjects/boss.d" />
 		<Unit filename="src/gameobjects/projectile.d" />
 		<Unit filename="src/gameobjects/updatedgameobject.d" />
 		<Unit filename="src/gameobjects/wall.d" />
+		<Unit filename="src/gamepart.d" />
 		<Unit filename="src/inputactions.d" />
 		<Unit filename="src/inputconfiguration.d" />
 		<Unit filename="src/inputmanager.d" />
 Indispensable:
-- Écran/message de gain
 - Map du niveau 1
 - Gfx background original
 
 	Menu Pause
 
 
-Souhaitable:
+Bonus:
+- Écran/message de gain
 - Pause du jeu quand on désélectionne la fenêtre
 - Explosions/Particules (projectiles et ennemis)
-
-Bonus:
 - High scores
 - Écran et Menu principal
 - Sélecteur de Map.

File src/gamemanager.d

 			case GAMEPART_STATE.EXIT:
 				delete( mGamePart );
 				return( false );
+			//break;
+
+			default:
 			break;
 
-
 		}
 
 		return( true );

File src/gameobject.d

 module gameobject;
 
 import derelict.sdl.sdl;
+import std.math;
 import std.stdio;
 import map;
+import screen;
+import soundmanager;
 import texturemanager;
+import view;
 
 
 class GameObject {
 		}
 	}
 
+
+	/**
+	 * Plays sound with a volume that decreases with distance to the view.
+	 */
+	void playSound( Mix_Chunk* sound ) {
+		float dx = gView.mX + Screen.WIDTH/2 - mX;
+		float dy = gView.mY + Screen.HEIGHT/2 - mY;
+		float distance = sqrt( dx*dx + dy*dy );
+		// volume should be max when distance = 0, 0 when distance = Screen.WIDTH
+		if( distance > Screen.WIDTH )
+			distance = Screen.WIDTH;
+		int volume = cast(int) (MIX_MAX_VOLUME * (1 - distance/Screen.WIDTH));
+		//writefln( "playing sound with volume ", volume );
+		gSoundManager.play( sound, volume );
+	}
 }

File src/gameobjects/fluid.d

 		// Note: 128 is a special case that is optimised for speed.
 		SDL_SetAlpha( mSurface, SDL_SRCALPHA|SDL_RLEACCEL, 196 );
 
-		if( rand()%240 == 0 ) { // FIXME: temporary !!!
-			gSoundManager.play( mSound[ rand()%3 ] );
+		if( rand()%500 == 0 ) { // FIXME: temporary !!!
+			playSound( mSound[ rand()%3 ] );
 		}
 
 	}

File src/gameobjects/projectile.d

 
 
 	void explode() {
-		gSoundManager.play(mHitSound);
+		playSound(mHitSound);
 		if( mType == Type.PROJECTILE_ENEMY ) {
 			mState = State.EXPLODING;
 			mFlags &= ~(Flags.COLLISION_TESTER|Flags.COLLISION_TESTED);

File src/soundmanager.d

 		Mix_Volume(-1, mVolume );
 	}
 
+
 	void volumeUp() {
 		mVolume += MIX_MAX_VOLUME/10;
 		if( mVolume > MIX_MAX_VOLUME ) mVolume = MIX_MAX_VOLUME;
 		Mix_Volume( -1, mVolume );
 	}
 
+
 	void volumeDown() {
 		mVolume -= MIX_MAX_VOLUME/10;
 		if( mVolume < 0 ) mVolume = 0;
 		Mix_Volume( -1, mVolume );
 	}
 
+
 	int getVolume() {
 		return( mVolume );
 	}
 
+
 	Mix_Chunk* get( char[] filename ) {
-
 		Mix_Chunk** m = ( filename in mSoundList );
 
 		if( m != null ) {
 
 			return( s );
 		}
-
 	}
 
 
-	void play( Mix_Chunk* sound ) {
-
-		Mix_PlayChannel( -1, sound, 0 /*loop*/ );
+	void play( Mix_Chunk* sound, int volume = MIX_MAX_VOLUME ) {
+		int channel = Mix_PlayChannel( -1, sound, 0 /*loop*/ );
+		Mix_Volume( channel, volume );
 	}
 
-
 }