Skip to content

Commit

Permalink
Implemented issue #20
Browse files Browse the repository at this point in the history
  • Loading branch information
jobtalle committed Sep 25, 2020
1 parent 38e26c0 commit fe7e030
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion js/audio/audioEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ AudioEngine.prototype.createGainNode = function() {
* @returns {StereoPannerNode} A stereo panner node
*/
AudioEngine.prototype.createPanNode = function() {
return new StereoPannerNode(this.context);
return this.context.createStereoPanner();
};

/**
Expand Down
16 changes: 14 additions & 2 deletions js/audio/instances/audioEffect.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,25 @@ const AudioEffect = function(engine, sources) {
AudioEffect.Track = function(engine, audio) {
const source = engine.createSourceNode(audio);

source.connect(engine.getDestinationNode());
this.nodePan = engine.createPanNode();

source.connect(this.nodePan).connect(engine.getDestinationNode());
};

/**
* Set the pan
* @param {Number} pan The pan in the range [-1, 1];
*/
AudioEffect.Track.prototype.setPan = function(pan) {
this.nodePan.pan.value = pan;
};

/**
* Play this audio effect
* @param {Number} pan The pan in the range [-1, 1];
* @returns {Number} The duration of the effect
*/
AudioEffect.prototype.play = function() {
AudioEffect.prototype.play = function(pan = 0) {
if (!this.engine.initialized)
return 0;

Expand All @@ -41,6 +52,7 @@ AudioEffect.prototype.play = function() {
if (this.tracks[index] === null)
this.tracks[index] = new AudioEffect.Track(this.engine, this.elements[index]);

this.tracks[index].setPan(pan);
this.elements[index].play();

return this.elements[index].duration;
Expand Down
2 changes: 1 addition & 1 deletion js/koi/fish/fishBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ FishBody.prototype.SAMPLER_SPRING_START = new SamplerPlateau(.15, .85, .95, 1.5)
FishBody.prototype.SAMPLER_SPRING_END = new SamplerPlateau(.05, .6, .7, 1.5);
FishBody.prototype.SPRING_POWER = 1.7;
FishBody.prototype.OFFSPRING_VERTEBRA = .3;
FishBody.prototype.KILOGRAMS_PER_AREA = 15;
FishBody.prototype.KILOGRAMS_PER_AREA = 22;

/**
* Deserialize a fish body
Expand Down
22 changes: 14 additions & 8 deletions js/koi/mover.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Mover.prototype.SPLASH_DROP_DISTANCE = 0.1;
Mover.prototype.AIR_RADIUS = 1.5;
Mover.prototype.AIR_INTENSITY = .4;
Mover.prototype.AIR_HEIGHT = .5;
Mover.prototype.BIG_THRESHOLD = 1.8;
Mover.prototype.BIG_THRESHOLD = 2;

/**
* Update the mover
Expand Down Expand Up @@ -122,12 +122,13 @@ Mover.prototype.startTouch = function(x, y) {
/**
* Play the fish specific interaction sound
* @param {Fish} fish The fish to play the sound for
* @param {Number} pan The pan in the range [-1, 1]
*/
Mover.prototype.playInteractionSound = function(fish) {
Mover.prototype.playInteractionSound = function(fish, pan) {
if (fish.getWeight() > this.BIG_THRESHOLD)
this.audio.effectFishMoveBig.play();
this.audio.effectFishMoveBig.play(pan);
else
this.audio.effectFishMoveSmall.play();
this.audio.effectFishMoveSmall.play(pan);
};

/**
Expand All @@ -139,16 +140,18 @@ Mover.prototype.playInteractionSound = function(fish) {
* @param {Random} random A randomizer
*/
Mover.prototype.pickUp = function(fish, x, y, waterPlane, random) {
const pan = 2 * fish.position.x / this.constellation.width - 1;

this.cursorPrevious.x = this.cursor.x = x;
this.cursorPrevious.y = this.cursor.y = y;
this.move = fish;
this.offset.x = fish.position.x - this.cursor.x;
this.offset.y = fish.position.y - this.cursor.y;
this.touch = true;

this.audio.effectFishUp.play();
this.audio.effectFishUp.play(pan);

this.playInteractionSound(fish);
this.playInteractionSound(fish, pan);

console.log(fish.getWeight().toFixed(2) + "kg");
console.log(fish); // TODO: For debugging only
Expand All @@ -162,8 +165,11 @@ Mover.prototype.pickUp = function(fish, x, y, waterPlane, random) {
*/
Mover.prototype.drop = function(waterPlane, random) {
if (this.move) {
this.playInteractionSound(this.move);
this.audio.effectFishDown.play();
const pan = 2 * this.move.position.x / this.constellation.width - 1;

this.audio.effectFishDown.play(pan);

this.playInteractionSound(this.move, pan);

this.constellation.drop(this.move);
this.createBodySplash(this.move.body, waterPlane, random);
Expand Down

0 comments on commit fe7e030

Please sign in to comment.