Skip to content

Commit

Permalink
Add laser and hit sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoldan committed Dec 30, 2016
1 parent 4bcfa65 commit d75a4bc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/ui/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Cookies from 'js-cookie';
import { FPS } from './sprite_sheets';
import UIHandler from './ui_handler';
import KeyHandler from './key_handler';
import SoundHandler from './sound_handler';

import Board from './board';
import Head from '../objects/head';
Expand All @@ -18,6 +19,7 @@ class Game {
this.stage = options.stage;
this.uiHandler = new UIHandler(this);
this.keyHandler = new KeyHandler(this);
this.soundHandler = new SoundHandler();
this.board = new Board(options);
this.initialStartLength = options.initialStartLength || 12;
this.started = false;
Expand Down Expand Up @@ -68,6 +70,7 @@ class Game {
fireLaser() {
if (!this.started || this.paused) return;
this.board.fireLaser();
this.soundHandler.playLaserSound();
}

updatePositions(e) {
Expand Down Expand Up @@ -133,6 +136,9 @@ class Game {
if(sponge.hits === 0) {
spongeIdxsToRemove.push(spongeIdx);
this.incrementScore(1);
this.soundHandler.playSeaSpongeDestroy();
} else {
this.soundHandler.playSeaSpongeHit();
}
hit = true;
}
Expand All @@ -146,6 +152,7 @@ class Game {
} else {
this.incrementScore(10);
}
this.soundHandler.playSegmentHit();
beamIdxsToRemove.push(beamIdx);
segmentIdxsToRemove.push(segmentIdx);
hit = true;
Expand Down
36 changes: 36 additions & 0 deletions lib/ui/sound_handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

import Tone from 'tone';


class SoundHandler {
constructor() {
const distortion = new Tone.Distortion(1).toMaster();
this.synth = new Tone.Synth({
oscillator: { type: 'square' },
envelope: { release: 0.2 },
});
this.synth.connect(distortion);
}

playLaserSound() {
this.synth.triggerAttackRelease("C4", "60hz", undefined, .5);
}

playSeaSpongeHit() {
this.synth.triggerAttackRelease("A2", "40hz", undefined, .1);
}

playSeaSpongeDestroy() {
this.synth.triggerAttackRelease("A1", "40hz", undefined, .1);
}

playSegmentHit() {
this.synth.triggerAttackRelease("F3", "40hz", undefined, .2);
}

playSegmentStep() {
this.synth.triggerAttackRelease("D0", "10hz", undefined, .05);
}
}

export default SoundHandler;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"babel-loader": "^6.2.10",
"babel-preset-es2015": "^6.18.0",
"js-cookie": "^2.1.3",
"tone": "^0.8.0",
"webpack": "^1.14.0"
}
}

0 comments on commit d75a4bc

Please sign in to comment.