Commit 5f07a580 authored by Matthew Odle's avatar Matthew Odle

add gameObjectsBase

parent 3be1c0c1
......@@ -3,9 +3,7 @@ var collisions = {
// TODO abstract non-centipede functionality and move to canvas-libs
init : function() {
Object.assign(this, collisionsBase);
Object.keys(this.functionOverrides).forEach(element => {
this[element] = this.functionOverrides[element];
});
supporting.applyOverrides(this);
console.log('collisions initialized');
},
functionOverrides : {
......
......@@ -6,21 +6,53 @@ var centipedes = {
numberSpawned : 0,
numberKilled : 0,
spawnPoints : 1,
manage : function() {
this.spawn();
this.update();
},
spawn : function() {
this.determineSpawnPositions();
if (!this.eligibleToSpawn()) {
return;
};
this.setXPosition();
let centipede = this.make();
if (this.cannotAdd(centipede)) {
return;
};
this.add(centipede);
init : function() {
Object.assign(this, gameObjectsBase);
supporting.applyOverrides(this);
console.log('centipedes initialized');
},
functionOverrides : {
manage : function() {
this.spawn();
this.update();
},
spawn : function() {
this.determineSpawnPositions();
if (!this.eligibleToSpawn()) {
return;
};
this.setXPosition();
let centipede = this.make();
if (this.cannotAdd(centipede)) {
return;
};
this.add(centipede);
},
make : function() {
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;
},
add : function(centipede) {
this.centipedes.push(centipede);
this.numberSpawned++;
},
update : function() {
this.resetCentipedeUpdateFlag();
this.determineDirections();
this.updateDirections();
this.updateCoordinates();
for (i = 0; i < this.centipedes.length; i += 1) {
this.centipedes[i].update();
};
},
clear : function() {
this.centipedes = [];
this.numberSpawned = 0;
this.numberKilled = 0;
},
},
determineSpawnPositions : function() {
if (!game.levelIsOver()) {
......@@ -47,34 +79,9 @@ var centipedes = {
setXPosition : function() {
knobsAndLevers.centipede.args.x = this.positions[this.centipedes.length % this.positions.length];
},
make : function() {
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) {
return this.centipedes.find(checkCentipede => checkCentipede.crashWith(centipede));
},
add : function(centipede) {
this.centipedes.push(centipede);
this.numberSpawned++;
},
update : function() {
this.resetCentipedeUpdateFlag();
this.determineDirections();
this.updateDirections();
this.updateCoordinates();
for (i = 0; i < this.centipedes.length; i += 1) {
this.centipedes[i].update();
};
},
clear : function() {
this.centipedes = [];
this.numberSpawned = 0;
this.numberKilled = 0;
},
determineDirections : function() {
this.centipedes.filter(centipede => !centipede.updated).forEach(centipede => {
this.moveDownwardInitially(centipede);
......
......@@ -4,6 +4,8 @@ var gameObjects = {
fleas : [],
spiders : [],
init : function() {
Object.assign(this, gameObjectsBase);
supporting.applyOverrides(this);
this.intervals = {
fleas : knobsAndLevers.fleas.initialInterval,
worms : knobsAndLevers.worms.initialInterval,
......@@ -11,15 +13,61 @@ var gameObjects = {
};
console.log('gameObjects initialized');
},
manage : function() {
Object.keys(this.intervals).forEach(type => {
this.spawnCreatureAtIntervals(type);
if (this[type] == false) {
functionOverrides : {
manage : function() {
Object.keys(this.intervals).forEach(type => {
this.spawnCreatureAtIntervals(type);
if (this[type] == false) {
return;
};
this[type] = this.clearOutsideCanvas(type);
this.update(type);
});
},
spawn : function(type) {
this.setMax(type);
if (this[type].length >= knobsAndLevers[type].maxNumber) {
return;
};
this[type] = this.clearOutsideCanvas(type);
this.update(type);
});
this.make(type);
},
make : function(type) {
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) {
if (this[type] == false) { return; };
return this[type].filter(target => {
return target.x < game.gameArea.canvas.width + 10
&& target.x > 0 - target.width
&& target.y < game.gameArea.canvas.height
});
},
update : function(type) {
this[type].forEach(creature => {
if (type == 'spiders') {
this.setSpeed(creature, type);
this.setDirection(creature);
this.removeMushrooms(creature);
};
if (type == 'fleas') {
this.dropMushrooms(creature);
};
if (type == 'worms') {
this.changeMushrooms(creature);
};
creature.newPos();
creature.update();
});
},
clear : function() {
this.worms = [];
this.fleas = [];
this.spiders = [];
},
},
spawnCreatureAtIntervals(type) {
if (supporting.everyinterval(game.gameArea.frameNo, this.intervals[type])) {
......@@ -30,49 +78,10 @@ var gameObjects = {
};
};
},
spawn : function(type) {
this.setMax(type);
if (this[type].length >= knobsAndLevers[type].maxNumber) {
return;
};
this.make(type);
},
setMax : function(type) {
let tier = knobsAndLevers.game.tier;
knobsAndLevers[type].maxNumber = tier.isMaxed ? tier.max : tier.current;
},
make : function(type) {
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) {
if (this[type] == false) { return; };
return this[type].filter(target => {
return target.x < game.gameArea.canvas.width + 10
&& target.x > 0 - target.width
&& target.y < game.gameArea.canvas.height
});
},
update : function(type) {
this[type].forEach(creature => {
if (type == 'spiders') {
this.setSpeed(creature, type);
this.setDirection(creature);
this.removeMushrooms(creature);
};
if (type == 'fleas') {
this.dropMushrooms(creature);
};
if (type == 'worms') {
this.changeMushrooms(creature);
};
creature.newPos();
creature.update();
});
},
setSpeed : function(creature, type) {
let speedLimits = knobsAndLevers[type].speedLimits;
creature.speedX = Math.sign(creature.speedX) * (supporting.roll(10).crit ? speedLimits.max : speedLimits.min);
......@@ -111,9 +120,4 @@ var gameObjects = {
};
});
},
clear : function() {
this.worms = [];
this.fleas = [];
this.spiders = [];
},
};
/*jslint white: true */
var lasers = {
lasers : {player1 : [], player2 : []},
manage : function() {
Object.keys(this.lasers).forEach(key => {
let player = players.players[key];
if (!player) {
init : function() {
Object.assign(this, gameObjectsBase);
supporting.applyOverrides(this);
console.log('lasers initialized');
},
functionOverrides : {
manage : function() {
Object.keys(this.lasers).forEach(key => {
let player = players.players[key];
if (!player) {
return;
};
this.spawn(player);
this.update(key);
this.clearOutsideCanvas(key);
});
},
spawn : function(player) {
if (!this.eligibleToSpawn(player)) {
return;
};
this.spawn(player);
this.update(key);
this.clearOutsideCanvas(key);
});
},
spawn : function(player) {
if (!this.eligibleToSpawn(player)) {
return;
};
this.add(player);
sounds.playSound('laser');
this.add(player);
sounds.playSound('laser');
},
add : function(player) {
this.lasers[player.name].push(this.make(player));
},
make : function(player) {
let laserArgs = knobsAndLevers.lasers.args;
laserArgs.extraArgs.speed.y = -1 * knobsAndLevers.lasers.speed.value;
laserArgs.x = player.x + player.width / 2;
laserArgs.y = player.y;
return new Component(laserArgs);
},
update : function(playerName) {
for (i = 0; i < this.lasers[playerName].length; i += 1) {
this.lasers[playerName][i].y += this.lasers[playerName][i].speedY;
this.lasers[playerName][i].update();
}
},
clearOutsideCanvas : function(playerName) {
this.lasers[playerName] = this.lasers[playerName].filter(laser => laser.y > 0 && !laser.remove);
},
clear : function() {
this.lasers = {player1 : [], player2 : []};
},
},
eligibleToSpawn : function(player) {
let eligible = this.lasers[player.name].length < knobsAndLevers.lasers.quantity.value
......@@ -29,32 +56,4 @@ var lasers = {
&& controls.isFiring(player);
return eligible;
},
add : function(player) {
this.lasers[player.name].push(this.make(player));
},
make : function(player) {
let laserArgs = knobsAndLevers.lasers.args;
laserArgs.extraArgs.speed.y = -1 * knobsAndLevers.lasers.speed.value;
laserArgs.x = player.x + player.width / 2;
laserArgs.y = player.y;
return new Component(laserArgs);
},
update : function(playerName) {
for (i = 0; i < this.lasers[playerName].length; i += 1) {
this.lasers[playerName][i].y += this.lasers[playerName][i].speedY;
this.lasers[playerName][i].update();
}
},
clearOutsideCanvas : function(playerName) {
this.lasers[playerName] = this.lasers[playerName].filter(laser => laser.y > 0 && !laser.remove);
},
clear : function() {
this.lasers = {player1 : [], player2 : []};
},
};
/*jslint white: true */
var mushrooms = {
mushrooms : [],
manage : function() {
if (game.gameArea.frameNo == 1) {
this.spawn(knobsAndLevers.mushrooms.initialAmount);
};
this.update();
init : function() {
Object.assign(this, gameObjectsBase);
supporting.applyOverrides(this);
console.log('mushrooms initialized');
},
spawn : function(amount) {
let coordinates = {};
while (this.mushrooms.length < amount) {
coordinates = this.getCoordinates();
// while it is possible this will be an infinite loop
// it is unlikely so long as maxMushrooms is reasonable
if (coordinates.y > knobsAndLevers.player.topLimit) {
continue;
functionOverrides : {
manage : function() {
if (game.gameArea.frameNo == 1) {
this.spawn(knobsAndLevers.mushrooms.initialAmount);
};
this.make(coordinates, knobsAndLevers.mushrooms.color);
};
this.update();
},
spawn : function(amount) {
let coordinates = {};
while (this.mushrooms.length < amount) {
coordinates = this.getCoordinates();
// while it is possible this will be an infinite loop
// it is unlikely so long as maxMushrooms is reasonable
if (coordinates.y > knobsAndLevers.player.topLimit) {
continue;
};
this.make(coordinates, knobsAndLevers.mushrooms.color);
};
},
make : function(coordinates, color) {
if (coordinates.x == undefined || coordinates.y == undefined) {
throw new Error('coordinate error: x: ' + coordinates.x + ', y: ' + coordinates.y);
};
color = color ? color : knobsAndLevers.mushrooms.color;
coordinates.x = supporting.getClosest(game.gameArea.xVertices, coordinates.x);
coordinates.y = supporting.getClosest(game.gameArea.yVertices, coordinates.y);
let mushroom = this.generate(coordinates, color);
if (collisions.withMushrooms(mushroom) || this.collidesWithPlayers(mushroom)) {
return;
};
this.mushrooms.push(mushroom);
},
generate : function(coordinates, color) {
let mushroom = new Component(knobsAndLevers.mushrooms.args);
mushroom.x = coordinates.x + knobsAndLevers.mushrooms.scaleFactor,
mushroom.y = coordinates.y + knobsAndLevers.mushrooms.scaleFactor,
mushroom.pointValue = metrics.currentLevel;
return mushroom;
},
update : function() {
for (i = 0; i < this.mushrooms.length; i += 1) {
this.mushrooms[i].update();
};
},
clear : function() {
this.mushrooms = [];
},
},
getCoordinates : function() {
let coordinates = {x : 0, y : 0};
......@@ -25,35 +60,7 @@ var mushrooms = {
coordinates.y = game.gameArea.yVertices[Math.floor(Math.random() * game.gameArea.yVertices.length)];
return coordinates;
},
make : function(coordinates, color) {
if (coordinates.x == undefined || coordinates.y == undefined) {
throw new Error('coordinate error: x: ' + coordinates.x + ', y: ' + coordinates.y);
};
color = color ? color : knobsAndLevers.mushrooms.color;
coordinates.x = supporting.getClosest(game.gameArea.xVertices, coordinates.x);
coordinates.y = supporting.getClosest(game.gameArea.yVertices, coordinates.y);
let mushroom = this.generate(coordinates, color);
if (collisions.withMushrooms(mushroom) || this.collidesWithPlayers(mushroom)) {
return;
};
this.mushrooms.push(mushroom);
},
collidesWithPlayers : function(mushroom) {
return Object.keys(players.players).find(key => players.players[key].crashWith(mushroom));
},
generate : function(coordinates, color) {
let mushroom = new Component(knobsAndLevers.mushrooms.args);
mushroom.x = coordinates.x + knobsAndLevers.mushrooms.scaleFactor,
mushroom.y = coordinates.y + knobsAndLevers.mushrooms.scaleFactor,
mushroom.pointValue = metrics.currentLevel;
return mushroom;
},
update : function() {
for (i = 0; i < this.mushrooms.length; i += 1) {
this.mushrooms[i].update();
};
},
clear : function() {
this.mushrooms = [];
},
};
......@@ -3,9 +3,7 @@ players = {
init : function() {
Object.assign(this, playersBase);
this.buildPlayers(game.numberOfPlayers, knobsAndLevers.player.args);
Object.keys(this.functionOverrides).forEach(element => {
this[element] = this.functionOverrides[element];
});
supporting.applyOverrides(this);
console.log('players initialized');
},
functionOverrides : {
......@@ -15,8 +13,8 @@ players = {
this.boundaries.aboveBottom = player.getBottom() < game.gameArea.canvas.height;
this.boundaries.insideLeft = player.getLeft() > 0;
},
},
collidedWithBarrier : function(player) {
return collisions.withMushrooms(player);
collidedWithBarrier : function(player) {
return collisions.withMushrooms(player);
},
},
};
......@@ -2,6 +2,7 @@ var game = {
init : function() {
Object.assign(this, gameBase);
this.gameArea.init(knobsAndLevers);
supporting.applyOverrides(this);
console.log('game initialized');
},
gameResets : {
......@@ -17,7 +18,9 @@ var game = {
init.afterGameOver();
},
},
gameLevelCheck : function() {
return centipedes.numberSpawned == centipedes.numberKilled && this.gameArea.frameNo;
functionOverrides : {
gameLevelCheck : function() {
return centipedes.numberSpawned == centipedes.numberKilled && this.gameArea.frameNo;
},
},
};
......@@ -17,6 +17,10 @@ var init = {
sounds.init();
texts.init();
collisions.init();
centipedes.init();
lasers.init();
gameObjects.init();
mushrooms.init();
console.log("centipede game initialized");
},
......
......@@ -20,6 +20,7 @@ var libs = [
'controls.js',
'dom.js',
'gameBase.js',
'gameObjectsBase.js',
'game-area.js',
'hud.js',
'images.js',
......
var main = {
init : function() {
Object.assign(this, mainBase);
Object.keys(this.functionOverrides).forEach(element => {
this[element] = this.functionOverrides[element];
});
supporting.applyOverrides(this);
console.log('main initialized');
},
functionOverrides : {
......
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