From c96610c899a475907c20f954e7551e6dd183ce42 Mon Sep 17 00:00:00 2001 From: Andrew Kesterson Date: Fri, 13 Jun 2014 23:19:20 -0700 Subject: [PATCH] Add positional sounds from the tilemap --- moonlight/js/moonlight-skulk.js | 53 +++++++++++++++++---------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/moonlight/js/moonlight-skulk.js b/moonlight/js/moonlight-skulk.js index 59c0f34..7df1eb4 100644 --- a/moonlight/js/moonlight-skulk.js +++ b/moonlight/js/moonlight-skulk.js @@ -90,6 +90,32 @@ SoundSprite.prototype = Object.create(Phaser.Sprite.prototype); SoundSprite.prototype.constructor = Light; SoundSprite.prototype.update_new_values = function() { + this.adjust_relative_to = function(spr) { + console.log(spr); + if ( this.sound_nofade == true ) { + this.sound.volume = 1.0; + return; + } + + // The volume of any given sound is equal to the length of the + // hypotenuse of a triangle drawn from the point (p) to the + // sprite in question + + var xd = (spr.x - this.x); + if ( xd < 0 ) + xd = -(xd); + var yd = (spr.y - this.y); + if ( yd < 0 ) + yd = -(yd); + var hyp = Math.sqrt((xd * xd) + (yd * yd)); + var hyp_perfect = Math.sqrt( + ((game.camera.width/2) * (game.camera.width/2)) + + ((game.camera.height/2) * (game.camera.height/2)) + ); + this.sound.volume = Number(hyp_perfect / hyp); + console.log([hyp_perfect, hyp, this.sound.volume]); + } + if ( this.sound_key == null ) { if ( this.sound !== null ) { this.sound.stop(); @@ -106,32 +132,6 @@ SoundSprite.prototype.update_new_values = function() { console.log("Sound should be playing"); } -SoundSprite.prototype.adjust_relative_to = function(spr) { - console.log(spr); - if ( this.sound_nofade == true ) { - this.sound.volume = 1.0; - return; - } - - // The volume of any given sound is equal to the length of the - // hypotenuse of a triangle drawn from the point (p) to the - // sprite in question - - var xd = (spr.x - this.x); - if ( xd < 0 ) - xd = -(xd); - var yd = (spr.y - this.y); - if ( yd < 0 ) - yd = -(yd); - var hyp = Math.sqrt((xd * xd) + (yd * yd)); - var hyp_perfect = Math.sqrt( - ((game.camera.width/2) * (game.camera.width/2)) + - ((game.camera.height/2) * (game.camera.height/2)) - ); - this.sound.volume = Number(hyp_perfect / hyp); - console.log([hyp_perfect, hyp, this.sound.volume]); -} - var moonlightSettings = { 'map' : { 'tilesets': [ @@ -1078,6 +1078,7 @@ GameState.prototype.update = function() } function _fix_audio_relative(x) { + console.log(x); x.adjust_relative_to(this.player); } this.staticSounds.forEach(_fix_audio_relative, this);