Skip to content

Commit

Permalink
moved logic for sound location from event to sound (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhnb committed Oct 27, 2019
1 parent 1168441 commit 088fe51
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
23 changes: 2 additions & 21 deletions srcjs/stendhal/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,9 @@ marauroa.rpeventFactory["sound_event"] = marauroa.util.fromProto(marauroa.rpeven
if (this.hasOwnProperty("volume")) {
volume *= parseInt(this["volume"], 10) / 100;
}
// Further adjustments if the sound has a radius
if (this.hasOwnProperty("radius")) {
if (!marauroa.me || !rpobject || !rpobject["_x"]) {
// Can't calculate the distance yet. Ignore the sound.
return;
}
var radius = parseInt(this["radius"], 10);

var radius = parseInt(this["radius"], 10);
var xdist = marauroa.me["_x"] - rpobject["_x"];
var ydist = marauroa.me["_y"] - rpobject["_y"];
var dist2 = xdist * xdist + ydist * ydist;
if (dist2 > radius * radius) {
// Outside the specified radius
return;
}
// The sound api does not guarantee anything about how the volume
// works, so it does not matter much how we scale it.
volume *= Math.min(radius * radius / (dist2 * 20), 1);
}
if (stendhal.ui.sound) {
stendhal.ui.sound.playEffect(this["sound"], volume);
}
stendhal.ui.sound.playLocalizedEffect(rpobject["_x"], rpobject["_y"], radius, this["layer"], this["sound"], volume);
}
});

Expand Down
23 changes: 22 additions & 1 deletion srcjs/stendhal/ui/sound.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,32 @@ var stendhal = window.stendhal = window.stendhal || {};
stendhal.ui = stendhal.ui || {};

stendhal.ui.sound = {
playEffect: function(soundName, volume) {
layers: ["music", "ambient", "creature", "sfx", "gui"],

playLocalizedEffect: function(x, y, radius, layer, soundName, volume) {
if (!stendhal.config.sound.play) {
return;
}

// Further adjustments if the sound has a radius
if (radius) {
if (!marauroa.me) {
// Can't calculate the distance yet. Ignore the sound.
return;
}

var xdist = marauroa.me["_x"] - x;
var ydist = marauroa.me["_y"] - y;
var dist2 = xdist * xdist + ydist * ydist;
if (dist2 > radius * radius) {
// Outside the specified radius
return;
}
// The sound api does not guarantee anything about how the volume
// works, so it does not matter much how we scale it.
volume *= Math.min(radius * radius / (dist2 * 20), 1);
}

var sound = new Audio();
sound.autoplay = true;
sound.volume = volume;
Expand Down

0 comments on commit 088fe51

Please sign in to comment.