Commit cae6bb7a authored by Matthew Odle's avatar Matthew Odle

collisions partially to canvas libs

parent 77bd4f7c
/*jslint white: true */
var collisions = {
// TODO abstract non-centipede functionality and move to canvas-libs
check : function() {
Object.keys(lasers.lasers).forEach(key => {
let targets = this.getLaserTargets();
this.checkLaser(key, targets);
this.removeUsedLasers(key);
init : function() {
Object.assign(this, collisionsBase);
Object.keys(this.functionOverrides).forEach(element => {
this[element] = this.functionOverrides[element];
});
Object.keys(players.players).forEach(player =>
this.checkPlayerVsEnemies(players.players[player], this.getPlayerEnemies())
);
this.removeDestroyedTargets(targets);
},
getLaserTargets : function() {
let targets = [];
targets.push(...mushrooms.mushrooms);
targets.push(...centipedes.centipedes);
targets.push(...gameObjects.worms);
targets.push(...gameObjects.fleas);
targets.push(...gameObjects.spiders);
return targets;
},
checkLaser : function(player, targets) {
lasers.lasers[player].forEach(laser =>
targets.forEach(target => {
if (!laser.remove && laser.crashWith(target)) {
this.processImpact(target);
laser.remove = true;
};
})
);
},
processImpact : function(target) {
this.damageTarget(target);
sounds.playSound('impact');
},
damageTarget : function(target) {
target.hitPoints--;
if (target.hitPoints <= 0) {
this.processKill(target);
return;
};
if (target.type == 'worm') {
this.changeSize(target);
};
},
changeSize : function(target) {
if (target.imageType == 'crazy') {
let widthBefore = target.width;
let heightBefore = target.height;
target.width = target.defaultWidth * target.hitPoints;
target.height = target.defaultHeight * target.hitPoints;
target.x += (widthBefore - target.width) / 2;
target.y += (heightBefore - target.height) / 2;
};
},
processKill : function(target) {
metrics.addNewFloatingPoint(target.getMiddleX(), target.getMiddleY(), target.pointValue, "gain");
metrics.manageScore(target.pointValue);
this.handleCentipedeKill(target);
},
handleCentipedeKill(target) {
if (target.type === 'centipede') {
mushrooms.make({x : target.x, y : target.y})
centipedes.numberKilled += 1;
};
console.log('collisions initialized');
},
removeUsedLasers : function(player) {
lasers.lasers[player] = lasers.lasers[player].filter(laser => !laser.remove);
},
getPlayerEnemies : function() {
targets = [];
targets.push(...centipedes.centipedes);
targets.push(...gameObjects.fleas);
targets.push(...gameObjects.spiders);
return targets;
},
checkPlayerVsEnemies : function(player, targets) {
if (!knobsAndLevers.game.playerCollisionsEnabled) {
return;
};
targets.forEach(target => {
if (player.crashWith(target)) {
this.killPlayer();
functionOverrides : {
handleSpecialKills(target) {
if (target.type === 'centipede') {
mushrooms.make({x : target.x, y : target.y})
centipedes.numberKilled += 1;
};
},
getPlayerEnemies : function() {
let enemies = [];
enemies.push(...centipedes.centipedes);
enemies.push(...gameObjects.fleas);
enemies.push(...gameObjects.spiders);
return enemies;
},
getLaserTargets : function() {
let targets = [];
targets.push(...mushrooms.mushrooms);
targets.push(...centipedes.centipedes);
targets.push(...gameObjects.worms);
targets.push(...gameObjects.fleas);
targets.push(...gameObjects.spiders);
return targets;
},
check : function() {
let targets = this.getLaserTargets();
Object.keys(lasers.lasers).forEach(key => {
this.checkLaser(key, targets, lasers.lasers);
});
Object.keys(players.players).forEach(player =>
this.checkPlayerVsEnemies(players.players[player], this.getPlayerEnemies())
);
this.removeDestroyedTargets(targets);
},
checkPlayerVsEnemies : function(player, targets) {
if (!knobsAndLevers.game.playerCollisionsEnabled) {
return;
};
});
},
killPlayer : function() {
players.died = true;
metrics.lives.player1 -= 1;
if (metrics.lives.player1 <= 0) {
game.gameOver = true;
return;
};
targets.forEach(target => {
if (player.crashWith(target)) {
this.killPlayer();
return;
};
});
},
removeDestroyedTargets : function(targets) {
mushrooms.mushrooms = mushrooms.mushrooms.filter(mushroom => mushroom.hitPoints > 0);
centipedes.centipedes = centipedes.centipedes.filter(centipede => centipede.hitPoints > 0);
gameObjects.worms = gameObjects.worms.filter(worm => worm.hitPoints > 0);
gameObjects.fleas = gameObjects.fleas.filter(flea => flea.hitPoints > 0);
gameObjects.spiders = gameObjects.spiders.filter(spider => spider.hitPoints > 0);
},
},
withMushrooms : function(obj) {
return mushrooms.mushrooms.find(mushroom => obj.crashWith(mushroom));
},
removeDestroyedTargets : function(targets) {
mushrooms.mushrooms = mushrooms.mushrooms.filter(mushroom => mushroom.hitPoints > 0);
centipedes.centipedes = centipedes.centipedes.filter(centipede => centipede.hitPoints > 0);
gameObjects.worms = gameObjects.worms.filter(worm => worm.hitPoints > 0);
gameObjects.fleas = gameObjects.fleas.filter(flea => flea.hitPoints > 0);
gameObjects.spiders = gameObjects.spiders.filter(spider => spider.hitPoints > 0);
},
};
......@@ -50,7 +50,7 @@ var lasers = {
clearOutsideCanvas : function(playerName) {
this.lasers[playerName] = this.lasers[playerName].filter(laser => laser.y > 0);
this.lasers[playerName] = this.lasers[playerName].filter(laser => laser.y > 0 && !laser.remove);
},
......
......@@ -16,6 +16,7 @@ var init = {
players.init();
sounds.init();
texts.init();
collisions.init();
console.log("centipede game initialized");
},
......
......@@ -13,7 +13,7 @@ var libsPath = '../canvas-libs/app/scripts/';
var scriptsPath = 'app/scripts/';
var libs = [
'supporting.js',
'collisionsBase.js',
'component.js',
'controls/gamepad.js',
'controls/keyboard.js',
......@@ -33,6 +33,7 @@ var libs = [
'playerBase.js',
'sound.js',
'soundsBase.js',
'supporting.js',
'templates.js',
'texts.js',
];
......
......@@ -372,6 +372,7 @@ var knobsAndLevers = {
let result = supporting.roll(sides = 10);
worm.imageType = result.crit ? 'crazy' : 'normal';
if (worm.imageType == 'crazy') {
worm.sizeMod = true;
worm.hitPoints *= result.value;
worm.width *= result.value;
worm.height *= result.value;
......
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