Commit 260fa54d authored by Matthew Odle's avatar Matthew Odle

refactor sounds

parent a468de88
......@@ -33,7 +33,7 @@ var collisions = {
},
processImpact : function(target) {
this.damageTarget(target);
sounds.playImpactSound(target.type);
sounds.playSound('impact');
},
damageTarget : function(target) {
target.hitPoints--;
......
......@@ -51,6 +51,7 @@ var centipedes = {
let centipede = Object.assign(new Component(knobsAndLevers.centipede.args), knobsAndLevers.centipede.defaults);
let pointValue = knobsAndLevers.centipede.pointValue;
centipede.pointValue = supporting.getRandom(pointValue, pointValue + 20);
centipede.sound = sounds.getSound('centipede');
return centipede;
},
cannotAdd : function(centipede) {
......
......@@ -45,6 +45,7 @@ var gameObjects = {
let spawnedCreature = new Component(knobsAndLevers[type].args);
let pointValue = knobsAndLevers[type].pointValue;
spawnedCreature.pointValue = supporting.getRandom(pointValue, pointValue + 400);
spawnedCreature.sound = sounds.getSound(type);
this[type].push(spawnedCreature);
},
clearOutsideCanvas : function(type) {
......
......@@ -19,7 +19,7 @@ var lasers = {
return;
};
this.add(player);
sounds.playAvailableLaserSound();
sounds.playSound('laser');
},
eligibleToSpawn : function(player) {
let eligible = this.lasers[player.name].length < knobsAndLevers.lasers.quantity.value
......
var sounds = {
// TODO abstract non-centipede functionality and move to canvas-libs
path : 'app/static/media/sounds/',
tracks : {
centipede : {filename : 'centipede.mp3', volume : 0.5, qty : 1, pool : []},
spider : {filename : 'spider.mp3', volume : 0.3, qty : 1, pool : []},
flea : {filename : 'flea.mp3', volume : 0.3, qty : 1, pool : []},
worm : {filename : 'worm.mp3', volume : 0.3, qty : 1, pool : []},
playerDied : {filename : 'player-died.mp3', volume : 0.3, qty : 1, pool : []},
tierChange : {filename : 'tier-change.mp3', volume : 0.3, qty : 1, pool : []},
laserPool : {filename : 'laser.mp3', volume : 0.3, qty : 20, pool : []},
impactPool : {filename : 'laser-impact.mp3', volume : 0.3, qty : knobsAndLevers.lasers.quantity.value, pool : []},
centipede : {filename : 'centipede.mp3', volume : 0.5, qty : 1, pool : [], index : 0},
spiders : {filename : 'spider.mp3', volume : 0.3, qty : 1, pool : [], index : 0},
fleas : {filename : 'flea.mp3', volume : 0.3, qty : 1, pool : [], index : 0},
worms : {filename : 'worm.mp3', volume : 0.3, qty : 1, pool : [], index : 0},
died : {filename : 'player-died.mp3', volume : 0.3, qty : 1, pool : [], index : 0},
tierChange : {filename : 'tier-change.mp3', volume : 0.3, qty : 1, pool : [], index : 0},
laser : {filename : 'laser.mp3', volume : 0.3, qty : 20, pool : [], index : 0},
impact : {filename : 'laser-impact.mp3', volume : 0.3, qty : knobsAndLevers.lasers.quantity.value, pool : [], index : 0},
},
init : function() {
Object.keys(this.tracks).forEach(key => {
......@@ -18,84 +17,27 @@ var sounds = {
});
console.log("sounds initialized");
},
buildSound : function(filename, volume) {
return new Sound(this.path + filename, volume);
},
buildManySounds : function(type, volume, qty) {
buildManySounds : function(file, volume, qty) {
let soundArray = [];
while (soundArray.length < qty) {
soundArray.push(this.buildSound(type, volume));
soundArray.push(new Sound(this.path + file, volume));
};
return soundArray;
},
manageSounds : function() {
if (!knobsAndLevers.game.sounds.value || !game.running) {
return;
};
this.manageCentipedeSounds();
this.manageSpiderSounds();
this.manageFleaSounds();
this.manageWormSounds();
},
manageCentipedeSounds : function() {
if (centipedes.centipedes != false) {
this.playSound(this.getSound('centipede'));
};
},
getSound : function(type) {
return this.getAvailableSound(this.tracks[type].pool);
},
getAvailableSound : function(availableSounds) {
let sound = availableSounds.pop();
availableSounds.unshift(sound);
return sound;
},
playSound : function(sound) {
playSound : function(type) {
if (knobsAndLevers.game.sounds.value) {
sound.play();
};
},
manageSpiderSounds : function() {
if (gameObjects.spiders != false) {
this.playSound(this.getSound('spider'));
this.getSound(type).play();
};
},
manageFleaSounds : function() {
let sound = this.getSound('flea');
if (gameObjects.fleas != false) {
if (!sound.played) {
this.playSound(sound);
};
sound.played = true;
} else {
sound.played = false;
};
getSound : function(type) {
let target = this.tracks[type];
target.index = (target.index + 1) % target.pool.length;
return target.pool[target.index];
},
manageWormSounds : function() {
if (gameObjects.worms != false) {
this.playSound(this.getSound('worm'));
} else {
this.stopSound(this.tracks['worm'].pool);
};
stopAllSounds : function() {
Object.keys(this.tracks).forEach(key => this.stopSound(this.tracks[key].pool));
},
stopSound : function(pool) {
pool.forEach(sound => sound.stop());
},
playAvailableLaserSound : function() {
this.playSound(this.getSound('laserPool'));
},
playImpactSound : function(type) {
if (type !== 'mushroom') {
this.playSound(this.getSound('impactPool'));
};
},
playDiedSound : function() {
this.playSound(this.getSound('playerDied'));
},
playTierChangeSound : function() {
this.playSound(this.getSound('tierChange'));
},
stopAllSounds : function() {
Object.keys(this.tracks).forEach(key => this.stopSound(this.tracks[key].pool));
},
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment