diff --git a/assets/explosion.mp3 b/assets/explosion.mp3 new file mode 100644 index 0000000..4af6b0b Binary files /dev/null and b/assets/explosion.mp3 differ diff --git a/assets/explosion.wav b/assets/explosion.wav new file mode 100644 index 0000000..8f1ffac Binary files /dev/null and b/assets/explosion.wav differ diff --git a/assets/shoot.mp3 b/assets/shoot.mp3 new file mode 100644 index 0000000..3b091df Binary files /dev/null and b/assets/shoot.mp3 differ diff --git a/assets/shoot.wav b/assets/shoot.wav new file mode 100644 index 0000000..f2886e2 Binary files /dev/null and b/assets/shoot.wav differ diff --git a/gulpfile.js b/gulpfile.js index 4dda7f8..9653cf2 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -25,7 +25,7 @@ gulp.task('minify', function() { }); gulp.task('zip', ['minify'], function() { - return gulp.src(['g.js', 'index.html', 'assets/*.png'], { base: './' }) + return gulp.src(['g.js', 'index.html', 'assets/*.png', 'assets/*.mp3'], { base: './' }) .pipe(zip('AquaticBeastForce.zip')) .pipe(filesize()) .pipe(gulp.dest('dist')); diff --git a/lib/enemy.js b/lib/enemy.js index 591298f..e37b030 100644 --- a/lib/enemy.js +++ b/lib/enemy.js @@ -31,6 +31,7 @@ Enemy.prototype.update = function(controls, deltaT) { if(this.shootTimer <= 0.0) { new Projectile(this.x, this.y, this.rotation, true); this.shootTimer += 0.5; + playSound(shootSound); } this.shootTimer -= deltaT; diff --git a/lib/explosion.js b/lib/explosion.js index 0fa3952..e0956b5 100644 --- a/lib/explosion.js +++ b/lib/explosion.js @@ -5,6 +5,7 @@ var Explosion = function(x, y) { this.frameTimer = 1/12; backgroundLayer.push(this); + playSound(explosionSound); } Explosion.prototype.update = function(controls, deltaT) { diff --git a/lib/main.js b/lib/main.js index 759a153..1c18734 100644 --- a/lib/main.js +++ b/lib/main.js @@ -9,6 +9,16 @@ var healthTimer = 0; var gameOverTimer = 0.5; var enemyCountdown = 10; +var shootSound; +var explosionSound; +var audioContext; + +try { + window.AudioContext = window.AudioContext || window.webkitAudioContext; + var audioContext = new AudioContext(); + loadSounds(); +} catch(e) {} + entities.push(player); placeRandomEnemy(); @@ -73,4 +83,37 @@ function resetGame() { entities.push(player); placeRandomEnemy(); +} + +function loadSounds() { + var shootRequest = new XMLHttpRequest(); + shootRequest.open('GET', 'assets/shoot.mp3', true); + shootRequest.responseType = 'arraybuffer'; + + // Decode asynchronously + shootRequest.onload = function() { + audioContext.decodeAudioData(shootRequest.response, function(buffer) { + shootSound = buffer; + }, function() { }); + } + shootRequest.send(); + + var explosionRequest = new XMLHttpRequest(); + explosionRequest.open('GET', 'assets/explosion.mp3', true); + explosionRequest.responseType = 'arraybuffer'; + + // Decode asynchronously + explosionRequest.onload = function() { + audioContext.decodeAudioData(explosionRequest.response, function(buffer) { + explosionSound = buffer; + }, function() { }); + } + explosionRequest.send(); +} + +function playSound(buffer) { + var source = audioContext.createBufferSource(); + source.buffer = buffer; + source.connect(audioContext.destination); + source.start(0); } \ No newline at end of file diff --git a/lib/player.js b/lib/player.js index 97c2aba..ccbdb4b 100644 --- a/lib/player.js +++ b/lib/player.js @@ -89,6 +89,7 @@ Player.prototype.shoot = function(deltaT) { // Shoot maybe? new Projectile(this.x, this.y, this.rotation, false); + playSound(shootSound); this.shots--; }