Browse files

- Added rudimentary support for 2D positional audio.

  • Loading branch information...
1 parent 51dcadb commit 03367509e48bb8232a79222b564566047390680d @denravonska committed Sep 24, 2010
View
10 src/com/ormgas/hackathon2010/GameScene.java
@@ -11,12 +11,14 @@
import com.ormgas.hackathon2010.eventbus.EntitySpawnedEvent;
import com.ormgas.hackathon2010.eventbus.EventBus;
+import com.ormgas.hackathon2010.eventbus.PlayRelativeSoundEvent;
import com.ormgas.hackathon2010.eventbus.SpawnBulletEvent;
import com.ormgas.hackathon2010.eventbus.SpawnExplosionEvent;
import com.ormgas.hackathon2010.gameobjects.BulletObject;
import com.ormgas.hackathon2010.gameobjects.ExplosionObject;
import com.ormgas.hackathon2010.gameobjects.ObjectHandler;
import com.ormgas.hackathon2010.networking.ServerClient.GameEvent;
+import com.ormgas.hackathon2010.sound.RelativeSound;
import com.ormgas.hackathon2010.eventbus.EventHandler;
public class GameScene extends Scene implements IGameEventHandler
@@ -25,6 +27,9 @@
private Camera camera;
private ScrollableParallaxBackground background = null;
public RectangularShape worldFloor;
+ private final static float WORLD_DISTANCE =
+ GameActivity.WORLD_WIDTH * GameActivity.WORLD_WIDTH +
+ GameActivity.WORLD_HEIGHT * GameActivity.WORLD_HEIGHT;
public GameScene(Camera camera)
{
@@ -81,6 +86,11 @@ public void onGameObjectSpawnedEvent(EntitySpawnedEvent event) {
}
@EventHandler
+ public void onPlayRelativeSoundEvent(PlayRelativeSoundEvent event) {
+ RelativeSound.play(event.sound, event.x, event.y, camera.getCenterX(), camera.getCenterY(), WORLD_DISTANCE);
+ }
+
+ @EventHandler
public void onSpawnExplosionEvent(SpawnExplosionEvent event)
{
ExplosionObject explosion = ObjectHandler.obtainItem(ExplosionObject.class);
View
6 src/com/ormgas/hackathon2010/gameobjects/BulletObject.java
@@ -4,11 +4,13 @@
import com.ormgas.hackathon2010.Sounds;
import com.ormgas.hackathon2010.Textures;
import com.ormgas.hackathon2010.eventbus.EventBus;
+import com.ormgas.hackathon2010.eventbus.PlayRelativeSoundEvent;
import com.ormgas.hackathon2010.eventbus.SpawnExplosionEvent;
public class BulletObject extends GameObject {
private final SpawnExplosionEvent explosionEvent = new SpawnExplosionEvent();
+ private final PlayRelativeSoundEvent explosionSoundEvent = new PlayRelativeSoundEvent();
public BulletObject(int id, int ownerId, float x, float y, float heading) {
super(id, x, y, 3, heading, Textures.bullet);
@@ -23,8 +25,8 @@ protected void onPositionChanged() {
final float y = this.getY();
if(x < 0 || x > GameActivity.WORLD_WIDTH ||
y < 0 || y > GameActivity.WORLD_HEIGHT) {
- Sounds.explosion1.play();
-
+ explosionSoundEvent.set(Sounds.explosion1, x, y);
+ EventBus.dispatch(explosionSoundEvent);
explosionEvent.set(x, y);
EventBus.dispatch(explosionEvent);
View
41 src/com/ormgas/hackathon2010/sound/RelativeSound.java
@@ -0,0 +1,41 @@
+package com.ormgas.hackathon2010.sound;
+
+import org.anddev.andengine.audio.sound.Sound;
+import org.anddev.andengine.entity.shape.IShape;
+
+import android.util.Log;
+
+import com.ormgas.hackathon2010.GameActivity;
+
+public class RelativeSound {
+
+ private final static String TAG = RelativeSound.class.getSimpleName();
+
+ /**
+ * Simple approximation of 2D positional audio. Tries to lower the sound's
+ * volume the further away it is. The distance calculation relies on knowing
+ * how large the world is.
+ *
+ * @param sound Sound to play.
+ * @param self Position of the sound.
+ * @param other Position of the object relative to the sound.
+ */
+ public static void play(Sound sound, float soundX, float soundY, float listenerX, float listenerY, float worldDistance) {
+ final float distanceX = soundX - listenerX;
+ final float distanceY = soundY - listenerY;
+ final float sqdist = distanceX * distanceX + distanceY * distanceY;
+
+ // Boost distance to get faster cutoff
+ float volume = (worldDistance - sqdist * 3) * (1f / worldDistance);
+ if(volume > 0) {
+ Log.d(TAG, "Playing at volume " + volume);
+ sound.setVolume(
+ distanceX >= 0 ? volume : volume * 0.75f,
+ distanceX <= 0 ? volume : volume * 0.75f);
+ sound.play();
+ } else {
+ Log.d(TAG, "Distance to far. Ignoring sound");
+ }
+
+ }
+}

0 comments on commit 0336750

Please sign in to comment.