Skip to content

Commit

Permalink
added sounds and sound library
Browse files Browse the repository at this point in the history
  • Loading branch information
basdebruin committed Nov 26, 2020
1 parent 1d91708 commit 91e3331
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
_site
_site
assets/sounds/405423__straget__wall-clock-ticking.wav
2 changes: 2 additions & 0 deletions assets/js/howler.core.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions assets/js/howler.min.js

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion assets/js/init.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//-script.
// load and setup swup
let swup;
let globalSounds = {};
$(document).ready(()=>{
swup = new Swup({
plugins: [new SwupScrollPlugin()]
Expand All @@ -12,7 +13,10 @@ $(document).ready(()=>{
});

// look for script with data-load-script and exec
swup.on('contentReplaced', loadScripts)
swup.on('contentReplaced', loadScripts);

// init sounds
loadGlobalSounds();
});


Expand All @@ -24,5 +28,17 @@ function loadScripts() {
try { swup.scrollTo(document.body, 0); } catch {}
}

// function for loading global win and fail sounds
function loadGlobalSounds() {
if (Howl) {
globalSounds.win = new Howl({
src: '/assets/sounds/win.mp3'
});
globalSounds.fail = new Howl({
src: '/assets/sounds/fail.mp3'
});
}
}

// simple reverse string function
const reverse = str => String(str).split("").reverse().join("");
55 changes: 52 additions & 3 deletions assets/js/level-adhd.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var adhd = {
order: [],
orderIndex: 0,

// sounds
sounds: {},

// initialisation
init() {

Expand All @@ -37,6 +40,9 @@ var adhd = {
console.log(this.shuffle([0, 1, 2, 3, 4]));
this.addOrder();

// sound
this.loadSounds();

},

addOrder() {
Expand All @@ -53,6 +59,8 @@ var adhd = {

// function called by bomb wire buttons
cutWire(index) {
this.sounds.snip.play();

if (index == this.order[this.orderIndex]) {

// CORRECT WIRE
Expand All @@ -72,8 +80,7 @@ var adhd = {
},


// creating order

// creating order shuffle function
shuffle(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
Expand All @@ -84,7 +91,8 @@ var adhd = {
return array;
},

// methods
// runs every 41 ms ~= 24 fps
// updates clock and stops game if time is up
updateClock() {

adhd.time = new Date(adhd.TOTAL_TIME - (new Date().getTime() - adhd.startTime));
Expand Down Expand Up @@ -120,29 +128,69 @@ var adhd = {
},

hidePopUp() {
// hide
$('#popup').removeClass('active');
// play alarm if not yet playing
//if (!this.sounds.alarm.playing()) this.sounds.alarm.play();
this.sounds.alarm.mute(false);
},

showPopUp(time = '00:30') {
// show
$('#popup').addClass('active');

time = ' '+time+'!!';
$('#time').text( $('#time').attr('data-original-text') + time);
},


// register SOUNDS
loadSounds() {
// load alarm sound: loop
this.sounds.alarm = new Howl({
src: '/assets/sounds/alarm.mp3',
loop: true,
autoplay: true
});

// explosion: one-shot
this.sounds.explosion = new Howl({
src: '/assets/sounds/explosion.mp3',
loop: false,
autoplay: false
});

// wire cut: one-shot
this.sounds.snip = new Howl({
src: '/assets/sounds/snip.mp3',
loop: false,
autoplay: false
});
},

// quickly stop all sounds
stopSounds() {
this.sounds.alarm.stop();
},

// WIN GAME
endGame() {
console.log('you win');

clearInterval(this.clockInterval);
showScore(100 + (this.time.getSeconds() + this.time.getMinutes()*60) * 3.3); // calc score based on time left

adhd.stopSounds();
},

// hide game and show explosion
stopGame() {
clearInterval(this.clockInterval);
$('#in-game').empty();
$('#fail').addClass('active');

this.stopSounds();
this.sounds.explosion.play();
},

retry() {
Expand All @@ -155,5 +203,6 @@ var adhd = {
adhd.init();

function unload() {
adhd.stopSounds();
delete adhd;
}
6 changes: 6 additions & 0 deletions assets/js/stars.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ function showScore(score = 0) {
// show it
$('#score-background').css('display', 'flex');

// play it
if (score < 100)
globalSounds.fail.play();
else
globalSounds.win.play();

// animate it (remove animation class)
setTimeout(() => { $('#score').removeClass('is-animating') }, 100);

Expand Down
Binary file added assets/sounds/alarm.mp3
Binary file not shown.
Binary file added assets/sounds/explosion.mp3
Binary file not shown.
Binary file added assets/sounds/fail.mp3
Binary file not shown.
Binary file added assets/sounds/snip.mp3
Binary file not shown.
Binary file added assets/sounds/win.mp3
Binary file not shown.
3 changes: 3 additions & 0 deletions src/_includes/layouts/head.pug
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ head
script(src='/assets/js/SwupScrollPlugin.min.js')
script(src='/assets/js/init.js')

//- howler audio library
script(src='/assets/js/howler.core.min.js')

//- star script
script(src='/assets/js/stars.js')
//- timer script
Expand Down
1 change: 1 addition & 0 deletions styles/_head.scss
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ a:focus, button:focus {

button {
border: none;
font-family: $fontFamily;
}

0 comments on commit 91e3331

Please sign in to comment.