Commit b024864e authored by Matthew Odle's avatar Matthew Odle

integrate spiders into gameObjects

parent d78b31e1
......@@ -18,7 +18,7 @@ var collisions = {
targets.push(...centipedes.centipedes);
targets.push(...gameObjects.worms);
targets.push(...gameObjects.flies);
targets.push(...spiders.spiders);
targets.push(...gameObjects.spider);
return targets;
},
checkLaser : function(player, targets) {
......@@ -72,7 +72,6 @@ var collisions = {
getPlayerEnemies : function() {
targets = [];
targets.push(...centipedes.centipedes);
targets.push(...spiders.spiders);
targets.push(...gameObjects.flies);
return targets;
},
......@@ -103,6 +102,6 @@ var collisions = {
centipedes.centipedes = centipedes.centipedes.filter(centipede => centipede.hitPoints > 0);
gameObjects.worms = gameObjects.worms.filter(worm => worm.hitPoints > 0);
gameObjects.flies = gameObjects.flies.filter(fly => fly.hitPoints > 0);
spiders.spiders = spiders.spiders.filter(spider => spider.hitPoints > 0);
gameObjects.spider = gameObjects.spider.filter(spider => spider.hitPoints > 0);
},
}
};
......@@ -2,10 +2,12 @@
var gameObjects = {
worms : [],
flies : [],
spider : [],
init : function() {
this.intervals = {
flies : knobsAndLevers.flies.initialInterval,
worms : knobsAndLevers.worms.initialInterval,
spider : knobsAndLevers.spider.initialInterval,
};
console.log('gameObjects initialized');
},
......@@ -55,19 +57,38 @@ var gameObjects = {
},
update : function(type) {
this[type].forEach(creature => {
if (type == 'spider') {
this.setSpeed(creature);
this.setDirection(creature);
};
creature.newPos();
creature.update();
if (type == 'flies') {
this.dropMushrooms(creature);
};
if (type == 'spider') {
this.removeMushrooms(creature);
};
if (type == 'worms') {
this.changeMushrooms(creature);
};
});
},
dropMushrooms : function(fly) {
if (this.eligibleToDrop() && fly.y <= game.gameArea.canvas.height * 0.90 && fly.y >= game.gameArea.firstMushroomLayer) {
mushrooms.make({x : fly.x, y : fly.y});
setSpeed : function(creature) {
let speedLimits = knobsAndLevers[creature.type].speedLimits;
creature.speedX = Math.sign(creature.speedX) * (supporting.roll(10).crit ? speedLimits.max : speedLimits.min);
creature.speedY = creature.directionY * (supporting.roll(10).crit ? speedLimits.max : speedLimits.min);
},
setDirection : function(creature) {
if (creature.getBottom() > game.gameArea.canvas.height) {
creature.directionY = -1;
} else if (creature.getTop() < game.gameArea.canvas.height - (knobsAndLevers.player.areaHeight * 2)) {
creature.directionY = 1;
};
},
dropMushrooms : function(creature) {
if (this.eligibleToDrop() && creature.y <= game.gameArea.canvas.height * 0.90 && creature.y >= game.gameArea.firstMushroomLayer) {
mushrooms.make({x : creature.x, y : creature.y});
};
},
eligibleToDrop : function() {
......@@ -76,6 +97,13 @@ var gameObjects = {
knobsAndLevers.flies.mushroomCreateInterval
);
},
removeMushrooms : function(creature) {
let theMushroom = mushrooms.mushrooms.find(mushroom => creature.crashWith(mushroom));
if (theMushroom) {
theMushroom.hitPoints = (supporting.roll(sides = 4).crit || theMushroom.creatureTouched) ? 0 : theMushroom.hitPoints;
theMushroom.creatureTouched = true;
};
},
changeMushrooms : function(creature) {
mushrooms.mushrooms.forEach(mushroom => {
if (creature.crashWithMiddle(mushroom)) {
......
/*jslint white: true */
var spiders = {
spiders : [],
interval : 0,
init : function() {
this.interval = knobsAndLevers.spider.initialInterval;
console.log('spiders initialized');
},
manage : function() {
this.spawn();
this.update();
this.clearOutsideCanvas();
},
spawn : function() {
if (this.maxedOut()) {
return;
};
if (this.updateInterval()) {
this.make();
};
},
maxedOut : function() {
this.setMax();
return this.spiders.length >= knobsAndLevers.spider.maxNumber;
},
setMax : function() {
let tier = knobsAndLevers.game.tier;
knobsAndLevers.spider.maxNumber = tier.isMaxed ? tier.max : tier.current;
},
updateInterval : function() {
if (this.canUpdate()) {
this.setInterval();
return true;
};
return false;
},
canUpdate : function() {
return supporting.everyinterval(game.getFrameNo(), this.interval);
},
setInterval : function() {
let min = knobsAndLevers.spider.interval.min;
let max = knobsAndLevers.spider.interval.max;
this.interval = supporting.getRandom(min, max);
},
make : function() {
let defaults = knobsAndLevers.spider;
let spider = new Component(defaults.args);
let points = defaults.points;
spider.pointValue = supporting.getRandom(points.base, points.base + points.range);
spider.speedX = supporting.getRandom(-1, 1);
if (spider.speedX < 0) {
spider.x = game.gameArea.canvas.width - 1;
};
spider.y = supporting.getRandom(knobsAndLevers.player.topLimit - knobsAndLevers.player.areaHeight, game.gameArea.canvas.height);
spider.directionY = defaults.directionY;
this.spiders.push(spider);
},
update : function() {
this.spiders.forEach(spider => {
this.removeMushrooms(spider);
this.updateSpeed(spider);
this.updatePos(spider);
this.updateComponent(spider);
this.updateYDirection(spider);
});
},
removeMushrooms : function(spider) {
let theMushroom = mushrooms.mushrooms.find(mushroom => spider.crashWith(mushroom));
// TODO 1d4, ignore if not 4
let doRemove = supporting.roll(4) == 4 ? true : false;
if (theMushroom) {
theMushroom.hitPoints = (doRemove || theMushroom.spiderTouched) ? 0 : theMushroom.hitPoints;
theMushroom.spiderTouched = true;
};
},
updateSpeed : function(spider) {
// TODO work on the spider movement algorithm
// spiders need to move up and down erratically, and occasionally move horizontally
let speedLimits = knobsAndLevers.spider.speedLimits;
spider.speedX = spider.speedX == 0 ? speedLimits.min : spider.speedX;
// TODO supporting.getRandom is a little confusing
// basically, most of the time we want to do speedLimits.min (when less than 0, to avoid direction switching)
// turn this into a roll check instead. 1d20 if 20, set speed to max, else speed to min
// -.-
spider.speedX = Math.sign(spider.speedX) * (supporting.getRandom(-50, 5) < 0 ? speedLimits.min : speedLimits.max);
spider.speedY = spider.directionY * (supporting.getRandom(-50, 5) < 0 ? speedLimits.min : speedLimits.max);
},
updatePos : function(spider) {
spider.newPos();
},
updateComponent : function(spider) {
spider.update();
},
updateYDirection : function(spider) {
if (spider.getBottom() > game.gameArea.canvas.height) {
spider.directionY = -1;
} else if (spider.getTop() < game.gameArea.canvas.height - (knobsAndLevers.player.areaHeight * 2)) {
spider.directionY = 1;
};
},
clearOutsideCanvas : function() {
if (this.spiders == false) { return; };
this.spiders = this.spiders.filter(spider =>
spider.x < game.gameArea.canvas.width
&& spider.x > -spider.width
);
},
clear : function() {
this.spiders = [];
},
};
......@@ -232,8 +232,6 @@ var knobsAndLevers = {
},
constructorFunctions : {
setX : function(player) {
console.log(player);
console.log(players.player);
player.x = knobsAndLevers.player.startX[Object.keys(players.players).length];
},
},
......@@ -266,16 +264,13 @@ var knobsAndLevers = {
},
spider : {
maxNumber : 0,
directionY : 1,
points : {
base : 500,
range : 400,
},
pointValue : 400,
interval : {
min: 800,
max: 1500,
min : 10,
max : 20,
},
args : {
directionY : 1,
extraArgs : {
animationInterval : 50,
hitPoints : 1,
......@@ -288,6 +283,21 @@ var knobsAndLevers = {
getSpriteKey : function(obj) {
return 'one';
},
constructorFunctions : {
setY : function(spider) {
spider.y = supporting.getRandom(knobsAndLevers.player.topLimit - knobsAndLevers.player.areaHeight, game.gameArea.canvas.height);
},
setX : function(spider) {
let theRoll = supporting.roll(sides = 2);
if (theRoll.crit) {
spider.speedX = -1;
spider.x = game.gameArea.canvas.width;
} else {
spider.speedX = 1;
spider.x = 1 - spider.width;
};
},
},
},
speedLimits : {
min : 0.01,
......@@ -324,8 +334,8 @@ var knobsAndLevers = {
},
},
worms : {
maxNumber: 1,
pointValue :200,
maxNumber : 1,
pointValue : 200,
interval : {
min: 0,
max: 0,
......
......@@ -12,7 +12,6 @@ var main = {
mushrooms.manage();
centipedes.manage();
gameObjects.manage();
spiders.manage();
lasers.manage();
players.manage();
collisions.check();
......
......@@ -47,7 +47,7 @@ var sounds = {
};
},
manageSpiderSounds : function() {
if (spiders.spiders != false) {
if (gameObjects.spiders != false) {
this.playSound(this.getSound('spider'));
};
},
......
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