Skip to content

Commit

Permalink
Added shoot sound and explosion sound.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunraz committed Sep 13, 2014
1 parent ce109aa commit 6f93f89
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 1 deletion.
Binary file added assets/explosion.mp3
Binary file not shown.
Binary file added assets/explosion.wav
Binary file not shown.
Binary file added assets/shoot.mp3
Binary file not shown.
Binary file added assets/shoot.wav
Binary file not shown.
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
1 change: 1 addition & 0 deletions lib/enemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lib/explosion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var Explosion = function(x, y) {
this.frameTimer = 1/12;

backgroundLayer.push(this);
playSound(explosionSound);
}

Explosion.prototype.update = function(controls, deltaT) {
Expand Down
43 changes: 43 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Player.prototype.shoot = function(deltaT) {

// Shoot maybe?
new Projectile(this.x, this.y, this.rotation, false);
playSound(shootSound);

this.shots--;
}
Expand Down

0 comments on commit 6f93f89

Please sign in to comment.